AWS-AMI with HashiCorp Packer

AWS-AMI with HashiCorp Packer

Imagine a situation where everyday as we start our and and we need to configure instances with different different requirement, based on developer / tester needs. If we keep on doing this our most of the time will gets involded in creating and deleting the instance and resolving dependency.
Better approach could be if we can keep something ready for developer / tester and put it somewhere and when needed we can just pull and just install few other tools as per situation, in this case it will save a lot of our time.

HashiCorp Packer is such a tool that help us to create an AMI , give the VPC, SUBNET, TYPE OF IMAGE, TOOL NEEDED ETC whatever is required we just need to put it in code and then just it will create a AMI Image which will keep it in EC2 —> AMI tabs.

Implementation ::

Step 1 ) Install Packer

Firstly we need to install Packer in our laptop, I am using Ubuntu 20.04 LTS , below is the link where you can find the download packages and complete instalation guide

https://developer.hashicorp.com/packer/install?product_intent=packer#linux

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packer

Once done just type packer , and if give below output ,means its installed properly

Step 2 ) Clone a repository ::

—> Create a New Repository and clone it into your local system,

mkdir packer
cd packer
git clone git clone https://github.com/ApurvDevops/packer.git

Step 3) Create Packer ::

—> Open VSCode —> Inside Packer folder that you created —> Create a fIle Packer.json and packer-vars.json

—> Created another file docker.services

Content for all 3 files is in below github link

https://github.com/ApurvDevops/packer.git

Step 5 : Create IAM User:

While you are using the AWS Config to created an AMI Image, using Hashi corp packer you need an User with proper permission

Go to IAM —> IAm —> User —> Create User —> Give “EC2 Full Access” —> Next —> Create User

Go to User —> Access Key —> Create Access key —> other —> Next (Save the Access and Secret key at some place as we need there in packer-vars.json

Step 5 : Create AWSAMI

Once everything is done, noe run below commands

packer validate --var-file packer-vars.json packer.json
packer inspect --var-file packer-vars.json packer.json
packer build --var-file packer-vars.json packer.json

Once evething is fine , and build is successful you can see an AMI in your AWS Account

**** Issues faced ****

  1. Mostly the issue that I faced was becasue of Uncompatiable Ubuntu AMI that I mentioned in packer.json file, you need to make sure that linux image you are using is not masking docker file

  2. The way you install docker can also give an issue, I have a google for correct URL of docker

This came to work correctly for me

curl -fsSL https://get.docker.com | bash
  1. The subnet you will use should have SSH access allowed.

  2. Auto assign public ip for the subnets

  3. SSH is allowed in SG -

  4. 'amazon-ebs' packer plugin is installed

In such case you need to install plugin for amazon -ebs , below is the command

packer plugins install github.com/hashicorp/amazon
  1. Instance should be in public subnet

  2. Proper routing in RT

This is it for this blog, try implemeting this . Happy Learning !!!