@@ -64,6 +64,10 @@ def __init__(
6464 self .destination_account : str = self .config .active_config .get (
6565 "destination_account" , ""
6666 )
67+ self .destination_kms : str = self .config .active_config .get ("destination_kms" , "" )
68+ self .destination_role : str = self .config .active_config .get (
69+ "destination_role" , ""
70+ )
6771 self .target_machines : List [str ] = self .config .active_config .get (
6872 "machines" , ""
6973 ).split ("," )
@@ -124,6 +128,16 @@ def get_project_id(self, project_name: str = "") -> str:
124128 return ""
125129 return project_id
126130
131+ def _get_role_credentials (self , name : str , role : str ) -> Dict [str , Any ]:
132+ _sts_client : boto_client = boto3 .client ("sts" )
133+
134+ print (f"Assuming role: { role } " )
135+ assumed_role : Dict [str , Any ] = _sts_client .assume_role (
136+ RoleArn = self .destination_role , RoleSessionName = name
137+ )
138+
139+ return assumed_role .get ("Credentials" )
140+
127141 def check (self ) -> bool :
128142 """Check the status of machines in the provided project."""
129143 print (
@@ -291,6 +305,8 @@ def update_blueprint(self) -> bool:
291305 },
292306 {"key" : "MigrationWave" , "value" : self .migration_wave },
293307 {"key" : "DestinationAccount" , "value" : self .destination_account },
308+ {"key" : "DestinationKMS" , "value" : self .destination_kms },
309+ {"key" : "DestinationRole" , "value" : self .destination_role },
294310 ]
295311
296312 blueprint ["publicIPAction" ] = self .config .active_config .get (
@@ -627,25 +643,33 @@ def create_ami(self) -> Dict[str, str]:
627643 return amis
628644 return amis
629645
630- def copy_image (self , image_id : str , kms_id : str ) -> str :
646+ def copy_image (self , image_id : str ) -> str :
631647 """Copy a shared image to an account.
632648
633649 Args:
634650 image_id (str): The AWS AMI to be copied.
635- kms_id (str): The AWS KMS ID to be used for image encryption.
636651
637652 Returns:
638653 str: The copied AWS AMI ID.
639654
640655 """
641- _ec2_client : boto_client = boto3 .client ("ec2" , AWS_REGION )
656+ credentials = self ._get_role_credentials ("CopyImage" , self .destination_role )
657+
658+ _ec2_client : boto_client = boto3 .client (
659+ "ec2" ,
660+ region_name = AWS_REGION ,
661+ aws_access_key_id = credentials ["AccessKeyId" ],
662+ aws_secret_access_key = credentials ["SecretAccessKey" ],
663+ aws_session_token = credentials ["SessionToken" ],
664+ )
642665
666+ print (f"Copying image { image_id } " )
643667 new_image : Dict [str , Any ] = _ec2_client .copy_image (
644668 SourceImageId = image_id ,
645669 SourceRegion = AWS_REGION ,
646670 Name = f"copied-{ image_id } " ,
647671 Encrypted = True ,
648- KmsKeyId = kms_id ,
672+ KmsKeyId = self . destination_kms ,
649673 )
650674
651675 return new_image .get ("ImageId" , "" )
@@ -660,8 +684,18 @@ def split_image(self, image_id: str) -> Dict[str, Any]:
660684 dict: The mapping of AWS EBS block devices.
661685
662686 """
663- print ("Loading EC2 resource for region: " , AWS_REGION )
664- _ec2_res = boto3 .resource ("ec2" , AWS_REGION )
687+ print (
688+ f"Loading EC2 resource for region: { AWS_REGION } using role: { self .destination_role } "
689+ )
690+ credentials = self ._get_role_credentials ("SplitImage" , self .destination_role )
691+
692+ _ec2_res : boto_client = boto3 .resource (
693+ "ec2" ,
694+ region_name = AWS_REGION ,
695+ aws_access_key_id = credentials ["AccessKeyId" ],
696+ aws_secret_access_key = credentials ["SecretAccessKey" ],
697+ aws_session_token = credentials ["SessionToken" ],
698+ )
665699
666700 # Access the image that needs to be split
667701 image = _ec2_res .Image (image_id )
@@ -700,6 +734,9 @@ def split_image(self, image_id: str) -> Dict[str, Any]:
700734 Tags = [{"Key" : f"Drive-{ drive } " , "Value" : json .dumps (drives [drive ])}],
701735 )
702736
737+ # remove the old image
738+ image .deregister ()
739+
703740 return root_ami
704741
705742 def gen_terraform (
@@ -726,7 +763,15 @@ def gen_terraform(
726763 str: The raw Terraform with volume, ENI, and EC2 instance templates.
727764
728765 """
729- _ec2_res = boto3 .resource ("ec2" , AWS_REGION )
766+ credentials = self ._get_role_credentials ("GenTerraform" , self .destination_role )
767+
768+ _ec2_res : boto_client = boto3 .resource (
769+ "ec2" ,
770+ region_name = AWS_REGION ,
771+ aws_access_key_id = credentials ["AccessKeyId" ],
772+ aws_secret_access_key = credentials ["SecretAccessKey" ],
773+ aws_session_token = credentials ["SessionToken" ],
774+ )
730775
731776 # Access the image
732777 image : str = _ec2_res .Image (image_id )
0 commit comments