ruk·si

Packer
AWS Redis

Updated at 2017-03-19 16:48

Make sure you have builder role defined in AWS.

Go to AWS region you want > EC2 > Launch Instance > Community AMIs and find the image you want to use as the base.

ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170113 (ami-d8f4deab)

Create example.json:

{
  "builders": [{
    "type": "amazon-ebs",
    "region": "eu-west-1",
    "source_ami": "ami-d8f4deab",
    "iam_instance_profile": "PackerBuildInstanceRole",
    "instance_type": "t2.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example-{{timestamp}}"
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sleep 30",
      "sudo apt-get update --fix-missing",
      "sudo unattended-upgrades",
      "sudo apt-get install -y redis-server",
      "sudo apt-get clean",
      "sudo apt-get autoremove",
      "sudo rm -rf /var/lib/apt/lists/*"
    ]
  }]
}

The previous inlined shell script will update all security upgrades, install Redis and remove all extra stuff to keep the image as small as possible in Debian-based system such as Ubuntu.

# Check that the template is valid after modifications
packer validate example.json

# Build the image
packer build example.json
# At the end of build Packer will output something like:
# ==> Builds finished. The artifacts of successful builds are:
# --> amazon-ebs: AMIs were created:
# eu-west-1: ami-29c6f74f