2525 "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole" ,
2626 "arn:aws:iam::aws:policy/AmazonS3FullAccess" ,
2727 "arn:aws:iam::aws:policy/AmazonSQSFullAccess" ,
28- "arn:aws:iam::aws:policy/CloudWatchFullAccess"
28+ "arn:aws:iam::aws:policy/CloudWatchFullAccess" ,
2929]
3030
3131
@@ -54,10 +54,19 @@ def setup():
5454 PolicyArn = arn ,
5555 RoleName = "ecsInstanceRole" ,
5656 )
57- print ( ' Created ecsInstanceRole.' )
57+ print ( " Created ecsInstanceRole." )
5858 except iam .exceptions .EntityAlreadyExistsException :
59- print ('Skipping creation of ecsInstanceRole. Already exists.' )
60-
59+ print ("Skipping creation of ecsInstanceRole. Already exists." )
60+ try :
61+ iam .create_instance_profile (InstanceProfileName = "ecsInstanceRole" )
62+ except iam .exceptions .EntityAlreadyExistsException :
63+ print ("Skipping creation of ecsInstanceProfile. Already exists." )
64+ try :
65+ iam .add_role_to_instance_profile (
66+ InstanceProfileName = "ecsInstanceRole" , RoleName = "ecsInstanceRole"
67+ )
68+ except iam .exceptions .LimitExceededException :
69+ print ("Instance Profile already added to Instance Role" )
6170
6271 # Create EC2 Spot Fleet Tagging Role
6372 assume_role_policy_document = json .dumps (
@@ -82,9 +91,9 @@ def setup():
8291 PolicyArn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole" ,
8392 RoleName = "aws-ec2-spot-fleet-tagging-role" ,
8493 )
85- print ( ' Created aws-ec2-spot-fleet-tagging-role.' )
94+ print ( " Created aws-ec2-spot-fleet-tagging-role." )
8695 except iam .exceptions .EntityAlreadyExistsException :
87- print ( ' Skipping creation of aws-ec2-spot-fleet-tagging-role. Already exists.' )
96+ print ( " Skipping creation of aws-ec2-spot-fleet-tagging-role. Already exists." )
8897
8998 # Create Lambda Full Access Role
9099 assume_role_policy_document = json .dumps (
@@ -110,18 +119,18 @@ def setup():
110119 PolicyArn = arn ,
111120 RoleName = "LambdaFullAccess" ,
112121 )
113- print ( ' Created LambdaFullAccess role.' )
122+ print ( " Created LambdaFullAccess role." )
114123 except iam .exceptions .EntityAlreadyExistsException :
115- print ( ' Skipping creation of LambdaFullAccess role. Already exists.' )
116-
124+ print ( " Skipping creation of LambdaFullAccess role. Already exists." )
125+
117126 # Create SNS Monitor topic
118127 MonitorTopic = sns .create_topic (Name = "Monitor" )
119- print ( ' (Re-)Created Monitor SNS Topic.' )
128+ print ( " (Re-)Created Monitor SNS Topic." )
120129
121130 # Create Monitor Lambda function
122131 LambdaFullAccess = iam .get_role (RoleName = "LambdaFullAccess" )
123132
124- shutil .make_archive ("lambda_function" , ' zip' , os .getcwd ())
133+ shutil .make_archive ("lambda_function" , " zip" , os .getcwd ())
125134 fxn = open ("lambda_function.zip" , "rb" ).read ()
126135 try :
127136 MonitorFunction = lmbda .create_function (
@@ -139,30 +148,36 @@ def setup():
139148 PackageType = "Zip" ,
140149 TracingConfig = {"Mode" : "PassThrough" },
141150 Architectures = ["x86_64" ],
142- EphemeralStorage = {"Size" : 512 }
151+ EphemeralStorage = {"Size" : 512 },
143152 )
144153 # Subscribe Monitor Lambda to Monitor Topic
145154 sns .subscribe (
146155 TopicArn = MonitorTopic ["TopicArn" ],
147156 Protocol = "lambda" ,
148157 Endpoint = MonitorFunction ["FunctionArn" ],
149158 )
150- print ( ' Created Monitor Lambda Function.' )
159+ print ( " Created Monitor Lambda Function." )
151160 except lmbda .exceptions .ResourceConflictException :
152- print ( ' Skipping creation of Monitor Lambda Function. Already exists.' )
161+ print ( " Skipping creation of Monitor Lambda Function. Already exists." )
153162 try :
154163 lmbda .add_permission (
155- FunctionName = 'Monitor' ,
156- StatementId = 'InvokeBySNS' ,
157- Action = 'lambda:InvokeFunction' ,
158- Principal = 'sns.amazonaws.com' )
164+ FunctionName = "Monitor" ,
165+ StatementId = "InvokeBySNS" ,
166+ Action = "lambda:InvokeFunction" ,
167+ Principal = "sns.amazonaws.com" ,
168+ )
159169 except lmbda .exceptions .ResourceConflictException :
160- print ('Monitor Lambda Function already has SNS invoke permission.' )
170+ print ("Monitor Lambda Function already has SNS invoke permission." )
171+
161172
162173def destroy ():
163174 # Delete roles
164175 for arn in ecsInstanceRole_policy_list :
165176 iam .detach_role_policy (RoleName = "ecsInstanceRole" , PolicyArn = arn )
177+ iam .remove_role_from_instance_profile (
178+ InstanceProfileName = "ecsInstanceRole" , RoleName = "ecsInstanceRole"
179+ )
180+ iam .delete_instance_profile (InstanceProfileName = "ecsInstanceRole" )
166181 iam .delete_role (RoleName = "ecsInstanceRole" )
167182
168183 iam .detach_role_policy (
0 commit comments