Automating AWS AMI Creation with Packer

·

4 min read

Overview

This article provides an overview of the Packer configuration used to create an Amazon Machine Image (AMI) for an Ubuntu instance. The configuration includes setting up required plugins, defining variables for AWS credentials, specifying the source AMI, and configuring build steps with provisioners.

Prerequisites

  1. Packer installation

  2. AWS CLI installation

  3. AWS CLI configuration

  4. VS code

AMIs

Amazon Machine Images (AMIs) are pre-configured templates that provide information required to launch an instance in AWS. They include: The operating system (e.g., Ubuntu, Windows Server) Application servers and installed software Configuration settings

Use Cases for AMIs

  1. Standardized Deployments: Ensuring all instances launched have the same configurations and software.

  2. Scaling Applications: Quickly launching multiple instances for horizontal scaling.

  3. Backup and Recovery: Creating snapshots of configured servers for backup purposes.

  4. Automation: Integrating with CI/CD pipelines to create fresh AMIs with the latest updates and patches.

Important Notes About AMIs

Once an AMI is created and deployed using Packer, it cannot be modified. If changes are required, a new AMI must be created from scratch using Packer.

The Packer process involves creating a temporary server, installing all the specified packages and configurations, and then terminating the instance once the AMI is created.

Configuration Details

AWS-CLI.sh script

  • Updates the package list to ensure the latest versions of software.

  • Installs the unzip utility required for extracting the AWS CLI.

  • Downloads, extracts, and installs the AWS CLI on the instance.

  • Verifies the installation by displaying the AWS CLI version.

#!/bin/bash -eux

# Install aws cli on the AMI

sudo curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

sudo apt-get update
sudo apt-get install unzip -y
sudo unzip awscliv2.zip
sudo ./aws/install

Create a Packer File

Create a Packer configuration file with a .pkr.hcl extension. This is required because when the packer init and packer build commands are executed, they look for a file ending with .pkr.hcl.

Required Plugins

The Packer configuration requires the Amazon plugin:

packer { 
    required_plugins {
        amazon = {
            version = "~> 1"
            source  = "github.com/hashicorp/amazon"
        }
    }
}

Variables

Two variables are defined to store AWS credentials:

variable "aws_access" {
    type = string
    default = ""
}

variable "aws_secret" {
    type = string
    default = ""
}

Data Source

The data source block fetches the most recent Ubuntu AMI owned by Canonical.

data "amazon-ami" "ubuntu" {
    most_recent = true
    owners = ["099720109477"]
}

Source AMI

The source block defines the configuration for the Amazon EBS-backed Ubuntu instance:

  • Sets the name of the AMI to be created (ubuntu-ami).

  • Defines the instance type as t4g.micro for cost-effective performance.

  • Specifies the AWS region (us-west-2).

  • Links the AMI to the latest Ubuntu image retrieved from the data source.

  • Adds tags for identification and environment tracking.

source "amazon-ebs" "ubuntu" {
    ami_name      = "ubuntu-ami"
    instance_type = "t4g.micro"
    region        = "us-west-2"
    ssh_username  = "ubuntu"
    source_ami    = "${data.amazon-ami.ubuntu.id}"
    run_tags = {
        Name = "ubuntu-ami"
        owner = "Packer"
        Environment = "Dev"
    }
}

Build Configuration

The build block defines the steps to create the AMI, including shell and file provisioners:

  • Build Block: Defines the build process and links the source configuration.

  • Shell Provisioner:

  • Updates the system package list.

  • Installs Apache web server.

  • Downloads, unzips, and installs the AWS CLI.

  • Transfers the aws-cli.sh script to the temporary instance for execution.

build {
    name = "ubuntu-ami"
    sources = ["source.amazon-ebs.ubuntu"]

    provisioner "shell" {
        inline = [
            "sudo apt-get update",
            "sudo apt-get install -y apache2"
        ]
    }

    provisioner "file" {
        source      = "/Users/ansh/Documents/VS code/AMI/Linux/aws-cli.sh"
        destination = "/tmp/aws-cli.sh"
    }
}

Setting Environment Variables and Initializing Packer in terminal

Before running the Packer build, set the AWS credentials as environment variables:

  • Exports AWS Credentials: Makes the AWS Access Key and Secret Key available to Packer.

  • Initializes Packer: Validates and sets up the configuration.

  • Builds the AMI: Executes the build process defined in the Packer configuration.

export AWS_ACCESS_KEY_ID="your_access_key"
export AWS_SECRET_ACCESS_KEY="your_secret_key"

After setting the environment variables, initialize the Packer configuration and build the AMI by running the following commands in the terminal:

packer init .
packer build .

Summary

This Packer configuration sets up an Amazon EBS-backed Ubuntu instance with the t4g.micro instance type, which supports the arm64 architecture. It includes provisioners to update the system, install Apache, and copy a script file to the instance. The AWS credentials are securely passed using environment variables. By leveraging Packer, users can automate the creation of AMIs, ensuring consistency and efficiency in the infrastructure workflows.