How to Create a Custom AMI with Image Pipeline and Automate its Creation Using EC2 Image Builder
Tags: ec2imagebuilder, aws, iam, cloudwatch
Published: 2022-04-02 17:26:47

“Challenges faced to find the solution of how to create a custom ami with image pipeline and automate its creation using ec2 image builder”. I have checked various documents on how to create a custom ami with required OS and packages and also able to automate the process of creating the custom image. I have found a way to do it using the EC2 image builder service of AWS. EC2 image builder is easy to use and able to create a custom AMI with image pipeline and also it is free of charge. We can also do custom VPC and other configuration while creating AMI in the image pipeline.

EC2 Image Builder is a fully managed AWS service that makes it easier to automate the creation, management, and deployment of customized, secure and up-to-date server images that are pre-installed and pre-configured with software and settings to meet specific IT standards.

EC2 Image Builder simplifies the building, testing and deployment of Virtual Machine and container images for use on AWS or on-premises. Keeping Virtual Machine and container images up-to-date can be time consuming, resource intensive and error-prone. Currently, customers either manually update and snapshot VMs or have teams that build automation scripts to maintain images.

Image Builder significantly reduces the effort of keeping images up-to-date and secure by providing a simple graphical interface, built-in automation, and AWS-provided security settings. With Image Builder, there are no manual steps for updating an image nor do you have to build your own automation pipeline. Image Builder is offered at no cost, other than the cost of the underlying AWS resources used to create, store and share the images.

In this post, you will get to know how to create a custom ami with image pipeline and automate its creation using ec2 image builder. Here I have used an EC2 image builder and IAM role with required permissions policy. 

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4677v1zvz05dpheqf2uw.png)
The architecture diagram shows the overall deployment architecture with data flow, ec2 image builder and ec2 console with AMI.

#Solution overview
The blog post consists of the following phases:
1. Create Image Pipeline with required IAM permissions
2. Run the Image Pipeline and Get the AMI and Snapshot Available
3. Check the Log Events in Cloudwatch

##Phase 1: Create Image Pipeline with required IAM permissions  
1. Open the ec2 image builder console, click on create image pipeline. Give the pipeline name, choose the build schedule, create an image recipe with the image type and its version. Select OS, instance configuration and working directory. Create a build component with component details and content. Also select the test to verify the output AMI and can also set the storage volume as per requirement. Define infrastructure configuration and distribution settings. And then create an image pipeline.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccli5oeittuse4jlem1c.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovm7n0u1u53rpb50og26.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfhu3oj16qsgudfq0d05.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q8vnoi5ppjqb03vmj81.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c743gb76dljvix7c4uig.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vayc8vslfs4z1l2pnmh2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x883e4w2b8moprjvt4ak.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yku1r3j548qsiljff3s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4a5tkgypqlx7apfio0t.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09amdq18q26yectfb851.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0pjc16z276i4gdapqf2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otp1vvlx150unwj2j101.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95jwcqrmorh3wz0wbiv1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wbzxtt7vu8yrkjbczls.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kh9f6t8ww4bz0yy4syh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8n93i99igswev6cvjte.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xff83o0m5hsvcuve5iqu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ely36m0ltwp5h1li92ix.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9ln1fsm8fdggdwub1v7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxkh5z4wui826w6qx0fz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ud4xnt6jwceps64w31i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/994t07fvfmaj9kmf0f2s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9mt7sc12kury8x7m0fq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmf3arik9z3bp4m29p7y.png)

##Phase 2: Run the Image Pipeline and Get the AMI and Snapshot Available 
1. We can change the infrastructure and scheduler builder settings as per requirement. Once the image pipeline is ready, we can run the pipeline using the run pipeline option. In the Image pipeline, the AMI creation goes through various status as pending, building, testing and distributing state. Once the process completes, we can go to the EC2 console and check the AMI and snapshots created. We can also disable and enable the pipeline.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93oezmlm8se2myfhvh7w.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yzg1fyoj9c2pk18buv2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16l34tta9eqqe5yooyc0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gmw56qzflmiwoze1xj3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zo1gxk5w4rz4ptv8axd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fzzpksw6avmogjzgxrz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyery3vgzlmof2tirigk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbw3xd33ei72w78de7lc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j3x8t47k7c0tvm458ze.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm0rommwvnn2eb5hztx2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgttv0jqbypqmekfsyxw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zto0bk14oaz3zoo2gmt2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fl35p32bi9vzp0dtxeg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hl4yl62mm6c1qf4ez6ia.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xzv87ft9u3hlzaxopuf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v7uy94rq6dwthlibsvm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7z1t1cykehpn9gfwigp0.png)

##Phase 3: Check the Log Events in Cloudwatch
1. Open the cloudwatch console and check the log events in log groups.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvrygt23skhkfiv7vj8o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsvf2n1q1eq7b1s8gb0i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uy97cmxmnk7z6c1d7zlx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw51g2kjx7rwwyrzxckf.png)

#Clean-up
Delete EC2 Image Builder, EC2 AMI and Snapshot, IAM Role, Cloudwatch Log groups.

#Pricing
I review the pricing and estimated cost of this example.
 
No cost for EC2 Image Builder as it's free of charge.

Cost of Cloudwatch = $0.0

Total Cost = $0.0

#Summary
In this post, I showed “how to create a custom ami with image pipeline and automate its creation using ec2 image builder”.

For more details on EC2 Image Builder, Checkout Get started EC2 Image Builder, open the [EC2 Image Builder console](https://ap-south-1.console.aws.amazon.com/imagebuilder/home?region=ap-south-1#/landingPage). To learn more, read the [EC2 Image Builder documentation](https://docs.aws.amazon.com/imagebuilder/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)