Amazon Elastic Compute Cloud (EC2)
EC2 Real Life Problems and Solutions
Demo EC2 image builder
Learn how to automate AMI creation with EC2 Image Builder. In this guide, we'll set up a pipeline that uses Amazon Linux 2 as a base, installs Nginx, runs validation and reboot tests, and outputs a ready-to-use Nginx AMI.
Prerequisites
- An AWS account with Image Builder, EC2, and IAM permissions
- An IAM role or instance profile with
ImageBuilderInstanceProfile
andiam:PassRole
- AWS Management Console access
1. Access EC2 Image Builder
- Sign in to the AWS Management Console.
- In the Services menu, search for EC2 Image Builder.
- Open the service to view the Image Builder dashboard, which highlights three main steps: Recipe, Infrastructure configuration, and Distribution.
2. Create a New Image Pipeline
- Click Create image pipeline.
- Enter a Pipeline name (e.g.,
nginx-pipeline
). - Add a description such as Install Nginx.
- Enable Enhanced metadata collection to capture detailed build metrics.
- Optionally, turn on security scanning for vulnerability reports.
3. Schedule Your Builds
You can trigger your image builds automatically or manually.
Schedule Type | Description |
---|---|
Manual | Run the pipeline on demand |
CRON | Automate builds on a time-based schedule |
For this demo, select Manual.
4. Define Your Recipe
A recipe lists the components and settings for your AMI.
- Click Next and choose Create new recipe.
- Select AMI as the target image type.
- Fill out the form:
Field | Value |
---|---|
Name | nginx |
Version | 1.22.0 |
Description | Install Nginx latest |
Base Image | Quick Start → Amazon Linux 2 (ARM64) |
- Check Remove after build to uninstall the SSM agent post-build.
- Leave the working directory at
/tmp
.
Version Format
Ensure the version follows semantic versioning (x.y.z
) to avoid validation errors.
5. Create a Build Component
Components are YAML definitions that execute commands in phases.
Click Create build component.
Choose Linux as the compatible OS.
Fill in component details:
- Name:
nginx
- Version:
1.22.0
- Description: Install Nginx version 1.22.0
- Name:
In the YAML editor, define the build, validate, and test phases:
name: install-nginx-latest
description: Install Nginx version 1.22.0
schemaVersion: 1.0
phases:
- name: build
steps:
- name: update-os
action: ExecuteBash
inputs:
commands:
- sudo yum update -y
- name: install-nginx
action: ExecuteBash
inputs:
commands:
- sudo amazon-linux-extras enable nginx1
- sudo yum install nginx -y
- name: enable-nginx
action: ExecuteBash
inputs:
commands:
- sudo systemctl enable nginx
- name: validate
steps:
- name: validate-status
action: ExecuteBash
inputs:
commands:
- systemctl status nginx
- name: test
steps:
- name: reboot-test
action: ExecuteBash
inputs:
commands:
- sudo reboot
Save the component. Back in the pipeline wizard, filter Components by Owned by me and select your nginx
component.
6. Add Tests
By default, Image Builder offers several tests. Select the Reboot Test to ensure the AMI boots correctly after a restart.
7. Configure Storage
Specify EBS settings for the build instance’s root volume:
- Size: 8 GB
- Type: gp3
- IOPS: Default
- Encryption: Enable (recommended)
8. Select a Workflow
Choose Default to let Image Builder orchestrate phases automatically. Use Custom if you need to reorder or skip specific steps.
9. Infrastructure Configuration
Define the compute environment for builds:
- Click Create new configuration.
- Name it (e.g.,
nginx-build-config
). - Attach an IAM instance profile with
ImageBuilderInstanceProfile
. - Choose an instance type, such as
t4g.medium
for ARM64. - Optionally, add an SNS topic for notifications.
10. Distribution Settings
By default, the AMI is shared in the current account and region. Add other accounts or regions if you need cross-account or cross-region distribution.
11. Review & Create
Double-check all settings:
- Pipeline name and metadata
- Recipe details
- Component tests
- Storage configuration
- Workflow selection
- Infrastructure profile
- Distribution targets
Click Create pipeline to launch.
12. Run the Pipeline
- Navigate to Image pipelines in the console.
- Select your newly created
nginx-pipeline
. - Click Run pipeline.
The status will change to Building.
13. Monitor Build Execution
As the pipeline runs, a build instance spins up. To view logs:
- Under Pipeline executions, select the active run.
- Click Log stream to open CloudWatch Logs.
14. Verify AMI Creation
After completion, go to the EC2 AMIs console.
Filter by Owned by me to confirm your new AMI and its status.
15. Test Instance Launch
Image Builder automatically launches a test instance:
- It boots the AMI.
- Runs the reboot-test.
- Terminates the instance upon success.
Check the EC2 Instances console or the component library to confirm.
To dive deeper into execution details, view the CloudWatch logs:
16. Completion
When the test instance terminates successfully and the pipeline status shows Available, your custom Nginx AMI is ready to deploy.
Links and References
Watch Video
Watch video content