18
18
from fabric import Connection
19
19
from retrying import retry
20
20
from time_utils import minutes , seconds
21
- from utils import random_alphanumeric
21
+ from utils import get_arn_partition , random_alphanumeric
22
22
23
23
SnapshotConfig = namedtuple ("ClusterConfig" , ["ssh_key" , "key_name" , "vpc_id" , "head_node_subnet_id" ])
24
24
@@ -98,7 +98,7 @@ def _create_volume_process(self, region, snapshot_config):
98
98
99
99
# Create a new volume and attach to the instance
100
100
self .volume = self ._create_volume (subnet )
101
- self .instance = self ._launch_instance (ami_id , subnet )
101
+ self .instance = self ._launch_instance (ami_id , subnet , region )
102
102
self ._attach_volume ()
103
103
# Open ssh connection
104
104
self .ssh_conn = self ._open_ssh_connection ()
@@ -212,7 +212,7 @@ def _get_security_group_id(self):
212
212
213
213
return security_group_id
214
214
215
- def _create_snapshot_instance_profile (self ):
215
+ def _create_snapshot_instance_profile (self , region ):
216
216
iam_resources_suffix = random_alphanumeric ()
217
217
snapshot_instance_role_name = f"SnapshotInstanceRole-{ iam_resources_suffix } "
218
218
snapshot_instance_profile_name = f"SnapshotInstanceProfile-{ iam_resources_suffix } "
@@ -230,7 +230,8 @@ def _create_snapshot_instance_profile(self):
230
230
)
231
231
if not self ._snapshot_instance_profile :
232
232
self .iam .attach_role_policy (
233
- RoleName = snapshot_instance_role_name , PolicyArn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
233
+ RoleName = snapshot_instance_role_name ,
234
+ PolicyArn = f"arn:{ get_arn_partition (region )} :iam::aws:policy/AmazonSSMManagedInstanceCore" ,
234
235
)
235
236
logging .info ("Creating profile (%s)." , snapshot_instance_profile_name )
236
237
self ._snapshot_instance_profile = self .iam .create_instance_profile (
@@ -243,8 +244,8 @@ def _create_snapshot_instance_profile(self):
243
244
InstanceProfileName = snapshot_instance_profile_name , RoleName = snapshot_instance_role_name
244
245
)
245
246
246
- def _launch_instance (self , ami_id , subnet ):
247
- self ._create_snapshot_instance_profile ()
247
+ def _launch_instance (self , ami_id , subnet , region ):
248
+ self ._create_snapshot_instance_profile (region )
248
249
instance = retry (stop_max_attempt_number = 5 , wait_fixed = minutes (1 ))(self .ec2_resource .create_instances )(
249
250
ImageId = ami_id ,
250
251
KeyName = self .config .key_name ,
@@ -290,10 +291,10 @@ def _get_amazonlinux2_ami(self):
290
291
amis = sorted (response ["Images" ], key = lambda x : x ["CreationDate" ], reverse = True )
291
292
return amis [0 ]["ImageId" ]
292
293
293
- def release_all (self ):
294
+ def release_all (self , region ):
294
295
"""Release all resources"""
295
296
self ._release_instance ()
296
- self ._release_instance_iam ()
297
+ self ._release_instance_iam (region )
297
298
self ._release_security_group ()
298
299
self ._release_volume ()
299
300
self ._release_snapshot ()
@@ -317,13 +318,14 @@ def _release_instance(self):
317
318
318
319
self .instance = None
319
320
320
- def _release_instance_iam (self ):
321
+ def _release_instance_iam (self , region ):
321
322
instance_profile_name = self ._snapshot_instance_profile ["InstanceProfile" ]["InstanceProfileName" ]
322
323
instance_role_name = self ._snapshot_instance_role ["Role" ]["RoleName" ]
323
324
if self ._snapshot_instance_role :
324
325
role_name = self ._snapshot_instance_role ["Role" ]["RoleName" ]
325
326
self .iam .detach_role_policy (
326
- RoleName = instance_role_name , PolicyArn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
327
+ RoleName = instance_role_name ,
328
+ PolicyArn = f"arn:{ get_arn_partition (region )} :iam::aws:policy/AmazonSSMManagedInstanceCore" ,
327
329
)
328
330
logging .info ("Deleting role: %s" , role_name )
329
331
self .iam .remove_role_from_instance_profile (
0 commit comments