-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.yaml
More file actions
529 lines (502 loc) · 17.9 KB
/
assignment3.yaml
File metadata and controls
529 lines (502 loc) · 17.9 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
AWSTemplateFormatVersion: 2010-09-09
Description: Template for our assignment 3 implementation
Resources:
PhotosDomain:
Type: AWS::OpenSearchService::Domain
Properties:
DomainName: photos
EBSOptions:
EBSEnabled: true
VolumeSize: 10
VolumeType: gp2
ClusterConfig:
InstanceType: t3.small.search
InstanceCount: 1
AccessPolicies:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !GetAtt LF1IAMRole.Arn
Action:
- es:ESHttpPost
- es:ESHttpPut
- es:ESHttpDelete
Resource: !Sub arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/photos/*
B1:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub front-end-bucket-${AWS::AccountId}-${AWS::Region}
WebsiteConfiguration:
IndexDocument: index.html
# Enable public access
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: false
IgnorePublicAcls: true
RestrictPublicBuckets: false
# Grants public read access
B1Policy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref B1
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: '*'
Action: s3:GetObject
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Ref B1
- /*
#Enables P2 to write front end to the bucket
- Sid: CodePipelineWriteAccess
Effect: Allow
Principal:
AWS: "arn:aws:iam::346225466066:role/service-role/AWSCodePipelineServiceRole-us-east-1-P2"
Action:
- s3:PutObject
- s3:GetObject
- s3:ListBucket
Resource:
- !Join [ "", [ "arn:aws:s3:::", !Ref B1 ] ]
- !Join [ "", [ "arn:aws:s3:::", !Ref B1, "/*" ] ]
B2:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
#B2Permission needs to be created first so that B2 has correct permissions for
#lambda configurations.
DependsOn: B2Permission
Properties:
BucketName: !Sub photosbucket-${AWS::AccountId}-${AWS::Region}
NotificationConfiguration:
#Allows S3 to use put method on LF1
LambdaConfigurations:
- Event: s3:ObjectCreated:Put
Filter:
S3Key:
Rules:
- Name: suffix
Value: .jpg
Function: !GetAtt LF1.Arn
B2Permission:
#Gives B2 permision to invoke LF1
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LF1
Principal: s3.amazonaws.com
SourceArn: !Sub arn:aws:s3:::photosbucket-${AWS::AccountId}-${AWS::Region}
LF1:
Type: AWS::Lambda::Function
DependsOn: LF1IAMRole
Properties:
#IAM Role that LF1 must assume.
Role: !GetAtt LF1IAMRole.Arn
Runtime: python3.12
FunctionName: index-photos
#Source code for lambda function
Code:
ZipFile: |
def main(event, context):
return "Hello World!"
Handler: index.main
Timeout: 15
LF1IAMRole:
Type: AWS::IAM::Role
Properties:
#Allows a lambda instance to assume this role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
#Allows a lambda to write logs to cloudwatch
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
#Provides the necessary permissions to lambda
Policies:
- PolicyName: LF1Permissions
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
- s3:HeadObject
#The /* gives the lambda object leve interactions. Without it,
#it can only perform bucket level actions like deleting the bucket.
#Hardcoding the arn for B2 is considered bad practice. It is done
#here to break the dependency loop B2->LF1->LF1IAMRole->B2
Resource:
- !Sub arn:aws:s3:::photosbucket-${AWS::AccountId}-${AWS::Region}
- !Sub arn:aws:s3:::photosbucket-${AWS::AccountId}-${AWS::Region}/*
- Effect: Allow
Action: rekognition:DetectLabels
#Wild card is used instead of ARN, because Rekognition
#is an API based service, so there is no specific ARN
#that it needs access to.
Resource: '*'
- Effect: Allow
Action:
- es:ESHttpPost
- es:ESHttpPut
- es:ESHttpDelete
Resource: !Sub arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/photos/*
LF2IAMRole:
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:
- PolicyName: LF2Permissions
PolicyDocument:
Version: '2012-10-17'
Statement:
# add es:ESHttpGet / ESHttpPost etc here when you wire OpenSearch
- Effect: Allow
Action:
- es:ESHttpGet
- es:ESHttpPost
Resource: '*'
ApiGatewayS3Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: ApiGatewayS3WritePhotos
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
Resource: !Sub arn:aws:s3:::photosbucket-${AWS::AccountId}-${AWS::Region}/*
BotRuntimeRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lexv2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: LexRuntimeRolePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- polly:SynthesizeSpeech
- comprehend:DetectSentiment
Resource: '*'
# 2. Inline bot definition which depends on the IAM role
# The bot definition consists of combining all the child resources into one CFN resource.
# This includes Locales, Intents, Slots, and SlotTypes.
PhotoSearchBot:
DependsOn: BotRuntimeRole
Type: AWS::Lex::Bot
Properties:
Name: PhotoSearcher
RoleArn: !GetAtt BotRuntimeRole.Arn
DataPrivacy:
ChildDirected: false
IdleSessionTTLInSeconds: 300
Description: Takes user input to get keywords to search with
# Provide a setting that allows you to either auto build the locales provided.
# Locale builds are also kicked off if you attempt to create a bot version
# that depends on an unbuilt locale.
AutoBuildBotLocales: false
BotLocales:
- LocaleId: en_US
Description: Find a photo based on description
NluConfidenceThreshold: 0.4
VoiceSettings:
VoiceId: Ivy
SlotTypes:
- Name: PhotoTypeValues
Description: Slot Type description
SlotTypeValues:
- SampleValue:
Value: person
- SampleValue:
Value: dog
- SampleValue:
Value: park
- SampleValue:
Value: bird
- SampleValue:
Value: tree
ValueSelectionSetting:
ResolutionStrategy: ORIGINAL_VALUE
Intents:
- Name: SearchIntent
Description: Intent to search for a photo
SampleUtterances:
- Utterance: Show me photos
- Utterance: Give me the photos
- Utterance: I want to search my photos
SlotPriorities:
- Priority: 1
SlotName: PhotoType
IntentConfirmationSetting:
PromptSpecification:
MessageGroupsList:
- Message:
PlainTextMessage:
Value: Okay, I am going to search for {PhotoType}, can you confirm?
MaxRetries: 3
AllowInterrupt: false
DeclinationResponse:
MessageGroupsList:
- Message:
PlainTextMessage:
Value: Okay, I have cancelled your search request.
AllowInterrupt: false
Slots:
- Name: PhotoType
Description: something
SlotTypeName: PhotoTypeValues
ValueElicitationSetting:
SlotConstraint: Required
PromptSpecification:
MessageGroupsList:
- Message:
PlainTextMessage:
Value: What do you want to look for?
MaxRetries: 3
AllowInterrupt: false
# We expect developers to provide the FallbackIntent when generating their bot.
# The service will throw an exception if it isn't provided.
- Name: FallbackIntent
Description: Default intent when no other intent matches
ParentIntentSignature: AMAZON.FallbackIntent
PhotoSearchBotVersionWithCFN:
DependsOn: PhotoSearchBot
Type: AWS::Lex::BotVersion
Properties:
BotId: !Ref PhotoSearchBot
BotVersionLocaleSpecification:
- LocaleId: en_US
BotVersionLocaleDetails:
SourceBotVersion: DRAFT
Description: BookTrip Version
# 4. We define the alias by providing the bot version created by the AWS::Lex::BotVersion resource above
FirstBotAliasWithCFN:
DependsOn: PhotoSearchBotVersionWithCFN
Type: AWS::Lex::BotAlias
Properties:
BotId: !Ref PhotoSearchBot
BotAliasName: PhotoSearchBotVersion1
# Remove BotAliasLocaleSettings if you aren't concerned with Lambda setup.
# If you are you can modify the LambdaArn below to get started.
# BotAliasLocaleSettings:
# - LocaleId: en_US
# BotAliasLocaleSetting:
# Enabled: false
# CodeHookSpecification:
# LambdaCodeHook:
# CodeHookInterfaceVersion: "1.0"
# LambdaArn: "arn:aws:lambda:us-east-1:111111111111:function:ReplaceWithYourOwnLambda"
BotVersion: !GetAtt PhotoSearchBotVersionWithCFN.BotVersion
SentimentAnalysisSettings:
DetectSentiment: true
LF2:
DependsOn: PhotoSearchBot
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt LF2IAMRole.Arn
Runtime: python3.12
FunctionName: search-photos
# Source code for lambda function
Environment:
Variables:
LEX_BOT_ID: !Ref PhotoSearchBot
LEX_ALIAS_ID: !Ref FirstBotAliasWithCFN
LEX_LOCALE_ID: en_US
OS_ENDPOINT: !GetAtt PhotosDomain.DomainEndpoint
OS_INDEX: "photos"
Code:
ZipFile: |
def main(event, context):
return "Hello World!"
Handler: index.main
Timeout: 15
PhotoGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: A3APIGateway
# /photos resource
PhotosResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref PhotoGateway
ParentId: !GetAtt PhotoGateway.RootResourceId
PathPart: photos
PhotosObjectResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref PhotoGateway
ParentId: !Ref PhotosResource
PathPart: '{object}'
# /search resource
SearchResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref PhotoGateway
ParentId: !GetAtt PhotoGateway.RootResourceId
PathPart: search
# PUT /photos (S3 proxy)
PutPhotosMethod:
Type: AWS::ApiGateway::Method
DependsOn:
- B2
Properties:
RestApiId: !Ref PhotoGateway
ResourceId: !Ref PhotosObjectResource
HttpMethod: PUT
AuthorizationType: NONE
ApiKeyRequired: false
RequestParameters:
method.request.header.x-amz-meta-customLabels: false
method.request.path.object: true # if using /photos/{object}
Integration:
Type: AWS
IntegrationHttpMethod: PUT
Uri: !Sub arn:aws:apigateway:${AWS::Region}:s3:path/${B2}/{object}
Credentials: !GetAtt ApiGatewayS3Role.Arn
RequestParameters:
integration.request.header.x-amz-meta-customLabels: method.request.header.x-amz-meta-customLabels
integration.request.path.object: method.request.path.object
PassthroughBehavior: WHEN_NO_MATCH
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,PUT'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,x-amz-meta-customLabels'"
ResponseTemplates:
application/json: ""
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Headers: true
PhotosObjectOptionsMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref PhotoGateway
ResourceId: !Ref PhotosObjectResource
HttpMethod: OPTIONS
AuthorizationType: NONE
Integration:
Type: MOCK
RequestTemplates:
application/json: '{"statusCode": 200}'
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,PUT'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,x-amz-meta-customLabels'"
ResponseTemplates:
application/json: ""
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Headers: true
SearchOptionsMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref PhotoGateway
ResourceId: !Ref SearchResource
HttpMethod: OPTIONS
AuthorizationType: NONE
Integration:
Type: MOCK
RequestTemplates:
application/json: '{"statusCode": 200}'
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'GET,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
ResponseTemplates:
application/json: ""
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Headers: true
# GET /search?q=...
GetSearchMethod:
Type: AWS::ApiGateway::Method
DependsOn:
- LF2
Properties:
RestApiId: !Ref PhotoGateway
ResourceId: !Ref SearchResource
HttpMethod: GET
AuthorizationType: NONE
ApiKeyRequired: false
RequestParameters:
method.request.querystring.q: false
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LF2.Arn}/invocations
# Allow API Gateway to invoke the search lambda
SearchPhotosLambdaPermissionForApiGateway:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LF2
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${PhotoGateway}/*/GET/search
# Deployment (depends on methods, but methods do NOT depend on deployment)
ApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- PutPhotosMethod
- PhotosObjectOptionsMethod
- GetSearchMethod
- SearchOptionsMethod
Properties:
RestApiId: !Ref PhotoGateway
StageName: Stage1
Outputs:
WebsiteURL:
Description: The URL of the front end.
Value: !GetAtt B1.WebsiteURL
S3BucketName:
Description: The name of the front end bucket.
Value: !Ref B1