-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
335 lines (310 loc) · 9.5 KB
/
template.yaml
File metadata and controls
335 lines (310 loc) · 9.5 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Demo GenAI app
SAM Template for sample genai App using Prompt management and prompt flow
Globals:
Function:
Timeout: 200
Tracing: Active
LoggingConfig:
LogFormat: JSON
Api:
TracingEnabled: true
Parameters:
modelId:
Type: String
Default: "anthropic.claude-3-haiku-20240307-v1:0"
pythonruntime:
Type: String
Default: "python3.12"
startPrompt:
Type: String
Default: "You are a helpful assistant. Answer the following questions based on the provided input: <description>{{description}}</description>"
Resources:
# The API Gateway that will receive
genaiappAPI:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
MethodSettings:
- HttpMethod: "*"
ResourcePath: "/*"
genaiappFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
# Links to the Prompt Flow and Alias
Environment:
Variables:
FLOW_ID: !GetAtt PromptgenaiappFlow.Id
FLOW_ALIAS_ID: !GetAtt PromptgenaiappFlowAlias.Id
CodeUri: lambdabedrock/
Handler: app.lambda_handler
Runtime: !Ref pythonruntime
Architectures:
- x86_64
MemorySize: 128
Policies:
- Statement:
- Sid: CallBedrockFlow
Effect: Allow
Action:
- bedrock:InvokeFlow
- bedrock:GetFlowAlias
Resource: !GetAtt PromptgenaiappFlowAlias.Arn
Events:
genaiappAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
RestApiId: !Ref genaiappAPI
Path: /api
Method: post
ApplicationResourceGroup:
Type: AWS::ResourceGroups::Group
Properties:
Name:
Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
ResourceQuery:
Type: CLOUDFORMATION_STACK_1_0
ApplicationInsightsMonitoring:
Type: AWS::ApplicationInsights::Application
Properties:
ResourceGroupName:
Ref: ApplicationResourceGroup
AutoConfigurationEnabled: 'true'
# an S3 bucket to store the static objects
genaiappStorageBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join ["-", ["genaiapp-storage", !Select [2, !Split ["/", !Ref "AWS::StackId"]]]]
# a cloudfront AOI to access my bucket
OriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Ref AWS::StackName
genaiappStorageBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref genaiappStorageBucket
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:GetObject
Principal:
CanonicalUser: !GetAtt OriginAccessIdentity.S3CanonicalUserId
Resource: !Sub 'arn:aws:s3:::${genaiappStorageBucket}/*'
- Effect: Deny
Action: "*"
Condition:
Bool:
'aws:SecureTransport': false
Principal: "*"
Resource: !Sub 'arn:aws:s3:::${genaiappStorageBucket}/*'
Sid: DenyUnencryptedConnections
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: false
TargetOriginId: the-s3-bucket
ViewerProtocolPolicy: redirect-to-https
#Cache behavior for the api gateway onn api/
CacheBehaviors:
- AllowedMethods:
- GET
- HEAD
- POST
- OPTIONS
- PUT
- DELETE
- PATCH
Compress: true
DefaultTTL: 0
ForwardedValues:
QueryString: false
MaxTTL: 300
MinTTL: 0
PathPattern: "/api*"
TargetOriginId: the-api-gateway
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
Enabled: true
HttpVersion: http2
Origins:
- DomainName: !Sub "${genaiappStorageBucket}.s3.amazonaws.com"
Id: the-s3-bucket
S3OriginConfig:
OriginAccessIdentity:
!Sub "origin-access-identity/cloudfront/${OriginAccessIdentity}"
# the api gateway origin
- DomainName: !Sub "${genaiappAPI}.execute-api.${AWS::Region}.amazonaws.com"
OriginPath: /Prod
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
Id: the-api-gateway
Promptgenaiapp:
Type: AWS::Bedrock::Prompt
Properties:
Name:
Fn::Join:
- "-"
- - "Promptgenaiapp"
- !Select
- 2
- !Split
- "/"
- !Ref AWS::StackId
DefaultVariant: DefaultVariant
Variants:
- Name: DefaultVariant
InferenceConfiguration:
Text:
MaxTokens: 1024
StopSequences: []
Temperature: 0.1
TopP: 1
TopK: 250
ModelId: !Ref modelId
TemplateType: TEXT
TemplateConfiguration:
Text:
Text: !Ref startPrompt
InputVariables:
- Name: description
PromptgenaiappVersion:
Type: AWS::Bedrock::PromptVersion
Properties:
PromptArn: !Ref Promptgenaiapp
PromptExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: bedrock.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: BedrockPromptExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "bedrock:InvokeModel"
Resource: !Join
- ''
- - "arn:aws:bedrock:*::foundation-model/"
- !Ref modelId
- Effect: Allow
Action:
- "bedrock:GetPrompt"
Resource: !GetAtt Promptgenaiapp.Arn
PromptgenaiappFlow:
Type: AWS::Bedrock::Flow
Properties:
Name:
Fn::Join:
- "-"
- - "PromptgenaiappFlow"
- !Select
- 2
- !Split
- "/"
- !Ref AWS::StackId
ExecutionRoleArn: !GetAtt PromptExecutionRole.Arn
Definition:
Nodes:
- Type: Input
Name: FlowInputNode
Outputs:
- Name: document
Type: String
- Type: Output
Name: FlowOutputNode
Inputs:
- Name: document
Type: String
Expression: $.data
- Type: Prompt
Name: Promptgenaiapp
Configuration:
Prompt:
SourceConfiguration:
Resource:
PromptArn: !GetAtt Promptgenaiapp.Arn
Inputs:
- Name: description
Type: String
Expression: $.data
Outputs:
- Name: modelCompletion
Type: String
Connections:
- Configuration:
Data:
SourceOutput: document
TargetInput: description
Name: InputToPtompt
Source: FlowInputNode
Target: Promptgenaiapp
Type: Data
- Configuration:
Data:
SourceOutput: modelCompletion
TargetInput: document
Name: OutputToFlow
Source: Promptgenaiapp
Target: FlowOutputNode
Type: Data
DependsOn:
- PromptgenaiappVersion
PromptgenaiappFlowVersion:
Type: AWS::Bedrock::FlowVersion
Properties:
FlowArn: !GetAtt PromptgenaiappFlow.Arn
PromptgenaiappFlowAlias:
Type: AWS::Bedrock::FlowAlias
Properties:
Name: PromptgenaiappFlowAlias
FlowArn: !GetAtt PromptgenaiappFlow.Arn
Description: "Prompt genaiapp Flow Alias"
RoutingConfiguration:
- FlowVersion: !GetAtt PromptgenaiappFlowVersion.Version
BedrockPromptExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: bedrock.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: BedrockPromptExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "bedrock:*"
Resource: "*"
Outputs:
CloudfrontURL:
Description: "the cloudfront url"
Value: !Sub "https://${CloudFrontDistribution.DomainName}"
S3BucketName:
Description: "the s3 bucket name"
Value: !Ref genaiappStorageBucket
PromptFlow:
Description: "the prompt flow arn"
Value: !Ref PromptgenaiappFlow