Anil's Devops Journey


Anil's Devops Journey


Create Golden Image using Packer

Anil Appari's photo
Anil Appari
·Oct 2, 2022·

3 min read

What's a Golden Image ?

A golden image is a base image on top of which developers can build applications, letting them focus on the application itself instead of system dependencies and patches. A typical golden image includes common system, logging, and monitoring tools, recent security patches, and application dependencies.

How it can be used?

Well, I have used it along with EC2 auto scaling. This golden image acted as the ami for our ec2. Since, this Golden Image already equipped with required application dependencies, application will be scaled up and accessible for end user in less time.

What is a packer?

Packer is an open source tool from Hashicorp, enables you to create identical machine images for multiple platforms from a single source template.

Let's start with installing Packer in Ubuntu.

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

Check for other installation at Packer.

Let's start building a simple image in which nginx will be installed.

Let's create a configuration file for Packer.


packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = ""
data "amazon-ami" "ubuntu-focal" {
  region = "us-east-1"
  filters = {
    name                = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  most_recent = true
  owners      = ["099720109477"]

source "amazon-ebs" "ubuntu" {
  ami_name      = "anils-packer-${formatdate("YYYY-MMM-DD-hh'hr'-mm'min'", "${timestamp()}")}" 

  instance_type = "t2.micro"
  region        = "us-east-1"
  source_ami =    ##  ami-0c1704bac156af62c
  ssh_username = "ubuntu"
  tags = {
    Name        = "demo"
    environment = "production"

build {
  name = "learn-packer"
  sources = [
  provisioner "shell" {

    inline = [
      "sudo apt-get clean",
      "echo updating",
      "sudo apt update",
      "echo Installing nginx",
      "sudo apt install -y nginx",

Let's start building it.

Export the aws credentials access key & secret key

export AWS_ACCESS_KEY_ID='************'
export AWS_SECRET_ACCESS_KEY='***********'

Create a folder and save the hcl file in it.

ubuntu@ip-172-31-31-137:~$ mkdir test-1
ubuntu@ip-172-31-31-137:~$ cd test-1/
ubuntu@ip-172-31-31-137:~/test-1$ ls

Initialize Packer

ubuntu@ip-172-31-31-137:~/test-1$ packer init aws-ami-nginx.pkr.hcl
Installed plugin v1.1.5 in "/home/ubuntu/.config/packer/plugins/"

Validate the hcl configuration

ubuntu@ip-172-31-31-137:~/test-1$ packer validate aws-ami-nginx.pkr.hcl
The configuration is valid.

Build the image

ubuntu@ip-172-31-31-137:~/test-1$ packer build aws-ami-nginx.pkr.hcl output will be in this color.

==> Prevalidating any provided VPC information
==> Prevalidating AMI Name: anils-packer-2022-Oct-02-05hr-44min Found Image ID: ami-0c1704bac156af62c
==> Creating temporary keypair: packer_6339255b-da8d-1231-d110-a250540bbcca
==> Creating temporary security group for this instance: packer_6339255c-8ce2-034b-d80a-c93c7c405841
==> Authorizing access to port 22 from [] in the temporary security groups...
==> Launching a source AWS instance... Instance ID: i-0385ead063ff2bf1f
==> Waiting for instance (i-0385ead063ff2bf1f) to become ready...
==> Using SSH communicator to connect:
==> Waiting for SSH to become available...
==> Connected to SSH!
==> Provisioning with shell script: /tmp/packer-shell1702171532 updating

==> Tagging snapshot: snap-0f655f5811e2b066f
==> Creating AMI tags Adding tag: "Name": "demo" Adding tag: "environment": "production"
==> Creating snapshot tags
==> Terminating the source AWS instance...
==> Cleaning up any extra volumes...
==> No volumes to clean up, skipping
==> Deleting temporary security group...
==> Deleting temporary keypair...
Build '' finished after 3 minutes 45 seconds.

==> Wait completed after 3 minutes 45 seconds

==> Builds finished. The artifacts of successful builds are:
--> AMIs were created:
us-east-1: ami-0bcc39f53ee5ffb19

Check the AMI at AWS Dashboard


Now let's deploy ec2 with this AMI. Select AMI and Click on "Launch Instance from AMI"

Fill the required..


Go to EC2 Console > Get Public IP of instance


Based on your application, configure the script for installing required dependencies and try to build ami.

Packer Configuration file will be available at Github.

Thanks for reading...

Share this