-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts_task_role.ts
188 lines (166 loc) · 5.15 KB
/
ts_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
/*
* Copyright 2023-2024 Amazon.com, Inc. or its affiliates.
*/
import { region_info } from "aws-cdk-lib";
import {
AccountPrincipal,
CompositePrincipal,
Effect,
IRole,
ManagedPolicy,
PolicyStatement,
Role,
ServicePrincipal
} from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";
import { OSMLAccount } from "../../osml_account";
import { TSDataplaneConfig } from "../ts_dataplane";
/**
* Represents the properties required to define a model runner ECS task role.
*
* @interface TSTaskRoleProps
*/
export interface TSTaskRoleProps {
/**
* 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 TSTaskRole construct.
*/
export class TSTaskRole extends Construct {
/**
* The AWS IAM role associated with this TSTaskRole.
*/
public role: IRole;
/**
* The AWS partition to be used for this TSTaskRole.
*/
public partition: string;
/**
* The TSDataplane Configuration class to be used for TSLambdaRole.
*/
public tsDataplaneConfig: TSDataplaneConfig = new TSDataplaneConfig();
/**
* Creates an TSTaskRole 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 {TSTaskRoleProps} props - The properties of this construct.
* @returns TSTaskRole - The TSTaskRole construct.
*/
constructor(scope: Construct, id: string, props: TSTaskRoleProps) {
super(scope, id);
// Defining constants for better readability
const DDB_JOB_TABLE_NAME = this.tsDataplaneConfig.DDB_JOB_TABLE;
const SQS_JOB_QUEUE_NAME = this.tsDataplaneConfig.SQS_JOB_QUEUE;
// Determine the AWS partition based on the provided AWS region
this.partition = region_info.Fact.find(
props.account.region,
region_info.FactName.PARTITION
)!;
// Create an AWS IAM role for the Model Runner Fargate ECS task
const tsTaskRole = new Role(this, "TSTaskRole", {
roleName: props.roleName,
assumedBy: new CompositePrincipal(
new ServicePrincipal("ecs-tasks.amazonaws.com"),
new ServicePrincipal("lambda.amazonaws.com")
),
description:
"Allows the OversightML Tile Server to access necessary AWS services (S3, SQS, DynamoDB, ...)"
});
const tsPolicy = new ManagedPolicy(this, "TSTaskPolicy", {
managedPolicyName: "TSTaskPolicy"
});
// dynamodb permissions
const ddbPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:*GetItem",
"dynamodb:*PutItem",
"dynamodb:UpdateItem",
"dynamodb:DescribeTable"
],
resources: [
`arn:${this.partition}:dynamodb:${props.account.region}:${props.account.id}:table/${DDB_JOB_TABLE_NAME}`
]
});
// sqs permissions
const sqsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"sqs:GetQueueUrl",
"sqs:SendMessage",
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:ListQueues"
],
resources: [
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_JOB_QUEUE_NAME}`,
`arn:${this.partition}:sqs:${props.account.region}:${props.account.id}:${SQS_JOB_QUEUE_NAME}DLQ`
]
});
// s3 permissions
const s3PolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"s3:GetBucketAcl",
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:PutObject"
],
resources: [`arn:${this.partition}:s3:::*`]
});
// Add permissions to assume roles
tsTaskRole.assumeRolePolicy?.addStatements(
new PolicyStatement({
actions: ["sts:AssumeRole"],
principals: [new AccountPrincipal(props.account.id)]
})
);
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 AWS Events
const eventsPolicyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["events:PutRule", "events:PutTargets", "events:DescribeRule"],
resources: [
`arn:${this.partition}:events:${props.account.region}:${props.account.id}:*`
]
});
tsPolicy.addStatements(
s3PolicyStatement,
kmsPolicyStatement,
eventsPolicyStatement,
sqsPolicyStatement,
ddbPolicyStatement,
stsPolicyStatement
);
tsTaskRole.addManagedPolicy(tsPolicy);
// Set the TSTaskRole property to the created role
this.role = tsTaskRole;
}
}