-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2412 from krao14/krao14-feature-qbusiness-s3-lambda
New serverless pattern - qbusiness-s3-lambda
- Loading branch information
Showing
14 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Amazon Q Business to Amazon Simple Storage Service (Amazon S3) | ||
|
||
This pattern contains a sample AWS SAM stack that leverages Amazon Q Business to build a generative AI application to derive insights from content present in an S3 bucket. An AWS Lambda function initiates the crawling and indexing of the documents present in the specified S3 bucket. Users can then ask questions to the Amazon Q Business application to receive a generated response. | ||
|
||
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
|
||
## Requirements | ||
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed | ||
* [Enable AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/get-set-up-for-idc.html) | ||
* [Create Users in AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/addusers.html). Note down the Instance ARN by going to the AWS IAM Identity Center console --> Settings --> Instance ARN. You will require it when deploying the stack. | ||
* [Create an S3 Bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/creating-bucket.html) and [upload documents](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) that you want to be indexed. If you already have an S3 bucket with data that you want to crawl, you can skip this step. | ||
|
||
## Deployment Instructions | ||
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: | ||
``` | ||
git clone https://github.com/aws-samples/serverless-patterns | ||
``` | ||
1. Change directory to the pattern directory: | ||
``` | ||
cd qbusiness-s3-lambda | ||
``` | ||
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: | ||
``` | ||
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM | ||
``` | ||
1. During the prompts: | ||
|
||
* Enter a stack name. | ||
* Enter the desired AWS Region. | ||
* Enter the S3 bucket name containing the contents you want to crawl. | ||
* Enter the AWS IAM Identity Center Instance ARN noted from the instructions followed in Requirements. | ||
* Allow SAM CLI to create IAM roles with the required permissions. | ||
|
||
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.* | ||
|
||
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
||
# How it works | ||
Please refer to the architecture diagram below: | ||
|
||
![End to End Architecture](images/architecture.png) | ||
|
||
Here's a breakdown of the steps: | ||
|
||
**Amazon Q Business Application:** Amazon Q Business application created with S3 as the data source. | ||
|
||
**Amazon S3:** S3 bucket that contains documents to be indexed. | ||
|
||
**AWS Lambda:** AWS Lambda function `DataSourceSync` crawls and indexes the content from the S3 bucket. The Amazon Q Business application retrieves data from the indexed content and provides a generated response. | ||
|
||
## Testing | ||
|
||
1. Go to the Amazon Q Business Console and verify that your application `MyQBusinessApp-${StackName}` has been created. | ||
![Amazon Q Business Application](images/qbusiness-application.png) | ||
|
||
1. Click on the Name of the Application. Scroll down to the `Groups and Users` section. Click on `Manage access and Subscriptions`. | ||
![Groups and Users Section](images/groups-users.png) | ||
1. Click on `Add groups and users` and select `Assign existing users and groups`. Click `Next`. | ||
|
||
Note: If you have NOT already created a user in the Requirements section, then create one by choosing `Add and assign new users` instead and add the user. | ||
![Assign users](images/assign-users-groups.png) | ||
1. Add the name of the user and click on `Assign`. | ||
![Assign user](images/assign-user.png) | ||
1. Select the user and in the `Change subscription` dropdown, select `Update subscription tier`. In the `New subscription` dropdown, choose `Q Business Lite` and `Confirm`. | ||
![User subscription](images/subscription.png) | ||
1. Go back to your application. Under `Web experience settings`, copy the `Deployed URL` link. | ||
![Deployed URL](images/deployed-url.png) | ||
1. Open the URL in a New Incognito Window. Login to the web experience with the credentials of the created user. Ask a question in the chat interface regarding the documents you have in the S3 bucket provided as a data source. | ||
![Q Business Web Experience](images/chat-interface.png) | ||
|
||
## Cleanup | ||
|
||
1. Delete the stack | ||
```bash | ||
sam delete | ||
``` | ||
---- | ||
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: MIT-0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"title": "Q Business to S3", | ||
"description": "Amazon Q Business to build a generative AI application to derive insights from content present in an S3 bucket.", | ||
"language": "Python", | ||
"level": "200", | ||
"framework": "SAM", | ||
"introBox": { | ||
"headline": "How it works", | ||
"text": [ | ||
"Amazon Q Business Application: Amazon Q Business application created with S3 as the data source.", | ||
"Amazon S3: S3 bucket that contains documents to be indexed.", | ||
"AWS Lambda: AWS Lambda function `DataSourceSync` crawls and indexes the content from the S3 bucket. The Amazon Q Business application retrieves data from the indexed content and provides a generated response." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/qbusiness-s3-lambda", | ||
"templateURL": "serverless-patterns/qbusiness-s3-lambda", | ||
"projectFolder": "qbusiness-s3-lambda", | ||
"templateFile": "template.yaml" | ||
} | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Amazon Q Business - AI Assistant for Enterprise", | ||
"link": "https://aws.amazon.com/q/business/" | ||
}, | ||
{ | ||
"text": "Discover insights with Amazon Q S3 connector", | ||
"link": "https://aws.amazon.com/blogs/machine-learning/discover-insights-from-amazon-s3-with-amazon-q-s3-connector/" | ||
} | ||
] | ||
}, | ||
"deploy": { | ||
"text": [ | ||
"sam deploy --guided" | ||
] | ||
}, | ||
"testing": { | ||
"text": [ | ||
"See the GitHub repo for detailed testing instructions." | ||
] | ||
}, | ||
"cleanup": { | ||
"text": [ | ||
"Delete the stack: <code>sam delete</code>." | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Kruthi Jayasimha Rao", | ||
"bio": "Kruthi is a Partner Solutions Architect with a focus in Generative AI. She provides technical guidance to AWS Partners in following best practices to build secure, resilient, and highly available solutions in the AWS Cloud.", | ||
"linkedin": "https://www.linkedin.com/in/kruthi-jayasimha-rao-132a78167" | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{ | ||
"title": "Amazon Q Business to S3", | ||
"description": "Use Amazon Q Business to build a generative AI application to derive insights from content present in an S3 bucket.", | ||
"language": "Python", | ||
"level": "200", | ||
"framework": "SAM", | ||
"introBox": { | ||
"headline": "How it works", | ||
"text": [ | ||
"Amazon Q Business Application: Amazon Q Business application created with S3 as the data source.", | ||
"Amazon S3: S3 bucket that contains documents to be indexed.", | ||
"AWS Lambda: AWS Lambda function `DataSourceSync` crawls and indexes the content from the S3 bucket. The Amazon Q Business application retrieves data from the indexed content and provides a generated response." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/qbusiness-s3-lambda", | ||
"templateURL": "serverless-patterns/qbusiness-s3-lambda", | ||
"projectFolder": "qbusiness-s3-lambda", | ||
"templateFile": "template.yaml" | ||
} | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Amazon Q Business - AI Assistant for Enterprise", | ||
"link": "https://aws.amazon.com/q/business/" | ||
}, | ||
{ | ||
"text": "Discover insights with Amazon Q S3 connector", | ||
"link": "https://aws.amazon.com/blogs/machine-learning/discover-insights-from-amazon-s3-with-amazon-q-s3-connector/" | ||
} | ||
] | ||
}, | ||
"deploy": { | ||
"text": [ | ||
"sam deploy --guided --capabilities CAPABILITY_NAMED_IAM" | ||
] | ||
}, | ||
"testing": { | ||
"text": [ | ||
"See the GitHub repo for detailed testing instructions." | ||
] | ||
}, | ||
"cleanup": { | ||
"text": [ | ||
"Delete the stack: <code>sam delete</code>." | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Kruthi Jayasimha Rao", | ||
"bio": "Kruthi is a Partner Solutions Architect with a focus in Generative AI. She provides technical guidance to AWS Partners in following best practices to build secure, resilient, and highly available solutions in the AWS Cloud.", | ||
"linkedin": "kruthi-jayasimha-rao-132a78167" | ||
} | ||
], | ||
"patternArch": { | ||
"icon1": { | ||
"x": 20, | ||
"y": 50, | ||
"service": "q", | ||
"label": "Amazon Q Business" | ||
}, | ||
"icon2": { | ||
"x": 50, | ||
"y": 50, | ||
"service": "s3", | ||
"label": "Amazon S3" | ||
}, | ||
"icon3": { | ||
"x": 80, | ||
"y": 50, | ||
"service": "lambda", | ||
"label": "AWS Lambda" | ||
}, | ||
"line1": { | ||
"from": "icon1", | ||
"to": "icon2" | ||
}, | ||
"line2": { | ||
"from": "icon2", | ||
"to": "icon3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2016 Amazon Web Services, Inc. or its affiliates. All Rights Reserved. | ||
# This file is licensed to you under the AWS Customer Agreement (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at http://aws.amazon.com/agreement/ . | ||
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. | ||
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
from __future__ import print_function | ||
import urllib3 | ||
import json | ||
|
||
SUCCESS = "SUCCESS" | ||
FAILED = "FAILED" | ||
|
||
http = urllib3.PoolManager() | ||
|
||
|
||
def send(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False, reason=None): | ||
responseUrl = event['ResponseURL'] | ||
|
||
print(responseUrl) | ||
|
||
responseBody = { | ||
'Status' : responseStatus, | ||
'Reason' : reason or "See the details in CloudWatch Log Stream: {}".format(context.log_stream_name), | ||
'PhysicalResourceId' : physicalResourceId or context.log_stream_name, | ||
'StackId' : event['StackId'], | ||
'RequestId' : event['RequestId'], | ||
'LogicalResourceId' : event['LogicalResourceId'], | ||
'NoEcho' : noEcho, | ||
'Data' : responseData | ||
} | ||
|
||
json_responseBody = json.dumps(responseBody) | ||
|
||
print("Response body:") | ||
print(json_responseBody) | ||
|
||
headers = { | ||
'content-type' : '', | ||
'content-length' : str(len(json_responseBody)) | ||
} | ||
|
||
try: | ||
response = http.request('PUT', responseUrl, headers=headers, body=json_responseBody) | ||
print("Status code:", response.status) | ||
|
||
except Exception as e: | ||
|
||
print("send(..) failed executing http.request(..):", e) |
31 changes: 31 additions & 0 deletions
31
qbusiness-s3-lambda/src/dataSourceSync/dataSourceSyncLambda.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
import json | ||
import logging | ||
import boto3 | ||
import cfnresponse | ||
import random | ||
import os | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
INDEX_ID = os.environ['INDEX_ID'] | ||
DS_ID = os.environ['DS_ID'] | ||
AWS_REGION = os.environ['AWS_REGION'] | ||
QBUSINESS = boto3.client('qbusiness') | ||
APP_ID = os.environ['APP_ID'] | ||
|
||
def start_data_source_sync(dsId, indexId, appID): | ||
logger.info(f"start_data_source_sync(dsId={dsId}, indexId={indexId})") | ||
resp = QBUSINESS.start_data_source_sync_job(dataSourceId=dsId, indexId=indexId, applicationId=appID) | ||
logger.info(f"response:" + json.dumps(resp)) | ||
|
||
def lambda_handler(event, context): | ||
logger.info("Received event: %s" % json.dumps(event)) | ||
start_data_source_sync(DS_ID, INDEX_ID, APP_ID) | ||
status = cfnresponse.SUCCESS | ||
cfnresponse.send(event, context, status, {}, None) | ||
return status |
Oops, something went wrong.