How to Build AWS AMI Images With Packer

Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration.
Packer can build machine images for multiple platforms in parallel. Packer is able to use tools like Chef or Puppet to install software onto the image.

A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines. Machine image formats change for each platform. Some examples include AMIs for EC2, VMDK/VMX files for VMware, OVF exports for VirtualBox, etc.

Packer uses a JSON template that contains build instructions.

Building a centos image

File ./base-ce-7.json

{
    "variables" : {
        "aws_profile": "citizix",
        "aws_region" : "eu-west-3"
    },
    "builders" : [
        {
            "type" : "amazon-ebs",
            "profile" : "{{user `aws_profile`}}",
            "region" : "{{user `aws_region`}}",
            "vpc_id": "vpc-xxxxx",
            "subnet_id": "subnet-xxxxx",
            "associate_public_ip_address": "true",
            "instance_type" : "t3.medium",
            "source_ami" : "ami-0cb72d2e599cffbf9",
            "ssh_username" : "centos",
            "ami_name" : "citizix-centos-7",
            "ami_description" : "Up to date centos 7 for citizix",
            "run_tags" : {
                "Name" : "packer-builder-base-ce",
                "Tool" : "Packer",
                "Author" : "citizix-devops"
            },
            "tags": {
                "Tool": "Packer",
                "Author": "citizix-devops"
            }
        }
    ],
    "provisioners" : [
        {
            "type" : "shell",
            "script" : "./setup-base-ce-7.sh"
        }
    ]
}

File ./setup-base-ce-7.sh

#/bin/bash

sudo mkdir /tmp/xyz && cd /tmp/xyz

sudo sed -i 's/SELINUX=enforcing/SELINUX=permisive/g' /etc/selinux/config
sudo yum install -y epel-release
sudo yum -y update
sudo yum -y install rsync vim telnet htop git monit jq zip unzip

sudo yum install -y python-pip gcc python-devel
sudo pip install -U pip
sudo pip install docker-compose awscli

sudo rm -rf /tmp/xyz

Building the app:

packer build base-ce-7.json

# You can pass more args
packer build -debug -var-file=vars.json base_ami.json
Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy