ImageBuilder: Image (CfnImage) do not clean generated AMI on destroy #23360
-
I previously open the issue #21431 The proposed solution by @peterwoodworth did not solve the issue
Here is an example code : class CfnAmi(Stack):
def __init__(self, scope: Construct, id: str,
**kwargs) -> None:
super().__init__(scope, id,
description="Test: ami ",
**kwargs)
stack=id+"-"
workingDir="/tmp"
# Ec2 builder role and instance profile
role = iam.Role(self, "amiBuilderRole",
assumed_by=iam.ServicePrincipal("ec2.amazonaws.com"),
)
role.add_managed_policy(iam.ManagedPolicy.from_aws_managed_policy_name("AmazonSSMManagedInstanceCore"))
role.add_managed_policy(iam.ManagedPolicy.from_aws_managed_policy_name("EC2InstanceProfileForImageBuilder"))
insProfileName=f"{stack}InstanceProfile"
instanceProfile = iam.CfnInstanceProfile(self, "MyCfnInstanceProfile",
roles=[role.role_name],
instance_profile_name=insProfileName,
)
# Image Builder Infrastructure configuration
self.infra = imagebuilder.CfnInfrastructureConfiguration(self, "Infra",
instance_profile_name=instanceProfile.instance_profile_name,
name=id+"InfraConfig",
# the properties below are optional
instance_types=["t2.micro"],
)
# Force the dependencies. For some reason it fails to do it itself !!!
# Using instanceProfile.instance_profile_name is not enough for some reason ...
self.infra.add_depends_on(instanceProfile)
self.recipe = imagebuilder.CfnImageRecipe(self, "MyCfnImageRecipe",
components=[{"componentArn":"arn:aws:imagebuilder:ap-southeast-2:aws:component/aws-cli-version-2-linux/x.x.x"}],
name=stack+"ImageRecipeUniq",
parent_image="arn:aws:imagebuilder:ap-southeast-2:aws:image/ubuntu-server-20-lts-x86/x.x.x",
version="1.0.0",
working_directory=workingDir,
block_device_mappings=[imagebuilder.CfnImageRecipe.InstanceBlockDeviceMappingProperty(
device_name="/dev/sda1",
ebs=imagebuilder.CfnImageRecipe.EbsInstanceBlockDeviceSpecificationProperty(
volume_size=8,
volume_type="gp2"
),
)],
)
# Construct responsible to triggering image build
cfn_image = imagebuilder.CfnImage(self, "MyCfnImage",
infrastructure_configuration_arn=self.infra.attr_arn,
# the properties below are optional
enhanced_image_metadata_enabled=False,
image_recipe_arn=self.recipe.attr_arn,
image_tests_configuration=imagebuilder.CfnImage.ImageTestsConfigurationProperty(
image_tests_enabled=False,
timeout_minutes=60
),
)
cfn_image.cfn_options.deletion_policy = cdk.CfnDeletionPolicy.DELETE
CfnOutput(self,"ami id",value=cfn_image.attr_image_id) Once deployed, an AMI will be available (after about 30min). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Can you share the section of the template which defines the Image please? |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Can you share the section of the template which defines the Image please?