Creating custom AWS EC2 images with Packer.io

Wed, Jul 25, 2018 2-minute read

In this post i’ll show how to create custom images for aws ec2 and launching them within seconds.

We need to create a json file to define our image settings.

{
  "variables": {
    "test-var": "test-value"
  },
  "builders": [{
    "type": "amazon-ebs",
    "region": "us-west-2",
    "source_ami_filter": {
      "filters": {
      "virtualization-type": "hvm",
      "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
      "root-device-type": "ebs"
      },
      "owners": ["099720109477"],
      "most_recent": true
    },
    "instance_type": "t2.micro",
    "ssh_username": "ubuntu",
    "ami_name": "webischia-test {{timestamp}}"
  }]
}

After that we just need to build this image and packer do the rest for us.

packer build test.json

Our output is:

amazon-ebs output will be in this color.
  
==> amazon-ebs: Prevalidating AMI Name: webischia-test 1532524410
    amazon-ebs: Found Image ID: ami-ba602bc2
==> amazon-ebs: Creating temporary keypair: packer_5b58777a-03a4-4dbe-123-123
==> amazon-ebs: Creating temporary security group for this instance: packer_5b58777e-e2c9-772f-123-123
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-069fa516123e4a5
==> amazon-ebs: Waiting for instance (i-069fa516123e4a5) to become ready...
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance, attempt 1
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating the AMI: webischia-test 1532521230
    amazon-ebs: AMI: ami-02be45db8e912819c
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-west-2: ami-02be45db8e123a19c