-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmfbot-trop.py
More file actions
351 lines (316 loc) · 11 KB
/
mfbot-trop.py
File metadata and controls
351 lines (316 loc) · 11 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
import troposphere
import troposphere.ec2 as ec2
import troposphere.elasticloadbalancing as elb
from troposphere import (
GetAZs,
Parameter,
Ref,
Region,
Select,
Split,
StackName,
Template,
Join,
GetAtt,
)
from troposphere.logs import (
LogGroup
)
from troposphere.iam import (
Role,
PolicyType
)
from troposphere.ecs import (
AwsvpcConfiguration,
Cluster,
ContainerDefinition,
Environment,
LogConfiguration,
Secret,
Service,
TaskDefinition,
NetworkConfiguration
)
BotDeploymentTemplate = Template()
BotDeploymentTemplate.set_description(
"""Cloudformation Stack for Deploying Metaflowbot"""
)
METADATA_SERVICE_URL = BotDeploymentTemplate.add_parameter(Parameter("MetadataServiceUrl",Type='String',\
Description="URL of the metadata service"))
ADMIN_USER_ADDRESS = BotDeploymentTemplate.add_parameter(Parameter(\
"AdminEmailAddress",
Type='String',\
Description="Email address of the admin user in the slack workspace"))
# Generate ARN from the s3 url and remove ARN parameter.
MFS3ROOTPATH = BotDeploymentTemplate.add_parameter(Parameter("MetaflowDatastoreSysrootS3",
Type='String',\
Description="Amazon S3 URL for Metaflow DataStore "))
MFS3ARN = Join('',['arn:aws:s3:::',Select(1,Split("s3://",Ref(MFS3ROOTPATH))),])
Metaflowbot_SECRETS_ARN = BotDeploymentTemplate.add_parameter(Parameter("MetaflowbotSecretsManagerARN",
Type='String',\
Description="ARN of the secret holding Metaflowbot credentials in Secrets Manager"))
# These are Parameter Store Secure secret names.
METADATA_AUTH = BotDeploymentTemplate.add_parameter(Parameter("MetadataServiceAuthParameterKey",
Type='String',\
Default="METADATASERVICE_AUTH_KEY",\
Description="Key for Metadata service auth parameter in Secrets Manager."))
SLACK_APP_TOKEN = BotDeploymentTemplate.add_parameter(Parameter("SlackAppTokenParameterKey",
Type='String',\
Default="SLACK_APP_TOKEN_KEY",\
Description="Key for SLACK_APP_TOKEN parameter in Secrets Manager."))
SLACK_BOT_TOKEN = BotDeploymentTemplate.add_parameter(Parameter("SlackBotTokenParameterKey",
Type='String',\
Default="SLACK_BOT_TOKEN_KEY",\
Description="Key for SLACK_BOT_TOKEN parameter in Secrets Manager."))
cluster = BotDeploymentTemplate.add_resource(Cluster("MetaflowbotCluster"))
ENV_DICT = {
"ADMIN_USER_ADDRESS":Ref(ADMIN_USER_ADDRESS),
"USERNAME":"slackbot",
"METAFLOW_SERVICE_URL":Ref(METADATA_SERVICE_URL),
"METAFLOW_DATASTORE_SYSROOT_S3":Ref(MFS3ROOTPATH),
"METAFLOW_DEFAULT_DATASTORE":"s3",
"METAFLOW_DEFAULT_METADATA":"service"
}
# best practices from : https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data-secrets.html
SECRETS = [
Secret(
Name='METAFLOW_SERVICE_AUTH_KEY',
ValueFrom=Join("",[Ref(Metaflowbot_SECRETS_ARN),":",Ref(METADATA_AUTH),"::"])
),
Secret(
Name='SLACK_APP_TOKEN',
ValueFrom=Join("",[Ref(Metaflowbot_SECRETS_ARN),":",Ref(SLACK_APP_TOKEN),"::"])
),Secret(
Name='SLACK_BOT_TOKEN',
ValueFrom=Join("",[Ref(Metaflowbot_SECRETS_ARN),":",Ref(SLACK_BOT_TOKEN),"::"])
)
]
# task role vs execution role :
# https://selfoverflow.com/questions/48999472/difference-between-aws-elastic-container-services-ecs-executionrole-and-taskr/49947471
# ECS execution role is capabilities of ECS agent
# ECS task role is specific capabilities within the task itself : s3_access_iam_role (capabilities of task)
EcsClusterRole = BotDeploymentTemplate.add_resource(
Role(
"EcsClusterRole",
Path="/",
ManagedPolicyArns=["arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"],
AssumeRolePolicyDocument={
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {"Service": "ecs-tasks.amazonaws.com"},
"Effect": "Allow",
},
],
},
)
)
EcsTaskRole = BotDeploymentTemplate.add_resource(
Role(
"EcsTaskRole",
Path="/",
AssumeRolePolicyDocument={
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {"Service": "ecs-tasks.amazonaws.com"},
"Effect": "Allow",
}
],
},
)
)
PolicyEcr = BotDeploymentTemplate.add_resource(
PolicyType(
"PolicyEcr",
PolicyName="MetaflowbotEcrPolicy",
PolicyDocument={
"Version": "2012-10-17",
"Statement": [
{
"Action": ["ecr:GetAuthorizationToken"],
"Resource": ["*"],
"Effect": "Allow",
},
{
"Action": [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": ["*"],
"Effect": "Allow",
"Sid": "AllowPull",
},
],
},
Roles=[Ref(EcsClusterRole)],
)
)
secrets_access_policy = BotDeploymentTemplate.add_resource(
PolicyType(
"MetaflowbotSecretAccess",
#
PolicyName='Metaflowbot',
PolicyDocument= {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"secretsmanager:GetSecretValue",
],
"Resource": [
Ref(Metaflowbot_SECRETS_ARN)
],
"Effect": "Allow",
"Sid": "S3GetObject",
},
]
},
Roles=[Ref(EcsClusterRole)],
)
)
S3AccessPolicy = BotDeploymentTemplate.add_resource(
PolicyType(
"S3AccessPolicy",
PolicyName="MetaflowbotS3AccessPolicy",
PolicyDocument={
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetObject",
# s3:ListBucket for https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
"s3:ListBucket",
],
"Resource": [
Join('',[MFS3ARN,'/*'])
],
"Effect": "Allow",
"Sid": "S3GetObject",
},
],
},
Roles=[Ref(EcsTaskRole)],
)
)
### Routing and VPC Settings.
# Create a VPC with Subnet. The VPC should have an IG attached
# and rule created in it's route table;
vpc = BotDeploymentTemplate.add_resource(ec2.VPC('MetaflowbotPublicVpc',CidrBlock="10.0.0.0/16",))
subnet = BotDeploymentTemplate.add_resource(ec2.Subnet(
"MetaflowbotDeploymentSubnet",
AvailabilityZone=Select(
0,GetAZs(region=Region)
),
CidrBlock="10.0.0.0/24",
VpcId=Ref(vpc),
MapPublicIpOnLaunch=True,
))
internetgateway = BotDeploymentTemplate.add_resource(ec2.InternetGateway("MetaflowbotInternetGateway"))
net_gw_vpc_attachment = BotDeploymentTemplate.add_resource(
ec2.VPCGatewayAttachment(
"InternetGatewayAttachment",
VpcId=Ref(vpc),
InternetGatewayId=Ref(internetgateway),
)
)
public_route_table = BotDeploymentTemplate.add_resource(
ec2.RouteTable(
"PublicRouteTable",
VpcId=Ref(vpc),
)
)
public_route_association = BotDeploymentTemplate.add_resource(
ec2.SubnetRouteTableAssociation(
"PublicRouteAssociation",
SubnetId=Ref(subnet),
RouteTableId=Ref(public_route_table),
)
)
default_public_route = BotDeploymentTemplate.add_resource(
ec2.Route(
"PublicDefaultRoute",
RouteTableId=Ref(public_route_table),
DestinationCidrBlock="0.0.0.0/0",
GatewayId=Ref(internetgateway),
)
)
LOG_GROUP_STRING = Join("",[
'/ecs/',
StackName,
"-metaflowbot",
])
loggroup= BotDeploymentTemplate.add_resource(
LogGroup(
"MetaflowbotLogGroup",
LogGroupName=LOG_GROUP_STRING
)
)
task_definition = BotDeploymentTemplate.add_resource(
TaskDefinition(
"MetaflowbotTaskDefinition",
RequiresCompatibilities=["FARGATE"],
Cpu="4096",
ExecutionRoleArn=GetAtt(EcsClusterRole,"Arn"),
TaskRoleArn=GetAtt(EcsTaskRole,"Arn"),
Memory="8192",
NetworkMode="awsvpc",
ContainerDefinitions=[
ContainerDefinition(
Name="metaflowbot",
Image="outerbounds/metaflowbot",
Essential=True,
LogConfiguration=LogConfiguration(
LogDriver = "awslogs",
Options= {
"awslogs-group": LOG_GROUP_STRING,
"awslogs-region":Region,
"awslogs-stream-prefix": 'ecs'
},
),
Environment = [
Environment(**dict(Name=k,Value=v)) for k,v in ENV_DICT.items()
],
Secrets=SECRETS
)
],
)
)
efs_security_group = BotDeploymentTemplate.add_resource(ec2.SecurityGroup(
"MetaflowbotSecurityGroup",
# Outbound rules
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group-rule.html
SecurityGroupEgress = [
ec2.SecurityGroupRule(
"MetaflowbotOutboundRules",
ToPort=65534,
FromPort=0,
IpProtocol="tcp",
CidrIp="0.0.0.0/0",
)
],
VpcId=Ref(vpc),
GroupDescription="Allow All In and outbound traffic",
))
service = BotDeploymentTemplate.add_resource(
Service(
"MetaflowbotDeployment",
Cluster=Ref(cluster),
DesiredCount=1,
TaskDefinition=Ref(task_definition),
LaunchType="FARGATE",
NetworkConfiguration=NetworkConfiguration(
AwsvpcConfiguration=AwsvpcConfiguration(
Subnets=[Ref(subnet)],
AssignPublicIp='ENABLED',
SecurityGroups=[Ref(efs_security_group)]
)
),
)
)
print(BotDeploymentTemplate.to_yaml())