-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
287 lines (284 loc) · 10.2 KB
/
template.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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Transform: AWS::Serverless-2016-10-31
Resources:
MvpStoriesApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub
- ${ResourceName} From Stack ${AWS::StackName}
- ResourceName: MvpStoriesApi
StageName: Prod
DefinitionBody:
openapi: '3.0'
info: {}
paths:
/:
post:
responses:
'200':
description: default OK response
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
x-amazon-apigateway-integration:
credentials: !GetAtt MvpStoriesApiToMvpStoriesWorkflowRole.Arn
httpMethod: POST
type: aws
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:states:action/StartSyncExecution
responses:
default:
statusCode: 200
responseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'*'"
method.response.header.Access-Control-Allow-Headers: "'*'"
responseTemplates:
application/json: |
#set($inputRoot = $input.path('$'))
#set($output = $util.parseJson($inputRoot.output))
{
"mp3Url": "$output.mp3Url"
}
requestTemplates:
application/json: !Sub |-
{
"input": "$util.escapeJavaScript($input.json('$'))",
"name": "$context.requestId",
"stateMachineArn": "${MvpStoriesWorkflow}"
}
EndpointConfiguration: REGIONAL
TracingEnabled: true
Cors:
AllowOrigin: '''*'''
AllowHeaders: '''*'''
AllowMethods: '''*'''
MaxAge: 5
MvpStoriesWorkflow:
Type: AWS::Serverless::StateMachine
Properties:
Definition:
StartAt: Generate Story
States:
Generate Story:
Type: Task
Resource: arn:aws:states:::bedrock:invokeModel
Parameters:
ModelId: arn:aws:bedrock:us-west-2::foundation-model/ai21.j2-mid-v1
Body:
prompt.$: States.Format('Generate an approx 100-120 words long bedtime story about {}', $.context)
maxTokens: 300
OutputPath: $.Body.completions[0].data.text
Next: Translate ?
Translate ?:
Type: Choice
Choices:
- Not:
Variable: $$.Execution.Input.language
StringEquals: en
Comment: Needs translation
Next: Translate Story
Default: Convert Story to Speech
Translate Story:
Type: Task
Parameters:
SourceLanguageCode: en
TargetLanguageCode.$: $$.Execution.Input.language
Text.$: $
Resource: arn:aws:states:::aws-sdk:translate:translateText
Next: Convert Story to Speech
OutputPath: $.TranslatedText
Convert Story to Speech:
Type: Task
Resource: arn:aws:states:::lambda:invoke
OutputPath: $.Payload
Parameters:
FunctionName: ${MvpStoriesTextToSpeechArn}
Payload:
story.$: $
language.$: $$.Execution.Input.language
Retry:
- ErrorEquals:
- Lambda.ServiceException
- Lambda.AWSLambdaException
- Lambda.SdkClientException
- Lambda.TooManyRequestsException
IntervalSeconds: 1
MaxAttempts: 3
BackoffRate: 2
End: true
Logging:
Level: ALL
IncludeExecutionData: true
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt MvpStoriesWorkflowLogGroup.Arn
Policies:
- AWSXrayWriteOnlyAccess
- Statement:
- Effect: Allow
Action:
- logs:CreateLogDelivery
- logs:GetLogDelivery
- logs:UpdateLogDelivery
- logs:DeleteLogDelivery
- logs:ListLogDeliveries
- logs:PutResourcePolicy
- logs:DescribeResourcePolicies
- logs:DescribeLogGroups
Resource: '*'
- Statement:
- Effect: Allow
Action:
- translate:*
Resource:
- '*'
- Statement:
- Effect: Allow
Action: bedrock:InvokeModel
Resource: 'arn:aws:bedrock:*::foundation-model/ai21.j2-mid-v1'
- LambdaInvokePolicy:
FunctionName: !Ref MvpStoriesTextToSpeech
Tracing:
Enabled: true
Type: EXPRESS
DefinitionSubstitutions:
MvpStoriesTextToSpeechArn: !GetAtt MvpStoriesTextToSpeech.Arn
Events:
MvpStoriesApiPOST:
Type: Api
Properties:
Path: /
Method: POST
RestApiId: !Ref MvpStoriesApi
MvpStoriesWorkflowLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub
- /aws/vendedlogs/states/${AWS::StackName}-${ResourceId}-Logs
- ResourceId: MvpStoriesWorkflow
MvpStoriesApiToMvpStoriesWorkflowRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: !Sub apigateway.${AWS::URLSuffix}
Policies:
- PolicyName: ExecuteStateMachine
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: states:StartSyncExecution
Resource: !GetAtt MvpStoriesWorkflow.Arn
MvpStoriesTextToSpeech:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: MvpStoriesTextToSpeech
CodeUri: src/handlers
Handler: textToSpeech.handler
Runtime: nodejs20.x
MemorySize: 3008
Timeout: 30
Tracing: Active
Policies:
- Statement:
- Effect: Allow
Action:
- polly:*
Resource:
- '*'
- Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectAcl
- s3:GetObjectLegalHold
- s3:GetObjectRetention
- s3:GetObjectTorrent
- s3:GetObjectVersion
- s3:GetObjectVersionAcl
- s3:GetObjectVersionForReplication
- s3:GetObjectVersionTorrent
- s3:ListBucket
- s3:ListBucketMultipartUploads
- s3:ListBucketVersions
- s3:ListMultipartUploadParts
- s3:AbortMultipartUpload
- s3:DeleteObject
- s3:DeleteObjectVersion
- s3:PutObject
- s3:PutObjectLegalHold
- s3:PutObjectRetention
- s3:RestoreObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${MvpStoriesBucket}
- !Sub arn:${AWS::Partition}:s3:::${MvpStoriesBucket}/*
Environment:
Variables:
MVPSTORIESBUCKET_BUCKET_NAME: !Ref MvpStoriesBucket
MVPSTORIESBUCKET_BUCKET_ARN: !GetAtt MvpStoriesBucket.Arn
MvpStoriesTextToSpeechLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${MvpStoriesTextToSpeech}
MvpStoriesBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-mvpstorie-${AWS::AccountId}
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
KMSMasterKeyID: alias/aws/s3
PublicAccessBlockConfiguration:
IgnorePublicAcls: true
RestrictPublicBuckets: true
MvpStoriesBucketBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref MvpStoriesBucket
PolicyDocument:
Id: RequireEncryptionInTransit
Version: '2012-10-17'
Statement:
- Principal: '*'
Action: '*'
Effect: Deny
Resource:
- !GetAtt MvpStoriesBucket.Arn
- !Sub ${MvpStoriesBucket.Arn}/*
Condition:
Bool:
aws:SecureTransport: 'false'
Outputs:
MvpStoriesApi:
Description: "Amazon API Gateway endpoint URL for Prod stage"
Value: !Sub "https://${MvpStoriesApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"