Create a custom AMI using EC2 Image Builder
We all know that how difficult is AMI creation when we do have multiple environments which have different requirement of application and configuration.
A couple of tools available in the market that will automate the creation of the AMI/Image using pre-configured scripts. For example, Hashicorp Packer is a widely used tool to create cross-platform server images either on Azure or Amazon.
Amazon launches the same kind of service called EC2 Image Builder. It helps you to create a wide range of AMI for Windows and multiple flavors of Linux.
In this blog post, we will create a pre-configured Ubuntu 18 AMI and install Django, Gunicorn, and Nginx on it and configure the required components to function the application correctly.
Step 1 - Login to the AWS console and go to EC2 Image Builder. You will see the landing page.
Step 2 - Expand the menu icon and go to Saved configurations -> Components and click on Create Component.
Fill the form using the below details.
- Compatible OS Versions - Ubuntu 18
- Component Version - 0.1.0
- Component name - ubuntu18-django-gunicorn-nginx
Step 3 - Copy the content from https://github.com/thecloudrecipes/ec2-image-builder-component/blob/main/component.yml and paste it in the Definition Document text area and click on Create Component.
Now your component is ready to install python3, gunicorn, nginx, and will perform the required configuration as well. Component steps will execute during the build process of AMI. Now create a recipe with the OS and components.
Step 1 - Go to Saved configurations -> Image recipes and click on Create recipe.
Fill the form using the below details.
- Name - myapp-ubuntu18-recipe
- Version - 0.1.0
- Click on Select managed images and click on Ubuntu.
- Select Image name as Ubuntu Server 18 LTS x86
Step 2 - Now select the build components to produce the desired AMI. For example, if we want to have an aws cloudwatch agent and awscli to be installed on the AMI then select build components with the name amazon-cloudwatch-agent-linux and aws-cli-version-2-linux.
Now, it’s time to use a custom component to install Django, Gunicorn, and Nginx. To choose the ubuntu18-django-gunicorn-nginx custom component, select Owned by me. It will show the build component, then click on the checkbox to add it. Your screen will look like the below screenshot.
Once you selected the build components go down to the page. At the end of the page, you will find storage configuration settings.
If necessary you can increase the root volume size or add additional volume as per the requirement. Now click on Create image recipe.
Your image recipe is ready with the mention configuration. You can use the same image recipe during the image pipeline creation.
Create Image Pipeline
Step 1 — Go to Image pipelines and click on Create image pipeline.
Add Name as myapp-ubuntu18-image and select build schedule as Manual. We will manually trigger the build pipeline as and when required. Once done click on the Next button.
Step 2 - On this screen, we need to select the image recipe.
Click on Use existing recipe and select the recipe with the name myapp-ubuntu18-recipe. Once you selected the specified recipe then click on the Next button.
Step 3 - Next two steps Define infrastructure configuration and Define distribution settings are optional. You can skip these two steps. Now you will see the Review screen. Verify the configurations mentioned on the screen and click on Create Pipeline. Now you are ready with the image pipeline. You will screen something like below.
Now we need to trigger the image pipeline execution manually to create the EC2 image. Now click on the myapp-ubuntu18-image link.
Step 4 - You will see the image pipeline details and the status of the pipeline.
Now click on Actions and then Run Pipeline. It will start running the image pipeline to create the EC2 image. In the background, it will create one EC2 instance and execute the components steps to install and configure the applications on it. Once the build runs successfully, then it will create the EC2 AMI out of it. This AMI will be available in the EC2 console under the AMI section.
You can have multiple versions of components and recipes over a period of time and you can achieve great automation of AMI's as per the requirement of your projects.