Skip to content

Commit 0087452

Browse files
authored
Merge pull request #2419 from krao14/krao14-feature-qbusiness-s3-cdk-python
New serverless pattern - qbusiness-s3-cdk-python
2 parents ff14c65 + b3938b7 commit 0087452

15 files changed

+569
-0
lines changed

qbusiness-s3-cdk-python/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Amazon Q Business to Amazon Simple Storage Service (Amazon S3)
2+
3+
This pattern contains a sample 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.
4+
5+
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.
6+
7+
## Requirements
8+
* [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.
9+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
10+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
11+
* [AWS CDK CLI](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html) (AWS CDK) installed
12+
* [Enable AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/get-set-up-for-idc.html)
13+
* [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.
14+
* [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.
15+
16+
## Deployment Instructions
17+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
18+
```
19+
git clone https://github.com/aws-samples/serverless-patterns
20+
```
21+
1. Change directory to the pattern directory:
22+
```
23+
cd qbusiness-s3-cdk-python
24+
```
25+
1. From the command line, use AWS CDK to deploy the AWS resources for the pattern as specified in the template.yml file:
26+
```
27+
cdk deploy --parameters S3DSBucketName=${YourS3BucketName} --parameters IdentityCenterInstanceArn=${YourIdentityCenterInstanceArn}
28+
```
29+
1. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing.
30+
31+
# How it works
32+
Please refer to the architecture diagram below:
33+
34+
![End to End Architecture](images/architecture.png)
35+
36+
Here's a breakdown of the steps:
37+
38+
**Amazon Q Business Application:** Amazon Q Business application created with S3 as the data source.
39+
40+
**Amazon S3:** S3 bucket that contains documents to be indexed.
41+
42+
**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.
43+
44+
## Testing
45+
46+
1. Go to the Amazon Q Business Console and verify that your application `MyQBusinessApp-${StackName}` has been created.
47+
![Amazon Q Business Application](images/qbusiness-application.png)
48+
49+
1. Click on the Name of the Application. Scroll down to the `Groups and Users` section. Click on `Manage access and Subscriptions`.
50+
![Groups and Users Section](images/groups-users.png)
51+
1. Click on `Add groups and users` and select `Assign existing users and groups`. Click `Next`.
52+
53+
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.
54+
![Assign users](images/assign-users-groups.png)
55+
1. Add the name of the user and click on `Assign`.
56+
![Assign user](images/assign-user.png)
57+
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`.
58+
![User subscription](images/subscription.png)
59+
1. Go back to your application. Under `Web experience settings`, copy the `Deployed URL` link.
60+
![Deployed URL](images/deployed-url.png)
61+
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.
62+
![Q Business Web Experience](images/chat-interface.png)
63+
64+
## Cleanup
65+
66+
1. Delete the stack
67+
```bash
68+
cdk destroy
69+
```
70+
----
71+
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
72+
73+
SPDX-License-Identifier: MIT-0

qbusiness-s3-cdk-python/app.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
import aws_cdk as cdk
5+
6+
from aws_cdk import (
7+
Stack,
8+
aws_qbusiness as qbusiness,
9+
aws_iam as iam,
10+
CfnParameter,
11+
CfnOutput,
12+
triggers,
13+
aws_lambda as lambda_,
14+
Duration
15+
)
16+
from constructs import Construct
17+
18+
class QBusinessStack(Stack):
19+
20+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
21+
super().__init__(scope, construct_id, **kwargs)
22+
23+
# Parameters
24+
s3_bucket_name = CfnParameter(self, "S3DSBucketName", type="String", description="Enter the S3 bucket name where the contents you want to be indexed are stored.")
25+
identity_center_arn = CfnParameter(self, "IdentityCenterInstanceArn", type="String", description="Enter the ARN of the Amazon Q Business Identity Center instance.")
26+
27+
# Q Business Application
28+
qbusiness_app = qbusiness.CfnApplication(
29+
self, "QBusinessApplication",
30+
display_name=f"MyQBusinessApp-{self.stack_name}",
31+
description="Amazon Q Business Application",
32+
identity_center_instance_arn=identity_center_arn.value_as_string
33+
)
34+
35+
# Web Experience Role
36+
web_exp_role = iam.Role(
37+
self, "QBusinessWebExperienceRole",
38+
assumed_by=iam.ServicePrincipal("application.qbusiness.amazonaws.com"),
39+
role_name=f"QBusinessWebExperienceRole-{self.stack_name}",
40+
description="IAM role for Q Business Web Experience",
41+
inline_policies={"WebExperiencePolicy": iam.PolicyDocument(
42+
statements=[
43+
iam.PolicyStatement(
44+
sid="QBusinessConversationPermission",
45+
actions=[
46+
"qbusiness:Chat",
47+
"qbusiness:ChatSync",
48+
"qbusiness:ListMessages",
49+
"qbusiness:ListConversations",
50+
"qbusiness:DeleteConversation",
51+
"qbusiness:PutFeedback",
52+
"qbusiness:GetWebExperience",
53+
"qbusiness:GetApplication",
54+
"qbusiness:ListPlugins",
55+
"qbusiness:GetChatControlsConfiguration"
56+
],
57+
resources=[qbusiness_app.attr_application_arn]
58+
),
59+
iam.PolicyStatement(
60+
sid="QBusinessKMSDecryptPermissions",
61+
actions=["kms:Decrypt"],
62+
resources=[f"arn:{self.partition}:kms:{self.region}:{self.account}:key/*"],
63+
conditions={
64+
"StringLike": {
65+
"kms:ViaService": f"qbusiness.{self.region}.amazonaws.com"
66+
}
67+
}
68+
),
69+
iam.PolicyStatement(
70+
sid="QBusinessSetContextPermissions",
71+
actions=["sts:SetContext"],
72+
resources=["arn:aws:sts::*:self"],
73+
conditions={
74+
"StringLike": {
75+
"aws:CalledViaLast": "qbusiness.amazonaws.com"
76+
}
77+
}
78+
)
79+
]
80+
)
81+
}
82+
)
83+
84+
# Adding set context action to web experience role
85+
web_exp_role.assume_role_policy.add_statements(iam.PolicyStatement(
86+
sid="QBusinessSetContextPermissions",
87+
actions=["sts:SetContext"],
88+
principals=[iam.ServicePrincipal("application.qbusiness.amazonaws.com")]
89+
))
90+
91+
# Web Experience
92+
qbusiness.CfnWebExperience(
93+
self, "QBusinessWebExperience",
94+
application_id=qbusiness_app.ref,
95+
role_arn=web_exp_role.role_arn
96+
)
97+
98+
# Index
99+
qbusiness_index = qbusiness.CfnIndex(
100+
self, "QBusinessIndex",
101+
display_name="MyQBusinessIndex",
102+
description="My Amazon Q Business Index",
103+
application_id=qbusiness_app.ref
104+
)
105+
106+
# Retriever
107+
qbusiness.CfnRetriever(
108+
self, "QBusinessRetriever",
109+
application_id=qbusiness_app.ref,
110+
configuration=qbusiness.CfnRetriever.RetrieverConfigurationProperty(
111+
native_index_configuration=qbusiness.CfnRetriever.NativeIndexConfigurationProperty(
112+
index_id=qbusiness_index.attr_index_id)
113+
),
114+
display_name="MyQBusinessRetriever",
115+
type="NATIVE_INDEX"
116+
)
117+
118+
# S3 Data Source Role
119+
s3_data_source_role = iam.Role(
120+
self, "S3DataSourceRole",
121+
assumed_by=iam.ServicePrincipal("qbusiness.amazonaws.com"),
122+
inline_policies={"S3DataSourcePolicy": iam.PolicyDocument(
123+
statements=[
124+
iam.PolicyStatement(
125+
actions=["s3:GetObject"],
126+
resources=[f"arn:aws:s3:::{s3_bucket_name.value_as_string}/*"],
127+
conditions={
128+
"StringEquals": {
129+
"aws:ResourceAccount": [self.account]
130+
}
131+
}
132+
),
133+
iam.PolicyStatement(
134+
actions=["s3:ListBucket"],
135+
resources=[f"arn:aws:s3:::{s3_bucket_name.value_as_string}"],
136+
conditions={
137+
"StringEquals": {
138+
"aws:ResourceAccount": [self.account]
139+
}
140+
}
141+
),
142+
iam.PolicyStatement(
143+
actions=[
144+
"qbusiness:BatchPutDocument",
145+
"qbusiness:BatchDeleteDocument"
146+
],
147+
resources=[f"arn:aws:qbusiness:{self.region}:{self.account}:application/{qbusiness_app.ref}/index/*"]
148+
),
149+
iam.PolicyStatement(
150+
actions=[
151+
"qbusiness:PutGroup",
152+
"qbusiness:CreateUser",
153+
"qbusiness:DeleteGroup",
154+
"qbusiness:UpdateUser",
155+
"qbusiness:ListGroups"
156+
],
157+
resources=[
158+
f"arn:aws:qbusiness:{self.region}:{self.account}:application/{qbusiness_app.ref}",
159+
f"arn:aws:qbusiness:{self.region}:{self.account}:application/{qbusiness_app.ref}/index/*"]
160+
)
161+
]
162+
)
163+
}
164+
)
165+
166+
# S3 Data Source
167+
s3_data_source = qbusiness.CfnDataSource(
168+
self, "S3DataSource",
169+
application_id=qbusiness_app.ref,
170+
display_name="MyS3DataSource",
171+
description="S3 Data Source for Amazon Q Business",
172+
role_arn=s3_data_source_role.role_arn,
173+
configuration={
174+
"connectionConfiguration": {
175+
"repositoryEndpointMetadata": {
176+
"BucketName": s3_bucket_name.value_as_string
177+
}
178+
},
179+
"repositoryConfigurations": {
180+
"document": {
181+
"fieldMappings": [
182+
{
183+
"indexFieldName": "s3_document_id",
184+
"indexFieldType": "STRING",
185+
"dataSourceFieldName": "s3_document_id"
186+
}
187+
]
188+
}
189+
},
190+
"syncMode": "FULL_CRAWL",
191+
"type": "S3",
192+
"version": "1.0.0"
193+
},
194+
index_id=qbusiness_index.attr_index_id,
195+
)
196+
197+
s3_data_source.node.add_dependency(qbusiness_index)
198+
199+
# Create a role for the DataSourceSyncLambda
200+
data_source_sync_lambda_role = iam.Role(
201+
self, "DataSourceSyncLambdaRole",
202+
assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),
203+
managed_policies=[
204+
iam.ManagedPolicy.from_aws_managed_policy_name("CloudWatchLogsFullAccess")],
205+
inline_policies={
206+
"QBusinessDataSourceSyncPolicy": iam.PolicyDocument(
207+
statements=[
208+
iam.PolicyStatement(
209+
actions=[
210+
"qbusiness:StartDataSourceSyncJob",
211+
"qbusiness:StopDataSourceSyncJob"
212+
],
213+
resources=[
214+
qbusiness_app.attr_application_arn,
215+
f"{qbusiness_app.attr_application_arn}/*"]
216+
)
217+
]
218+
)
219+
}
220+
)
221+
222+
# Lambda function for initiating data source sync
223+
data_source_sync_lambda = lambda_.Function(
224+
self, "DataSourceSyncLambda",
225+
runtime=lambda_.Runtime.PYTHON_3_12,
226+
code=lambda_.Code.from_asset("src/dataSourceSync"),
227+
handler="dataSourceSyncLambda.lambda_handler",
228+
timeout=Duration.minutes(15),
229+
memory_size=1024,
230+
role = data_source_sync_lambda_role,
231+
environment={
232+
"INDEX_ID": qbusiness_index.attr_index_id,
233+
"DS_ID": s3_data_source.attr_data_source_id,
234+
"APP_ID": qbusiness_app.ref
235+
}
236+
)
237+
238+
# Trigger data source sync lambda
239+
triggers.Trigger(self, "data_source_sync_lambda_trigger",
240+
handler=data_source_sync_lambda,
241+
timeout=Duration.minutes(10),
242+
invocation_type=triggers.InvocationType.EVENT
243+
)
244+
245+
# Define the outputs
246+
qbusiness_app_id_output = CfnOutput(
247+
self, "QBusinessApplicationId",
248+
value=qbusiness_app.ref,
249+
description="Amazon Q Business Application ID"
250+
)
251+
252+
s3_data_source_id_output = CfnOutput(
253+
self, "S3DataSourceId",
254+
value=s3_data_source.ref,
255+
description="S3 Data Source ID"
256+
)
257+
258+
app = cdk.App()
259+
QBusinessStack(app, "QBusinessStack")
260+
261+
app.synth()

0 commit comments

Comments
 (0)