Let us first understand, what is Packer?
Packer is an open source tools used for creating identical machine images for multiple platforms for a single source configuration.
There are three major components of Packer -
*** For this blog we are just going to use the Builder element,and subsequently we will look into Provisioners and Post-Processors in the upcoming blogs in the Packer Series ***
Let's get started
Basic pre-requisites for creating an AWS AMI with Packer would be -
- Having an AWS account
- Installing AWS CLI
- Creating an IAM User with EC2 Full Access Policy
- Running aws configure on base os terminal and providing the created IAM user creds and region
If you are not aware of how to create an IAM user and execute aws configure, please go through these links-
Next step, would be to download the Packer software from https://www.packer.io/downloads
And, we are good to proceed!
Now, we need to choose an IDE of our choice( I have used VS Code for this task), create a packer directory/folder in your system and within it code an example.json file.
example.json code - https://github.com/RajitPaul11/Packer/tree/main/Project1
The Packer component that we have used in this code is Builders which helps us to create a machine image for a single platform (in this case AWS).
Hashicorp Packer Amazon Builders Doc -
To know more about Packer Terminologies -
Once we have the code in place we need to run the packer build command in our terminal, and the command goes as follows -
packer build example.json
The packer build command will
- First validate our AWS Credentials,
- Create a temp key pair, a temp sec group,
- Launch an AWS Instance, connect to the AWS Instance,
- Configure as prescribed, terminate the EC2 Instance.
- Create a private AMI of the EC2 Instance.
- Delete and clean up all the resources.
So the packer build output will look something like this
Once the build is complete, we will be able to see a terminated Packer build EC2 instance in our AWS Console with a private AMI :)
Thanks for reading, see you in the next blog!