1414
1515def create (profile_name , stack_file , stack_name , secret , slack_webhook_url ,
1616 s3_bucket , s3_prefix , only_errors , from_pipeline = False ):
17+
1718 aws_region = None
1819 aws_account = None
1920 stacks = None
@@ -108,14 +109,20 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
108109 print (exc )
109110 exit (1 )
110111
112+ sts = boto3 .client ('sts' )
113+ iam_user = sts .get_caller_identity ()['Arn' ]
114+
111115 # Verify integrity of stack
112116 stacks = None
117+ aws_role_arn = None
113118 try :
114119 with open (stack_file , 'r' ) as stack_stream :
115120 stacks = yaml .safe_load (stack_stream )
116121 aws_region = stacks [stack_name ]['Region' ]
117122 aws_account = stacks [stack_name ]['Account' ]
118123 stack_template = stacks [stack_name ]['Template' ]
124+ if 'AssumedRoleArn' in stacks [stack_name ]:
125+ aws_role_arn = stacks [stack_name ]['AssumedRoleArn' ]
119126 except yaml .YAMLError as exc :
120127 print (exc )
121128 alert (stack_name ,
@@ -124,14 +131,52 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
124131 aws_region , aws_account , action , profile_name , slack_webhook_url )
125132 exit (1 )
126133
134+ # Set role
135+ role_session = None
136+ if aws_role_arn is not None :
137+ try :
138+ if profile_name is not None :
139+ boto3 .setup_default_session (profile_name = profile_name )
140+
141+ # Get current timestamp
142+ current_date = datetime .utcnow ().isoformat ()
143+ current_date = current_date .replace (':' , '-' ).replace ('.' , '-' )
144+
145+ # Assume role
146+ sts_client = boto3 .client ('sts' )
147+ assumed_role_object = sts_client .assume_role (
148+ RoleArn = aws_role_arn ,
149+ RoleSessionName = "AssumeRoleSession-{}" .format (current_date )
150+ )
151+ credentials = assumed_role_object ['Credentials' ]
152+ role_session = boto3 .Session (
153+ aws_access_key_id = credentials ['AccessKeyId' ],
154+ aws_secret_access_key = credentials ['SecretAccessKey' ],
155+ aws_session_token = credentials ['SessionToken' ]
156+ )
157+ iam_user = aws_role_arn
158+ except Exception as exc :
159+ print (exc )
160+ alert (stack_name ,
161+ "Unable to assume role {} for stack {}. Exception = {}" .format (
162+ aws_role_arn , stack_name , exc ),
163+ aws_region , aws_account , action , profile_name , slack_webhook_url )
164+ exit (1 )
165+
127166 # Connect to AWS CloudFormation
128- cf_client = boto3 .client ('cloudformation' , region_name = aws_region )
167+ cf_client = boto3 .client ('cloudformation' , region_name = aws_region ) if role_session is None else \
168+ role_session .client ('cloudformation' , region_name = aws_region )
169+
170+ # Create AWS S3 Resource
171+ s3_session = boto3 .resource ('s3' , region_name = aws_region ) if role_session is None else \
172+ role_session .resource ('s3' , region_name = aws_region )
129173
130174 # Get parameter and tags from stack
131175 stack_parameters , stack_tags , stack_template_url = result (stack_file , stack_name , secret ,
132176 s3_bucket , s3_prefix ,
133177 aws_region , aws_account ,
134- action , profile_name , slack_webhook_url )
178+ action , profile_name , slack_webhook_url ,
179+ s3_session )
135180
136181 # Check if stack exists and if it is in "ROLLBACK_COMPLETE" status, delete it
137182 try :
@@ -147,7 +192,7 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
147192
148193 except Exception as exc :
149194 print (exc )
150- alert (stack_name , exc , aws_region , aws_account , action , profile_name , slack_webhook_url )
195+ alert (stack_name , exc , aws_region , aws_account , action , profile_name , slack_webhook_url , iam_user )
151196 exit (1 )
152197 except botocore .exceptions .ClientError as exc :
153198 if "Stack with id {} does not exist" .format (stack_name ) in str (exc ):
@@ -156,19 +201,20 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
156201 print (exc )
157202 alert (stack_name ,
158203 "Unable to check if stack already exists. Exception = {}" .format (exc ),
159- aws_region , aws_account , action , profile_name , slack_webhook_url )
204+ aws_region , aws_account , action , profile_name , slack_webhook_url , iam_user )
160205 exit (1 )
161206 except Exception as exc :
207+ print ("aici crapa ?!" )
162208 print (exc )
163209 alert (stack_name ,
164210 "Unable to check if stack already exists. Exception = {}" .format (exc ),
165- aws_region , aws_account , action , profile_name , slack_webhook_url )
211+ aws_region , aws_account , action , profile_name , slack_webhook_url , iam_user )
166212 exit (1 )
167213
168214 # Create the stack
169215 if not only_errors :
170216 print ("CREATE_STARTED for stack {}" .format (stack_name ))
171- alert (stack_name , None , aws_region , aws_account , "CREATE_STARTED" , profile_name , slack_webhook_url )
217+ alert (stack_name , None , aws_region , aws_account , "CREATE_STARTED" , profile_name , slack_webhook_url , iam_user )
172218 waiter = None
173219 try :
174220 if stack_template_url ['type' ] == 'TemplateURL' :
@@ -197,7 +243,7 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
197243 print (exc )
198244 alert (stack_name ,
199245 "Unable to start stack creation process. Exception = {}" .format (exc ),
200- aws_region , aws_account , action , profile_name , slack_webhook_url )
246+ aws_region , aws_account , action , profile_name , slack_webhook_url , iam_user )
201247 exit (1 )
202248
203249 # Waiting for stack to finish creating
@@ -225,11 +271,11 @@ def create(profile_name, stack_file, stack_name, secret, slack_webhook_url,
225271 "CREATE_FAILED" ,
226272 "\n " .join (failure_reasons )
227273 ),
228- aws_region , aws_account , action , profile_name , slack_webhook_url )
274+ aws_region , aws_account , action , profile_name , slack_webhook_url , iam_user )
229275 exit (1 )
230276
231277 if not only_errors :
232278 print ("CREATE_COMPLETE for stack {}" .format (stack_name ))
233- alert (stack_name , None , aws_region , aws_account , "CREATE_COMPLETE" , profile_name , slack_webhook_url )
279+ alert (stack_name , None , aws_region , aws_account , "CREATE_COMPLETE" , profile_name , slack_webhook_url , iam_user )
234280
235281 return True
0 commit comments