generated from DistributedScience/Distributed-CellProfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_AWS.py
192 lines (175 loc) · 6.53 KB
/
setup_AWS.py
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
import sys
import boto3
import json
iam = boto3.client("iam")
sns = boto3.client("sns")
lmbda = boto3.client("lambda")
ecsInstanceRole_policy_list = [
"arn:aws:iam::aws:policy/AmazonS3FullAccess",
"arn:aws:iam::aws:policy/CloudWatchFullAccess",
"arn:aws:iam::aws:policy/CloudWatchActionsEC2Access",
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole",
]
LambdaFullAccess_policy_list = [
"arn:aws:iam::aws:policy/AWSLambda_FullAccess",
"arn:aws:iam::aws:policy/AmazonSNSFullAccess",
"arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole",
"arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole",
"arn:aws:iam::aws:policy/AWSLambdaExecute",
"arn:aws:iam::aws:policy/AmazonECS_FullAccess",
"arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole",
"arn:aws:iam::aws:policy/AmazonS3FullAccess",
"arn:aws:iam::aws:policy/AmazonSQSFullAccess",
"arn:aws:iam::aws:policy/CloudWatchFullAccess"
]
def setup():
# Create ECS Instance Role
assume_role_policy_document = json.dumps(
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole",
}
],
}
)
try:
iam.create_role(
RoleName="ecsInstanceRole",
AssumeRolePolicyDocument=assume_role_policy_document,
)
for arn in ecsInstanceRole_policy_list:
iam.attach_role_policy(
PolicyArn=arn,
RoleName="ecsInstanceRole",
)
print ('Created ecsInstanceRole.')
except iam.exceptions.EntityAlreadyExistsException:
print ('Skipping creation of ecsInstanceRole. Already exists.')
# Create EC2 Spot Fleet Tagging Role
assume_role_policy_document = json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"Service": "spotfleet.amazonaws.com"},
"Action": "sts:AssumeRole",
}
],
}
)
try:
iam.create_role(
RoleName="aws-ec2-spot-fleet-tagging-role",
AssumeRolePolicyDocument=assume_role_policy_document,
)
iam.attach_role_policy(
PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole",
RoleName="aws-ec2-spot-fleet-tagging-role",
)
print ('Created aws-ec2-spot-fleet-tagging-role.')
except iam.exceptions.EntityAlreadyExistsException:
print ('Skipping creation of aws-ec2-spot-fleet-tagging-role. Already exists.')
# Create Lambda Full Access Role
assume_role_policy_document = json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole",
}
],
}
)
try:
iam.create_role(
RoleName="LambdaFullAccess",
AssumeRolePolicyDocument=assume_role_policy_document,
)
for arn in LambdaFullAccess_policy_list:
iam.attach_role_policy(
PolicyArn=arn,
RoleName="LambdaFullAccess",
)
print ('Created LambdaFullAccess role.')
except iam.exceptions.EntityAlreadyExistsException:
print ('Skipping creation of LambdaFullAccess role. Already exists.')
# Create SNS Monitor topic
MonitorTopic = sns.create_topic(Name="Monitor")
print ('(Re-)Created Monitor SNS Topic.')
# Create Monitor Lambda function
LambdaFullAccess = iam.get_role(RoleName="LambdaFullAccess")
fxn = open("lambda_function.zip", "rb").read()
try:
MonitorFunction = lmbda.create_function(
FunctionName="Monitor",
Runtime="python3.9",
Role=LambdaFullAccess["Role"]["Arn"],
Handler="lambda_function.lambda_handler",
Code={
"ZipFile": fxn,
},
Description="Auto-monitor DS runs",
Timeout=900,
MemorySize=3008,
Publish=True,
PackageType="Zip",
TracingConfig={"Mode": "PassThrough"},
Architectures=["x86_64"],
EphemeralStorage={"Size": 512}
)
# Subscribe Monitor Lambda to Monitor Topic
sns.subscribe(
TopicArn=MonitorTopic["TopicArn"],
Protocol="lambda",
Endpoint=MonitorFunction["FunctionArn"],
)
print ('Created Monitor Lambda Function.')
except lmbda.exceptions.ResourceConflictException:
print ('Skipping creation of Monitor Lambda Function. Already exists.')
try:
lmbda.add_permission(
FunctionName='Monitor',
StatementId='InvokeBySNS',
Action='lambda:InvokeFunction',
Principal='sns.amazonaws.com')
except lmbda.exceptions.ResourceConflictException:
print ('Monitor Lambda Function already has SNS invoke permission.')
def destroy():
# Delete roles
for arn in ecsInstanceRole_policy_list:
iam.detach_role_policy(RoleName="ecsInstanceRole", PolicyArn=arn)
iam.delete_role(RoleName="ecsInstanceRole")
iam.detach_role_policy(
RoleName="aws-ec2-spot-fleet-tagging-role",
PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole",
)
iam.delete_role(RoleName="aws-ec2-spot-fleet-tagging-role")
for arn in LambdaFullAccess_policy_list:
iam.detach_role_policy(RoleName="LambdaFullAccess", PolicyArn=arn)
iam.delete_role(RoleName="LambdaFullAccess")
# Delete Monitor Lambda function
lmbda.delete_function(FunctionName="Monitor")
# Delete Monitor SNS topic
# create_topic is idempotent so we use it to return ARN since topic already exists
MonitorTopic = sns.create_topic(Name="Monitor")
sns.delete_topic(TopicArn=MonitorTopic["TopicArn"])
if __name__ == "__main__":
if len(sys.argv) == 1:
setup()
else:
if sys.argv[1] == "destroy":
destroy()
else:
print("Use: setup_AWS.py or setup_AWS.py destroy")
sys.exit()