Using Packer to Create an AMI

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

In this hands-on lab, you are a DevOps Engineer that works for OmniCorp. Your team has an AWS account where you deploy out your dev and test environments. After having to create AMIs manually, your team has decided that it’s time to automate the process. You are given access to Cloud9 where you will use Packer to create a base AMI for the team.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Install Packer on the Cloud9 EC2 Instance
  1. In a new browser tab, navigate to

  2. Right-click the 64-bit link in the Linux section, and choose Copy link address.

  3. Paste the link address into a text file, as we’ll need it a little later.

  4. In the AWS browser tab, navigate to Cloud9.

  5. Click Open IDE.

  6. In the Cloud9 terminal, become the root user:

    sudo su
  7. Change directory to /usr/local/bin:

    cd /usr/local/bin
  8. Download the Packer installer, replacing <PACKER_LINK> with the one you copied earlier:

    wget <PACKER_LINK>
  9. Extract the file:

  10. Remove the Packer ZIP file:

  11. Exit the root user session:

  12. Verify Packer works:

    packer --version
Create a packer.json File
  1. In Cloud9, click File > New File.

  2. Click File > Save As.

  3. Name your file "packer.json".

  4. Provide the following JSON for your file, replacing <USERNAME> with a username of your choosing:

      "variables": {
        "instance_size": "t2.small",
        "ami_name": "ami-<USERNAME>",
        "base_ami": "ami-1853ac65",
        "ssh_username": "ec2-user",
        "vpc_id": "",
        "subnet_id": ""
      "builders": [
          "type": "amazon-ebs",
          "region": "us-east-1",
          "source_ami": "{{user `base_ami`}}",
          "instance_type": "{{user `instance_size`}}",
          "ssh_username": "{{user `ssh_username`}}",
          "ssh_timeout": "20m",
          "ami_name": "{{user `ami_name`}}",
          "ssh_pty" : "true",
          "vpc_id": "{{user `vpc_id`}}",
          "subnet_id": "{{user `subnet_id`}}",
          "tags": {
            "Name": "App Name",
            "BuiltBy": "Packer"
      "description": "AWS image",
      "provisioners": [
          "type": "shell",
          "inline": [
            "sudo yum update -y",
            "sudo yum install -y git"
  5. Validate the packer.json file by running the following command in the terminal window at the bottom of the page:

    packer validate packer.json
Build an AMI Using packer.json

We need to obtain our variable values for the command at the end of this step.


  1. In a new browser tab, navigate to EC2.
  2. Click Launch Instance.
  3. Copy the AMI ID at the end of the "Amazon Linux AMI" line, and paste it into a text file.


  1. Navigate to VPC > Your VPCs.
  2. Copy the VPC ID, and paste it into a text file.


  1. Click Subnets in the left-hand menu.
  2. Check the box to the left of the name for the first subnet in the list.
  3. Ensure Auto-assign Public IP is set to yes for this subnet.
  4. Copy the Subnet ID, and paste it into a text file.

Run the Command

  1. Use the values you gathered to populate the following command in the Cloud9 terminal:

    packer build -var 'ami_name=ami-<USERNAME>' -var 'base_ami=<AMI_ID>' -var 'vpc_id=<VPC_ID>' -var 'subnet_id=<SUBNET_ID>' packer.json
  2. Once the command has completed, copy the AMI ID from the output.

  3. In another browser tab, navigate to EC2 > AMIs to verify your new AMI is listed.

Build an EC2 Instance Using the AMI
  1. Click Launch.
    1. Check the box to select a t2.small instance type.
    2. Click Next: Configure Instance Details > Next: Add Storage > Next: Add Tags.
    3. Add a tag:
      • Key: Name
      • Value: test-ami
    4. Click Next: Configure Security Group > Review and Launch > Launch.
    5. Choose Proceed without a key pair and check the box to acknowledge.
    6. Click Launch Instances.
    7. On the next screen, you should see a green box saying "Your instances are now launching". Click the instance ID number provided next to the text "The following instance launches have been initiated:"
    8. Watch your AMI progress to a "Running" instance state. You may need to click the refresh icon in the top-right to show the updated state.

Additional Resources

  1. Log in to the AWS Console and navigate to Cloud9.

  2. sudo to root and install packer into /usr/local/bin. Then drop out of root.

  3. Use Cloud9 to create a packer.json file with the following contents:

    • Create a variable called instance_size; the default values should be t2.small.
    • Create a variable called ami_name; set the default value to ami-[USERNAME]. This will be the name of the AMI.
    • Create a variable called base_ami; the default value shoudl be ami-1853ac65
    • Create a variable called ssh_username; the default value should be ec2-user.
    • Create a variable called vpc_id, there should be no default value.
    • Create a variable called subnet_id, there should be no default value.
    • It should use the AWS builder.
    • The region should be set to us-east-1.
    • Source AMI should use the ami_base variable.
    • Instance Type should use the instance_size variable.
    • SSH username should use the ssh_username variable.
    • The ssh timeout should be set to 20m.
    • The AMI name should use the ami_name variable.
    • Set SSH pty to true.
    • Set the VPC ID to use the vpc_id variable.
    • Set the Subnet ID to use the subnet_id variable.
    • Create two tags:
      • Call the first one Name with a value of “Base AMI”.
      • Call the second BuiltBy and set it to “Packer”.
    • The description should be set to “AWS image”.
    • Create an inline shell provisioner that will execute a yum update and install git.
  4. Validate the packer.json file.

  5. Execute the packer build and supply the VPC ID and Subnet ID as variables. Use AMI ami-1853ac65 for the base_ami.

  6. After the AMI has completed building, create an EC2 instance using the AMI that was built and set the instance type to t2.small.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?