Skip to content

Commit 48b8e93

Browse files
committed
handle existing infrastructure
1 parent ccd65e3 commit 48b8e93

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

setup_AWS.py

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ def setup():
4141
],
4242
}
4343
)
44-
iam.create_role(
45-
RoleName="ecsInstanceRole",
46-
AssumeRolePolicyDocument=assume_role_policy_document,
47-
)
48-
for arn in ecsInstanceRole_policy_list:
49-
iam.attach_role_policy(
50-
PolicyArn=arn,
44+
try:
45+
iam.create_role(
5146
RoleName="ecsInstanceRole",
47+
AssumeRolePolicyDocument=assume_role_policy_document,
5248
)
49+
for arn in ecsInstanceRole_policy_list:
50+
iam.attach_role_policy(
51+
PolicyArn=arn,
52+
RoleName="ecsInstanceRole",
53+
)
54+
print ('Created ecsInstanceRole.')
55+
except iam.exceptions.EntityAlreadyExistsException:
56+
print ('Skipping creation of ecsInstanceRole. Already exists.')
57+
5358

5459
# Create EC2 Spot Fleet Tagging Role
5560
assume_role_policy_document = json.dumps(
@@ -65,14 +70,18 @@ def setup():
6570
],
6671
}
6772
)
68-
iam.create_role(
69-
RoleName="aws-ec2-spot-fleet-tagging-role",
70-
AssumeRolePolicyDocument=assume_role_policy_document,
71-
)
72-
iam.attach_role_policy(
73-
PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole",
74-
RoleName="aws-ec2-spot-fleet-tagging-role",
75-
)
73+
try:
74+
iam.create_role(
75+
RoleName="aws-ec2-spot-fleet-tagging-role",
76+
AssumeRolePolicyDocument=assume_role_policy_document,
77+
)
78+
iam.attach_role_policy(
79+
PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole",
80+
RoleName="aws-ec2-spot-fleet-tagging-role",
81+
)
82+
print ('Created aws-ec2-spot-fleet-tagging-role.')
83+
except iam.exceptions.EntityAlreadyExistsException:
84+
print ('Skipping creation of aws-ec2-spot-fleet-tagging-role. Already exists.')
7685

7786
# Create Lambda Full Access Role
7887
assume_role_policy_document = json.dumps(
@@ -88,50 +97,55 @@ def setup():
8897
],
8998
}
9099
)
91-
iam.create_role(
92-
RoleName="LambdaFullAccess",
93-
AssumeRolePolicyDocument=assume_role_policy_document,
94-
)
95-
for arn in LambdaFullAccess_policy_list:
96-
iam.attach_role_policy(
97-
PolicyArn=arn,
100+
try:
101+
iam.create_role(
98102
RoleName="LambdaFullAccess",
103+
AssumeRolePolicyDocument=assume_role_policy_document,
99104
)
100-
105+
for arn in LambdaFullAccess_policy_list:
106+
iam.attach_role_policy(
107+
PolicyArn=arn,
108+
RoleName="LambdaFullAccess",
109+
)
110+
print ('Created LambdaFullAccess role.')
111+
except iam.exceptions.EntityAlreadyExistsException:
112+
print ('Skipping creation of LambdaFullAccess role. Already exists.')
113+
101114
# Create SNS Monitor topic
102115
MonitorTopic = sns.create_topic(Name="Monitor")
116+
print ('(Re-)Created Monitor SNS Topic.')
103117

104118
# Create Monitor Lambda function
105119
LambdaFullAccess = iam.get_role(RoleName="LambdaFullAccess")
106120

107121
fxn = open("lambda_function.zip", "rb").read()
108-
109-
MonitorFunction = lmbda.create_function(
110-
FunctionName="Monitor",
111-
Runtime="python3.9",
112-
Role=LambdaFullAccess["Role"]["Arn"],
113-
Handler="lambda_function.lambda_handler",
114-
Code={
115-
"ZipFile": fxn,
116-
},
117-
Description="Auto-monitor DS runs",
118-
Timeout=900,
119-
MemorySize=3008,
120-
Publish=True,
121-
PackageType="Zip",
122-
TracingConfig={"Mode": "PassThrough"},
123-
Architectures=["x86_64"],
124-
EphemeralStorage={"Size": 512},
125-
SnapStart={"ApplyOn": "None"},
126-
)
127-
128-
# Subscribe Monitor Lambda to Monitor Topic
129-
sns.subscribe(
130-
TopicArn=MonitorTopic["TopicArn"],
131-
Protocol="lambda",
132-
Endpoint=MonitorFunction["FunctionArn"],
133-
)
134-
122+
try:
123+
MonitorFunction = lmbda.create_function(
124+
FunctionName="Monitor",
125+
Runtime="python3.9",
126+
Role=LambdaFullAccess["Role"]["Arn"],
127+
Handler="lambda_function.lambda_handler",
128+
Code={
129+
"ZipFile": fxn,
130+
},
131+
Description="Auto-monitor DS runs",
132+
Timeout=900,
133+
MemorySize=3008,
134+
Publish=True,
135+
PackageType="Zip",
136+
TracingConfig={"Mode": "PassThrough"},
137+
Architectures=["x86_64"],
138+
EphemeralStorage={"Size": 512}
139+
)
140+
# Subscribe Monitor Lambda to Monitor Topic
141+
sns.subscribe(
142+
TopicArn=MonitorTopic["TopicArn"],
143+
Protocol="lambda",
144+
Endpoint=MonitorFunction["FunctionArn"],
145+
)
146+
print ('Created Monitor Lambda Function.')
147+
except lmbda.exceptions.ResourceConflictException:
148+
print ('Skipping creation of Monitor Lambda Function. Already exists.')
135149

136150
def destroy():
137151
# Delete roles

0 commit comments

Comments
 (0)