Easy Guide to Building AMIs Using Packer for AWS

Mohit MeshramMohit Meshram
3 min read

In the world of cloud computing, automation is key to efficient infrastructure management. One powerful tool that simplifies this process is Packer, developed by HashiCorp. In this blog post, I’ll walk you through my Packer AMI Build Project, which creates an Amazon Machine Image (AMI) on an Ubuntu base that installs essential software like Nginx, Git, and Docker.

1. What is Packer?

Definition: Packer is an open-source tool developed by HashiCorp that automates the creation of machine images for multiple platforms from a single source configuration. Functionality: It allows you to define a machine image template using a JSON or HCL (HashiCorp Configuration Language) configuration file, specifying what software to install and how to configure the image. Supported Platforms: Packer supports various platforms such as AWS (AMI), Azure (VM Image), Google Cloud (GCE), and many others.

2. Why is Packer Useful?

  • Consistency: Ensures consistent image builds across different environments, minimizing discrepancies between development, staging, and production.

  • Speed: Automates the image creation process, significantly reducing the time needed to build images manually.

  • Multi-Platform Support: Allows users to create images for different cloud providers and platforms simultaneously, streamlining deployment processes.

  • Integration: Easily integrates with other tools like Terraform, Ansible, and Jenkins, enhancing your CI/CD pipelines.

  • Versioning: Facilitates version control of images, enabling rollbacks and reproducibility in deployments.

3. How to Use Packer

Installation Before getting started, you need to install Packer on your system. Here are the steps for various operating systems:

Mac: Use Homebrew to install Packer: brew install packer

Linux: Download the latest version from the Packer website and unzip it: wget https://releases.hashicorp.com/packer/x.y.z/packer_x.y.z_linux_amd64.zip unzip packer_x.y.z_linux_amd64.zip sudo mv packer /usr/local/bin/

Windows: Download the installer from the Packer website and follow the installation instructions. https://developer.hashicorp.com/packer/install?product_intent=packer

Configuration

  1. Variables (packer-vars.json) The packer-vars.json file contains the following key information:

    { "aws_access_key": "abc", "aws_secret_key": "xyz", "region": "us-east-2", "source_ami": "ami-0ea3c35c5c3284d82", "instance_type": "t2.micro", "vpc_id": "vpc-09a4ce0aa31937649", "subnet_id": "subnet-0fc0b086ab9266897", "aws_profile": "user1" }

- aws_access_key and aws_secret_key: Your AWS credentials. Replace "abc" and "xyz" with your actual credentials.

- region: The AWS region where the AMI will be built (e.g., us-east-2).

- source_ami: The base AMI to use for the build (in this case, an Ubuntu AMI).

- instance_type: The type of EC2 instance (e.g., t2.micro).

- vpc_id and subnet_id: The VPC and Subnet where the instance will be launched.

- aws_profile: Your AWS profile, if applicable.

  1. Packer Configuration (main.pkr.hcl)

The main.pkr.hcl file defines the configuration for Packer. It uses the variables defined in packer-vars.json to customize the build. It includes provisioning scripts that install Nginx, Git, and Docker, and configure a web server to serve content from a cloned Git repository.

  1. Variables File (variables.pkr.hcl)

The variables.pkr.hcl file defines variables used in the Packer build. Ensure that this file contains the correct variables for your environment.

Steps to Build the AMI:

  1. Clone the Repository Clone this repository to your local machine: git clone https://github.com/mohit-decoder/packer.git

  2. Make necessary changes Make the necessary changes in file packer-vars.json

  3. Run Export command export AWS_ACCESS_KEY_ID=your_access_key export AWS_SECRET_ACCESS_KEY=your_secret_key

  4. Validate the Packer Template Run the following command to ensure the Packer template is valid: packer validate -var-file=packer-vars.json .

  5. Build the AMI Run the following command to start building the AMI: packer build -var-file=packer-vars.json .

You can refer my github repo: https://github.com/mohit-decoder/packer.git

Video Reference: https://youtu.be/cZEKWxYeEUA?si=spDz8ZxeomvaJfnb

Also thanks to mentor Saikiran Pinapathruni for creating such informative and real-time scenario based projects and practicals.

0
Subscribe to my newsletter

Read articles from Mohit Meshram directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohit Meshram
Mohit Meshram