Package software.amazon.awscdk.services.imagebuilder.alpha


@Stability(Experimental) package software.amazon.awscdk.services.imagebuilder.alpha

EC2 Image Builder Construct Library

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


This module is part of the AWS Cloud Development Kit project.

README

Amazon EC2 Image Builder is a fully managed AWS service that helps you automate the creation, management, and deployment of customized, secure, and up-to-date server images. You can use Image Builder to create Amazon Machine Images (AMIs) and container images for use across AWS Regions.

This module is part of the AWS Cloud Development Kit project. It allows you to define Image Builder pipelines, images, recipes, components, workflows, and lifecycle policies. A component defines the sequence of steps required to customize an instance during image creation (build component) or test an instance launched from the created image (test component). Components are created from declarative YAML or JSON documents that describe runtime configuration for building, validating, or testing instances. Components are included when added to the image recipe or container recipe for an image build.

EC2 Image Builder supports AWS-managed components for common tasks, AWS Marketplace components, and custom components that you create. Components run during specific workflow phases: build and validate phases during the build stage, and test phase during the test stage.

Infrastructure Configuration

Infrastructure configuration defines the compute resources and environment settings used during the image building process. This includes instance types, IAM instance profile, VPC settings, subnets, security groups, SNS topics for notifications, logging configuration, and troubleshooting settings like whether to terminate instances on failure or keep them running for debugging. These settings are applied to builds when included in an image or an image pipeline.

 InfrastructureConfiguration infrastructureConfiguration = InfrastructureConfiguration.Builder.create(this, "InfrastructureConfiguration")
         .infrastructureConfigurationName("test-infrastructure-configuration")
         .description("An Infrastructure Configuration")
         // Optional - instance types to use for build/test
         .instanceTypes(List.of(InstanceType.of(InstanceClass.STANDARD7_INTEL, InstanceSize.LARGE), InstanceType.of(InstanceClass.BURSTABLE3, InstanceSize.LARGE)))
         // Optional - create an instance profile with necessary permissions
         .instanceProfile(InstanceProfile.Builder.create(this, "InstanceProfile")
                 .instanceProfileName("test-instance-profile")
                 .role(Role.Builder.create(this, "InstanceProfileRole")
                         .assumedBy(ServicePrincipal.fromStaticServicePrincipleName("ec2.amazonaws.com"))
                         .managedPolicies(List.of(ManagedPolicy.fromAwsManagedPolicyName("AmazonSSMManagedInstanceCore"), ManagedPolicy.fromAwsManagedPolicyName("EC2InstanceProfileForImageBuilder")))
                         .build())
                 .build())
         // Use VPC network configuration
         .vpc(vpc)
         .subnetSelection(SubnetSelection.builder().subnetType(SubnetType.PUBLIC).build())
         .securityGroups(List.of(SecurityGroup.fromSecurityGroupId(this, "SecurityGroup", vpc.getVpcDefaultSecurityGroup())))
         .keyPair(KeyPair.fromKeyPairName(this, "KeyPair", "imagebuilder-instance-key-pair"))
         .terminateInstanceOnFailure(true)
         // Optional - IMDSv2 settings
         .httpTokens(HttpTokens.REQUIRED)
         .httpPutResponseHopLimit(1)
         // Optional - publish image completion messages to an SNS topic
         .notificationTopic(Topic.fromTopicArn(this, "Topic", this.formatArn(ArnComponents.builder().service("sns").resource("image-builder-topic").build())))
         // Optional - log settings. Logging is enabled by default
         .logging(InfrastructureConfigurationLogging.builder()
                 .s3Bucket(Bucket.fromBucketName(this, "LogBucket", String.format("imagebuilder-logging-%s", Aws.ACCOUNT_ID)))
                 .s3KeyPrefix("imagebuilder-logs")
                 .build())
         // Optional - host placement settings
         .ec2InstanceAvailabilityZone(Stack.of(this).getAvailabilityZones()[0])
         .ec2InstanceHostId(dedicatedHost.getAttrHostId())
         .ec2InstanceTenancy(Tenancy.HOST)
         .resourceTags(Map.of(
                 "Environment", "production"))
         .build();