DEV Community

Cover image for Packer Series - Blog #1 Creating our first AWS AMI with Hashicorp Packer
Rajit Paul
Rajit Paul

Posted on

Packer Series - Blog #1 Creating our first AWS AMI with Hashicorp Packer

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 -

  • Builders
  • Provisioners
  • Post-Processors

*** 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

Alt Text

Basic pre-requisites for creating an AWS AMI with Packer would be -

  1. Having an AWS account
  2. Installing AWS CLI
  3. Creating an IAM User with EC2 Full Access Policy
  4. 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-
https://www.youtube.com/watch?v=5YnTstk3RxM
https://www.youtube.com/watch?v=JvtmmS9_tfU
https://www.youtube.com/watch?v=n3KacV0UlSM

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 -

https://www.packer.io/docs/builders/amazon

To know more about Packer Terminologies -

https://www.packer.io/docs/terminology

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

Alt Text

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 :)

Alt TextAlt Text

Thanks for reading, see you in the next blog!

Top comments (0)