Creating custom AMI using EC2 Image Builder

Introduction

Amazon Machine Images (AMIs) are pre-configured virtual machines used to launch instances in Amazon Elastic Compute Cloud (EC2). An AMI includes an operating system, applications, libraries, and other dependencies needed to run an instance. Creating custom AMIs allows users to launch instances with the desired software stack installed. In this tutorial, we will walk through the process of creating a custom AMI using Amazon EC2 Image Builder.

Prerequisites

To follow along with this tutorial, you will need the following:

  • An AWS account with permissions to create EC2 instances
  • An EC2 instance running a supported Linux operating system
  • AWS CLI installed and configured on your local machine
  • EC2 Image Builder configured and set up with a distribution configuration to build your custom AMI
  • Access to the Amazon S3 bucket where the custom AMI will be stored
  • Basic knowledge of Linux commands

Step 1: Launch EC2 instance

To create a custom AMI, start by launching an EC2 instance with the desired operating system and software stack installed. For this tutorial, we will use an Ubuntu 20.04 LTS instance.

  1. Open the Amazon EC2 console and click on “Launch Instance”.
  2. Choose the Ubuntu Server 20.04 LTS AMI and select an instance type that meets your requirements.
  3. Complete the remaining steps of the launch wizard by selecting the desired VPC, subnet, security group, and key pair for SSH access.
  4. Launch the instance and wait for it to become available.

Step 2: Configure the instance

After the EC2 instance is launched, connect to it using SSH and configure it to install the necessary software packages, libraries, and dependencies for your software stack. For this tutorial, we will install the Apache web server.

  1. Connect to the instance using SSH.
  2. Update the package index and upgrade any outdated packages by running the following commands:
sudo apt update
sudo apt upgrade -y
  1. Install the Apache web server by running the following command:
sudo apt install apache2 -y
  1. Verify that the Apache web server is running by navigating to the public IP address of the instance in a web browser. If Apache is installed correctly, you should see the default Apache web page.

Step 3: Create an EC2 Image Builder recipe

After the EC2 instance is configured with the desired software stack, create an EC2 Image Builder recipe to automate the creation of a custom AMI.

  1. Open the EC2 Image Builder console and select “Create Image Recipe”.
  2. Choose a name and a description for the recipe.
  3. For the “Base AMI” field, select the Ubuntu 20.04 LTS AMI that was used to launch the EC2 instance.
  4. Under “Component Configuration”, select “Add Component” and choose “Shell Script”.
  5. Enter a name and description for the script and add the following commands to install and configure Apache:
#!/bin/bash
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y
sudo systemctl enable apache2
  1. Save the recipe.

Step 4: Create an EC2 Image Builder pipeline

After the recipe is created, use it to create an EC2 Image Builder pipeline that will automate the process of creating custom AMIs.

  1. Open the EC2 Image Builder console and select “Create Image Pipeline”.
  2. Choose a name and description for the pipeline.
  3. Under “Source”, select “EC2 Image Recipe” and choose the recipe that was created in Step 3.
  4. Under “Distribution Configuration”, select “My Distribution Configuration” or choose an existing configuration that specifies the settings for the custom AMI.
  5. Under “Setup”, specify the S3 bucket and IAM role that Image Builder will use to create and store the custom AMI.
  6. Save the pipeline.

Step 5: Build the custom AMI

After the pipeline is created, start the pipeline to build the custom AMI.

  1. Open the EC2 Image Builder console, select the pipeline that was created in Step 4, and click on “Build Image”.
  2. Choose a name and description for the custom AMI.
  3. Wait for the pipeline to complete the image build process.
  4. Verify that the custom AMI is available in the specified S3 bucket.

Conclusion

Custom AMIs allow users to launch EC2 instances with the software stack and configurations that are required by their applications. With Amazon EC2 Image Builder, creating and managing custom AMIs becomes an automated and streamlined process. By following the steps outlined in this tutorial, users can create custom AMIs using EC2 Image Builder in an efficient and scalable manner.

Related Post