@@ -41,15 +41,20 @@ def setup():
41
41
],
42
42
}
43
43
)
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 (
51
46
RoleName = "ecsInstanceRole" ,
47
+ AssumeRolePolicyDocument = assume_role_policy_document ,
52
48
)
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
+
53
58
54
59
# Create EC2 Spot Fleet Tagging Role
55
60
assume_role_policy_document = json .dumps (
@@ -65,14 +70,18 @@ def setup():
65
70
],
66
71
}
67
72
)
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.' )
76
85
77
86
# Create Lambda Full Access Role
78
87
assume_role_policy_document = json .dumps (
@@ -88,50 +97,55 @@ def setup():
88
97
],
89
98
}
90
99
)
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 (
98
102
RoleName = "LambdaFullAccess" ,
103
+ AssumeRolePolicyDocument = assume_role_policy_document ,
99
104
)
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
+
101
114
# Create SNS Monitor topic
102
115
MonitorTopic = sns .create_topic (Name = "Monitor" )
116
+ print ('(Re-)Created Monitor SNS Topic.' )
103
117
104
118
# Create Monitor Lambda function
105
119
LambdaFullAccess = iam .get_role (RoleName = "LambdaFullAccess" )
106
120
107
121
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.' )
135
149
136
150
def destroy ():
137
151
# Delete roles
0 commit comments