-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_task_role.ts
315 lines (288 loc) · 11.1 KB
/
mr_task_role.ts
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
/*
* Copyright 2023-2024 Amazon.com, Inc. or its affiliates.
*/
import { region_info } from "aws-cdk-lib";
import {
CompositePrincipal,
Effect,
IRole,
ManagedPolicy,
PolicyStatement,
Role,
ServicePrincipal
} from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";
import { OSMLAccount } from "../../osml_account";
import { MRDataplaneConfig } from "../mr_dataplane";
import { MRSyncConfig } from "../testing/mr_sync";
/**
* Represents the properties required to define a model runner ECS task role.
*
* @interface MRTaskRoleProps
*/
export interface MRTaskRoleProps {
/**
* The OSML (OversightML) deployment account associated with this role.
*
* @type {OSMLAccount}
*/
account: OSMLAccount;
/**
* The name to give to the role.
*
* @type {string}
*/
roleName: string;
}
/**
* Represents an MRTaskRole construct.
*/
export class MRTaskRole extends Construct {
/**
* The AWS IAM role associated with this MRTaskRole.
*/
public role: IRole;
/**
* The AWS partition to be used for this MRTaskRole.
*/
public partition: string;
/**
* The Model Runner Dataplane Configuration values to be used for this MRTaskRole
*/
public mrDataplaneConfig: MRDataplaneConfig = new MRDataplaneConfig();
/**
* The Model Runner Sync Configuration values to be used for this MRTaskRole
*/
public mrSyncConfig: MRSyncConfig = new MRSyncConfig();
/**
* Creates an MRTaskRole construct.
* @param {Construct} scope - The scope/stack in which to define this construct.
* @param {string} id - The id of this construct within the current scope.
* @param {MRTaskRoleProps} props - The properties of this construct.
* @returns MRTaskRole - The MRTaskRole construct.
*/
constructor(scope: Construct, id: string, props: MRTaskRoleProps) {
super(scope, id);
// Determine the AWS partition based on the provided AWS region
this.partition = region_info.Fact.find(
props.account.region,
region_info.FactName.PARTITION
)!;
// Defining constants for better readability
const SQS_IMAGE_REQUEST_QUEUE_NAME =
this.mrDataplaneConfig.SQS_IMAGE_REQUEST_QUEUE;
const SQS_REGION_REQUEST_QUEUE_NAME =
this.mrDataplaneConfig.SQS_REGION_REQUEST_QUEUE;
const SQS_IMAGE_STATUS_QUEUE_NAME =
this.mrDataplaneConfig.SQS_IMAGE_STATUS_QUEUE;
const SQS_REGION_STATUS_QUEUE_NAME =
this.mrDataplaneConfig.SQS_REGION_STATUS_QUEUE;
const SNS_TOPIC_IMAGE_NAME = this.mrDataplaneConfig.SNS_IMAGE_STATUS_TOPIC;
const SNS_TOPIC_REGION_NAME =
this.mrDataplaneConfig.SNS_REGION_STATUS_TOPIC;
const ECS_CLUSTER_NAME = this.mrDataplaneConfig.MR_CLUSTER_NAME;
const DDB_JOB_STATUS_TABLE_NAME =
this.mrDataplaneConfig.DDB_JOB_STATUS_TABLE;
const DDB_FEATURES_TABLE_NAME = this.mrDataplaneConfig.DDB_FEATURES_TABLE;
const DDB_ENDPOINT_PROCESSING_TABLE_NAME =
this.mrDataplaneConfig.DDB_ENDPOINT_PROCESSING_TABLE;
const DDB_REGION_REQUEST_TABLE_NAME =
this.mrDataplaneConfig.DDB_REGION_REQUEST_TABLE;
const MR_FIRELENS_LOG_GROUP_NAME = `/aws/${this.mrDataplaneConfig.METRICS_NAMESPACE}/MRFireLens`;
const MR_SERVICE_LOG_GROUP_NAME = `/aws/${this.mrDataplaneConfig.METRICS_NAMESPACE}/MRService`;
const MR_HTTPENDPOINT_LOG_GROUP_NAME = `/aws/${this.mrDataplaneConfig.METRICS_NAMESPACE}/HTTPEndpoint`;
// Create an AWS IAM role for the Model Runner Fargate ECS task
const mrTaskRole = new Role(this, "MRTaskRole", {
roleName: props.roleName,
assumedBy: new CompositePrincipal(
new ServicePrincipal("ecs-tasks.amazonaws.com"),
new ServicePrincipal("lambda.amazonaws.com")
),
description:
"Allows the Oversight Model Runner to access necessary AWS services (S3, SQS, DynamoDB, ...)"
});
const mrPolicy = new ManagedPolicy(this, "MRTaskPolicy", {
managedPolicyName: "MRTaskPolicy"
});
// Add permissions to assume roles
const stsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["sts:AssumeRole"],
resources: ["*"]
});
// Add permissions for AWS Key Management Service (KMS)
const kmsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["kms:Decrypt", "kms:GenerateDataKey", "kms:Encrypt"],
resources: [
`arn:${this.partition}:kms:${props.account.region}:${props.account.id}:key/*`
]
});
// Add permissions for Amazon Kinesis
const kinesisPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"kinesis:PutRecord",
"kinesis:PutRecords",
"kinesis:DescribeStream"
],
resources: [
`arn:${this.partition}:kinesis:${props.account.region}:${props.account.id}:stream/*`
]
});
// Add permissions to describe EC2 instance types
const ec2PolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ec2:DescribeInstanceTypes", "ec2:DescribeSubnets"],
resources: ["*"]
});
// Add permissions for SQS permissions
const sqsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"sqs:DeleteMessage",
"sqs:ListQueues",
"sqs:GetQueueUrl",
"sqs:ReceiveMessage",
"sqs:SendMessage",
"sqs:GetQueueAttributes"
],
resources: [
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_IMAGE_REQUEST_QUEUE_NAME}`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_REGION_REQUEST_QUEUE_NAME}`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_IMAGE_STATUS_QUEUE_NAME}`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_REGION_STATUS_QUEUE_NAME}`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_IMAGE_REQUEST_QUEUE_NAME}DLQ`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_REGION_REQUEST_QUEUE_NAME}DLQ`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_IMAGE_STATUS_QUEUE_NAME}DLQ`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_REGION_STATUS_QUEUE_NAME}DLQ`
]
});
// Add permissions for S3 permissions
const s3PolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"s3:GetBucketAcl",
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:PutObject",
"s3:DeleteObject"
],
resources: [`arn:${this.partition}:s3:::*`]
});
// Add permissions for SNS permissions
const snsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["sns:Publish"],
resources: [
`arn:${this.partition}:sns:${props.account.region}:${props.account.id}:${SNS_TOPIC_IMAGE_NAME}`,
`arn:${this.partition}:sns:${props.account.region}:${props.account.id}:${SNS_TOPIC_REGION_NAME}`
]
});
// Add permissions for dynamodb permissions
const ddbPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"dynamodb:BatchGetItem",
"dynamodb:BatchWriteItem",
"dynamodb:PutItem",
"dynamodb:ListTables",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem",
"dynamodb:UpdateTable"
],
resources: [
`arn:${this.partition}:dynamodb:${props.account.region}:${props.account.id}:table/${DDB_JOB_STATUS_TABLE_NAME}`,
`arn:${this.partition}:dynamodb:${props.account.region}:${props.account.id}:table/${DDB_FEATURES_TABLE_NAME}`,
`arn:${this.partition}:dynamodb:${props.account.region}:${props.account.id}:table/${DDB_ENDPOINT_PROCESSING_TABLE_NAME}`,
`arn:${this.partition}:dynamodb:${props.account.region}:${props.account.id}:table/${DDB_REGION_REQUEST_TABLE_NAME}`
]
});
// Add permission for autoscaling ECS permissions
const autoScalingEcsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ecs:DescribeServices", "ecs:UpdateService"],
resources: [
`arn:${this.partition}:ecs:${props.account.region}:${props.account.id}:cluster/${ECS_CLUSTER_NAME}`,
`arn:${this.partition}:ecs:${props.account.region}:${props.account.id}:service/${ECS_CLUSTER_NAME}/*`
]
});
// Add permission for CW ECS permissions
const cwPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:DescribeLogStreams",
"logs:DescribeLogGroups",
"logs:CreateLogStream",
"logs:CreateLogGroup"
],
resources: [
`arn:${this.partition}:logs:${props.account.region}:${props.account.id}:log-group:${MR_FIRELENS_LOG_GROUP_NAME}:*`,
`arn:${this.partition}:logs:${props.account.region}:${props.account.id}:log-group:${MR_SERVICE_LOG_GROUP_NAME}:*`,
`arn:${this.partition}:logs:${props.account.region}:${props.account.id}:log-group:${MR_HTTPENDPOINT_LOG_GROUP_NAME}:*`,
`arn:${this.partition}:logs:${props.account.region}:${props.account.id}:log-group:/aws/sagemaker/Endpoints/*`
]
});
// Add permission for autoscaling CW permissions
const autoScalingCwPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["cloudwatch:DescribeAlarms"],
resources: [`*`]
});
// Add permissions for SageMaker permissions
const sagemakerPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"sagemaker:ListEndpointConfigs",
"sagemaker:DescribeEndpointConfig",
"sagemaker:UpdateEndpoint",
"sagemaker:InvokeEndpoint",
"sagemaker:DescribeEndpoint",
"sagemaker:ListEndpoints",
"sagemaker:InvokeEndpointAsync",
"sagemaker:DescribeModel",
"sagemaker:ListModels",
"sagemaker:DescribeModelPackage",
"sagemaker:DescribeModelPackageGroup",
"sagemaker:BatchDescribeModelPackage",
"sagemaker:ListModelMetadata",
"sagemaker:DeleteEndpoint",
"sagemaker:CreateModel",
"sagemaker:CreateEndpoint",
"sagemaker:CreateEndpointConfig",
"sagemaker:BatchGetRecord",
"sagemaker:DeleteEndpointConfig",
"sagemaker:UpdateEndpoint",
"sagemaker:BatchGetMetrics",
"sagemaker:BatchPutMetrics"
],
resources: [
`arn:${this.partition}:sagemaker:${props.account.region}:${props.account.id}:*`
]
});
mrPolicy.addStatements(
stsPolicyStatement,
kmsPolicyStatement,
sagemakerPolicyStatement,
s3PolicyStatement,
ec2PolicyStatement,
kinesisPolicyStatement,
snsPolicyStatement,
sqsPolicyStatement,
ddbPolicyStatement,
autoScalingEcsPolicyStatement,
autoScalingCwPolicyStatement,
cwPolicyStatement
);
mrTaskRole.addManagedPolicy(mrPolicy);
// Set the MRTaskRole property to the created role
this.role = mrTaskRole;
}
}