Lesson 2 of 3 AMIs and EC2 Image Builder

Automating AMI Builds with EC2 Image Builder Pipelines

This lesson explains how EC2 Image Builder automates building, testing and distributing AMIs and container images with a managed, repeatable pipeline.

Learning outcomes

After completing this lesson, you will be able to:

  • Assemble components, recipes and configurations.
  • Run and test pipelines.
  • Automate image retirement with lifecycle policies.

Image Builder components

An Image Builder setup is assembled from several resources. A component is a YAML document of build or test steps, such as installing packages, applying CIS hardening, or running validation commands; AWS provides managed components (for example update-linux and amazon-cloudwatch-agent-linux), and you can write your own. An image recipe combines a parent image (an AWS-managed base AMI, a Marketplace AMI or your own) with an ordered list of components and block device settings; a container recipe does the same for Docker images. An infrastructure configuration specifies the instance types, subnet, security group, instance profile (needing EC2InstanceProfileForImageBuilder and AmazonSSMManagedInstanceCore), S3 logging and SNS notifications. A distribution configuration names the target Regions and accounts, AMI name, launch permissions, KMS keys, launch template updates and SSM parameter updates, or the ECR repository for container images.

Match the pairs

Image Builder resources

Exam skills 3.1.1

Match each Image Builder resource with what it defines.

Log in to try this activity Interactive activities are free with an account.

Pipelines and testing

An image pipeline runs the recipe with the infrastructure and distribution configurations manually, on a cron schedule, or when the parent image has updates (only build if dependency updates are available). Image Builder launches a build instance, applies build components through Systems Manager, creates a snapshot image, launches a test instance and runs test components, and distributes the image only if the tests pass. Image scanning with Amazon Inspector can be enabled to report vulnerabilities in each image. Pipelines can also be defined with CloudFormation, and workflows customise the build and test stages.

The following diagram shows one pipeline run and its test gate.

An EC2 Image Builder pipeline run. A schedule or a parent image update starts the pipeline. A build instance applies the build components and a snapshot image is created; a test instance runs the test components, and the image is distributed only if the tests pass. Amazon Inspector scanning reports vulnerabilities in each image.
Distribution can also write the new AMI ID to a Parameter Store parameter for launch templates.
Put in order

A pipeline run

Exam skills 3.1.1

Put the stages of an Image Builder pipeline run in order.

Log in to try this activity Interactive activities are free with an account.

Lifecycle policies

Image Builder lifecycle policies automate retirement of images built by Image Builder: rules based on age or count deprecate, disable or delete AMIs and their snapshots, or delete container images, with exclusion rules for tagged images that must be kept. This prevents snapshot storage growing without limit as pipelines produce new images every week. The following table summarises the Image Builder resources.

ResourceDefines
ComponentBuild or test steps (YAML)
Image recipe / container recipeParent image plus ordered components
Infrastructure configurationInstance type, subnet, security group, instance profile, logging, SNS
Distribution configurationRegions, accounts, launch permissions, KMS keys, launch templates, SSM parameters, ECR
Image pipelineSchedule and the configurations above
Lifecycle policyDeprecate, disable or delete old images
Worked example

A monthly patched AMI

A team needs a CIS-hardened Amazon Linux 2023 AMI rebuilt after each monthly patch release and rolled out to Auto Scaling groups in two Regions. The recipe uses the AWS-managed AL2023 parent image with the update-linux, CIS hardening and CloudWatch agent components plus a custom test component that checks the application starts. The pipeline runs on a schedule, shares the AMI with the production accounts, copies it to the DR Region with the DR KMS key, updates the /golden/al2023/latest parameter and creates a new launch template version. A lifecycle policy keeps the last six images. An instance refresh then rolls the new AMI into each group.

Check yourself

What does an Image Builder recipe contain?

Show answer

A parent image and the components to apply to it.

Exam Tips

Remember for the exam

  • Recipe = parent image + components; infrastructure configuration = where the build runs; distribution = where the image goes.
  • Pipelines run on a schedule or when the parent image updates; tests gate distribution.
  • Build instances need the Image Builder and SSM instance profile policies and a route to SSM.
  • Distribution can update launch templates and SSM parameters automatically.
  • Lifecycle policies deprecate and delete old images and snapshots.