-
Notifications
You must be signed in to change notification settings - Fork 102
/
kendra-docs-index.yaml
219 lines (204 loc) · 7.56 KB
/
kendra-docs-index.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
##This CloudFormation template creates an Amazon Kendra index. It adds a webcrawler datasource
##to the index and crawls the online AWS Documentation for Amazon Kendra, Amazon Lex and Amazon SageMaker
##After the datasource is configured, it triggers a datasource sync, i.e. the process to crawl the sitemaps
##and index the crawled documents.
##The output of the CloudFormation template shows the Kendra index id and the AWS region it was created in.
##It takes about 30 minutes to create an Amazon Kendra index and about 15 minutes more to crawl and index
##the content of these webpages to the index. Hence you might need to wait for about 45 minutes after
##launching the CloudFormation stack
Resources:
##Create the Role needed to create a Kendra Index
KendraIndexRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: kendra.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Resource: '*'
Condition:
StringEquals:
'cloudwatch:namespace': 'Kendra'
Action:
- 'cloudwatch:PutMetricData'
- Effect: Allow
Resource: '*'
Action: 'logs:DescribeLogGroups'
- Effect: Allow
Resource: !Sub
- 'arn:aws:logs:${region}:${account}:log-group:/aws/kendra/*'
- region: !Ref 'AWS::Region'
account: !Ref 'AWS::AccountId'
Action: 'logs:CreateLogGroup'
- Effect: Allow
Resource: !Sub
- 'arn:aws:logs:${region}:${account}:log-group:/aws/kendra/*:log-stream:*'
- region: !Ref 'AWS::Region'
account: !Ref 'AWS::AccountId'
Action:
- 'logs:DescribeLogStreams'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
PolicyName: !Join
- ''
- - !Ref 'AWS::StackName'
- '-DocsKendraIndexPolicy'
RoleName: !Join
- ''
- - !Ref 'AWS::StackName'
- '-DocsKendraIndexRole'
##Create the Kendra Index
DocsKendraIndex:
Type: 'AWS::Kendra::Index'
Properties:
Name: !Join
- ''
- - !Ref 'AWS::StackName'
- '-Index'
Edition: !Ref KendraEdition
RoleArn: !GetAtt KendraIndexRole.Arn
##Create the Role needed to attach the Webcrawler Data Source
KendraDSRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: kendra.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Resource: !Sub
- 'arn:aws:kendra:${region}:${account}:index/${index}'
- region: !Ref 'AWS::Region'
account: !Ref 'AWS::AccountId'
index: !GetAtt DocsKendraIndex.Id
Action:
- 'kendra:BatchPutDocument'
- 'kendra:BatchDeleteDocument'
PolicyName: !Join
- ''
- - !Ref 'AWS::StackName'
- '-DocsDSPolicy'
RoleName: !Join
- ''
- - !Ref 'AWS::StackName'
- '-DocsDSRole'
#Docs Data Source
KendraDocsDS:
Type: 'AWS::Kendra::DataSource'
Properties:
DataSourceConfiguration:
WebCrawlerConfiguration:
UrlInclusionPatterns:
- '.*https://docs.aws.amazon.com/lex/.*'
- '.*https://docs.aws.amazon.com/kendra/.*'
- '.*https://docs.aws.amazon.com/sagemaker/.*'
Urls:
SiteMapsConfiguration:
SiteMaps:
- 'https://docs.aws.amazon.com/lex/latest/dg/sitemap.xml'
- 'https://docs.aws.amazon.com/kendra/latest/dg/sitemap.xml'
- 'https://docs.aws.amazon.com/sagemaker/latest/dg/sitemap.xml'
IndexId: !GetAtt DocsKendraIndex.Id
Name: 'KendraDocsDS'
RoleArn: !GetAtt KendraDSRole.Arn
Type: 'WEBCRAWLER'
DataSourceSyncLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Resource: !Sub
- 'arn:aws:kendra:${region}:${account}:index/${index}*'
- region: !Ref 'AWS::Region'
account: !Ref 'AWS::AccountId'
index: !GetAtt DocsKendraIndex.Id
Action:
- 'kendra:*'
PolicyName: DataSourceSyncLambdaPolicy
DataSourceSyncLambda:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Runtime: python3.8
Role: !GetAtt 'DataSourceSyncLambdaRole.Arn'
Timeout: 900
MemorySize: 1024
Code:
ZipFile: |
# 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']
KENDRA = boto3.client('kendra')
def start_data_source_sync(dsId, indexId):
logger.info(f"start_data_source_sync(dsId={dsId}, indexId={indexId})")
resp = KENDRA.start_data_source_sync_job(Id=dsId, IndexId=indexId)
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)
status = cfnresponse.SUCCESS
cfnresponse.send(event, context, status, {}, None)
return status
Environment:
Variables:
INDEX_ID: !GetAtt DocsKendraIndex.Id
DS_ID: !GetAtt KendraDocsDS.Id
DataSourceSync:
Type: Custom::DataSourceSync
DependsOn:
- DocsKendraIndex
- KendraDocsDS
Properties:
ServiceToken: !GetAtt DataSourceSyncLambda.Arn
Parameters:
KendraEdition:
Type: String
Default: 'ENTERPRISE_EDITION'
AllowedValues:
- 'ENTERPRISE_EDITION'
- 'DEVELOPER_EDITION'
Description: 'ENTERPRISE_EDITION (default) is recommended for production deployments, and offers high availability and scale up capabilities. DEVELOPER_EDITION (Free Tier eligible) is suitable for temporary, non-production, experimental workloads. NOTE: indexes cannot currently be migrated from one type to another.'
Outputs:
KendraIndexID:
Value: !GetAtt DocsKendraIndex.Id
AWSRegion:
Value: !Ref 'AWS::Region'