-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
379 lines (362 loc) · 11.1 KB
/
Copy pathtemplate.yaml
File metadata and controls
379 lines (362 loc) · 11.1 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Bedrock Model Evaluation Tool
Parameters:
LogRetentionInDays:
Type: Number
Default: 14
Owner:
Type: String
Project:
Type: String
Default: genai-inference-evaluation-tool
CodeSendSESSourceEmailAddress:
Type: String
Globals:
Function:
Timeout: 30
MemorySize: 256
CodeUri: backend/
Tags:
Owner: !Ref Owner
Project: !Ref Project
Architectures:
- x86_64
HttpApi:
CorsConfiguration:
AllowOrigins:
- '*'
AllowMethods:
- GET
- POST
- PUT
- DELETE
- OPTIONS
AllowHeaders:
- '*'
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: infrastructure/network.yaml
Parameters:
ProjectName: !Ref AWS::StackName
PreprocessingStack:
Type: AWS::CloudFormation::Stack
DependsOn:
- DatasetBucket
Properties:
TemplateURL: infrastructure/preprocessing.yaml
Parameters:
LogRetentionInDays: !Ref LogRetentionInDays
Owner: !Ref Owner
Project: !Ref Project
DatasetBucketName: !Ref DatasetBucket
RootStackName: !Ref AWS::StackName
ApiStack:
Type: AWS::CloudFormation::Stack
DependsOn:
[
DatasetBucket,
EvaluationJobsTable,
AccessCodesTable,
EvaluationCluster,
EvaluationTaskDefinition,
FargateExecutionRole,
FargateTaskRole,
PreprocessingStack,
]
Properties:
TemplateURL: infrastructure/api.yaml
Parameters:
LogRetentionInDays: !Ref LogRetentionInDays
Owner: !Ref Owner
Project: !Ref Project
DatasetBucketName: !Ref DatasetBucket
EvaluationJobsTableName: !Ref EvaluationJobsTable
AccessCodesTableName: !Ref AccessCodesTable
EvaluationClusterName: !Ref EvaluationCluster
EvaluationTaskDefinitionArn: !Ref EvaluationTaskDefinition
PublicSubnetId: !GetAtt NetworkStack.Outputs.PublicSubnetId
FargateSecurityGroupId: !GetAtt NetworkStack.Outputs.FargateSecurityGroupId
FargateExecutionRoleArn: !GetAtt FargateExecutionRole.Arn
FargateTaskRoleArn: !GetAtt FargateTaskRole.Arn
SesFromAddress: !Ref CodeSendSESSourceEmailAddress
PreprocessingStateMachineArn: !GetAtt PreprocessingStack.Outputs.PreprocessingStateMachineArn
PreprocessingStateMachineName: !GetAtt PreprocessingStack.Outputs.PreprocessingStateMachineName
EvaluationEngineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub '/ecs/${AWS::StackName}-evaluation-engine'
RetentionInDays: !Ref LogRetentionInDays
# S3 Buckets
DatasetBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'bedrock-eval-datasets-${Owner}-${AWS::AccountId}-${AWS::Region}'
Tags:
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub '${AWS::StackName}-dataset-bucket'
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
CorsConfiguration:
CorsRules:
- AllowedOrigins:
- '*'
AllowedMethods:
- GET
- PUT
- POST
AllowedHeaders:
- '*'
MaxAge: 3600
FrontendBucket:
Type: AWS::S3::Bucket
Properties:
Tags:
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub '${AWS::StackName}-frontend-bucket'
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
FrontendBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref FrontendBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: cloudfront.amazonaws.com
Action: s3:GetObject
Resource: !Sub '${FrontendBucket.Arn}/*'
Condition:
StringEquals:
'AWS:SourceArn': !Sub 'arn:aws:cloudfront::${AWS::AccountId}:distribution/${CloudFrontDistribution}'
- Effect: Deny
Action: 's3:*'
Principal: '*'
Resource:
- !GetAtt FrontendBucket.Arn
- !Sub '${FrontendBucket.Arn}/*'
Condition:
Bool:
'aws:SecureTransport': false
# DynamoDB Table
EvaluationJobsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${AWS::StackName}-evaluation-jobs'
BillingMode: PAY_PER_REQUEST
Tags:
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub '${AWS::StackName}-evaluation-jobs-table'
SSESpecification:
SSEEnabled: true
AttributeDefinitions:
- AttributeName: evaluation_id
AttributeType: S
- AttributeName: dataset_id
AttributeType: S
KeySchema:
- AttributeName: evaluation_id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: dataset_id-index
KeySchema:
- AttributeName: dataset_id
KeyType: HASH
Projection:
ProjectionType: ALL
AccessCodesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${AWS::StackName}-access-codes'
BillingMode: PAY_PER_REQUEST
Tags:
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub '${AWS::StackName}-access-codes-table'
AttributeDefinitions:
- AttributeName: email
AttributeType: S
KeySchema:
- AttributeName: email
KeyType: HASH
# ECS Cluster
EvaluationCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Sub '${AWS::StackName}-evaluation-cluster'
Tags:
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub '${AWS::StackName}-evaluation-cluster'
# IAM Roles
FargateExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
FargateTaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: EvaluationEnginePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource: !Sub '${DatasetBucket.Arn}/*'
- Effect: Allow
Action:
- s3:ListBucket
Resource: !GetAtt DatasetBucket.Arn
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource: !GetAtt EvaluationJobsTable.Arn
- Effect: Allow
Action:
- bedrock:InvokeModel
- bedrock:InvokeModelWithResponseStream
Resource: '*'
# Fargate Task Definition
EvaluationTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Sub '${AWS::StackName}-evaluation-engine'
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Cpu: '2048'
Memory: '4096'
ExecutionRoleArn: !GetAtt FargateExecutionRole.Arn
TaskRoleArn: !GetAtt FargateTaskRole.Arn
ContainerDefinitions:
- Name: evaluation-engine
Image: !Sub '${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/genai-evaluation:latest'
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref EvaluationEngineLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: evaluation-engine
Environment:
- Name: DATASET_BUCKET
Value: !Ref DatasetBucket
- Name: DYNAMODB_TABLE
Value: !Ref EvaluationJobsTable
- Name: EVALUATION_TIMEOUT_MINUTES
Value: '30'
- Name: AWS_REGION
Value: !Ref AWS::Region
# CloudFront
CloudFrontOAC:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Name: !Sub '${AWS::StackName}-oac'
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
DefaultRootObject: index.html
Origins:
- Id: S3Origin
DomainName: !GetAtt FrontendBucket.RegionalDomainName
OriginAccessControlId: !Ref CloudFrontOAC
S3OriginConfig:
OriginAccessIdentity: ''
DefaultCacheBehavior:
TargetOriginId: S3Origin
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
Outputs:
ApiUrl:
Description: API Gateway endpoint URL
Value: !GetAtt ApiStack.Outputs.ApiUrl
CloudFrontUrl:
Description: CloudFront distribution URL
Value: !Sub 'https://${CloudFrontDistribution.DomainName}'
CloudFrontDistributionId:
Description: CloudFront distribution ID
Value: !Ref CloudFrontDistribution
FrontendBucketName:
Description: S3 bucket for frontend assets
Value: !Ref FrontendBucket
DatasetBucketName:
Description: S3 bucket for dataset storage
Value: !Ref DatasetBucket
EvaluationJobsTableName:
Description: DynamoDB table for evaluation jobs
Value: !Ref EvaluationJobsTable
AccessCodesTableName:
Description: DynamoDB table for access codes
Value: !Ref AccessCodesTable
EvaluationClusterName:
Description: ECS cluster for evaluation tasks
Value: !Ref EvaluationCluster
EvaluationTaskDefinitionArn:
Description: Fargate task definition ARN
Value: !Ref EvaluationTaskDefinition