Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Using Packer to Create an AMI

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Jul 30, 2018

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Install Packer on the Cloud9 EC2 Instance

    1. In a new browser tab, navigate to https://packer.io/downloads.html.

    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:

      unzip packer_1.5.5_linux_amd64.zip
      
    10. Remove the Packer ZIP file:

      rm packer_1.5.5_linux_amd64.zip
      
    11. Exit the root user session:

      exit
      
    12. Verify Packer works:

      packer --version
      
  2. Challenge

    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. Save the file, then install the Amazon plugin to create custom images on AWS:

      packer plugins install github.com/hashicorp/amazon
      
    6. Validate the packer.json file by running the following command in the terminal window at the bottom of the page:

      packer validate packer.json
      
  3. Challenge

    Build an AMI Using packer.json

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

    base_ami

    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.

    vpc_id

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

    subnet_id

    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.

  4. Challenge

    Build an EC2 Instance Using the AMI

    1. Click Launch.
    2. Check the box to select a t2.small instance type.
    3. Click Next: Configure Instance Details > Next: Add Storage > Next: Add Tags.
    4. Add a tag:
      • Key: Name
      • Value: test-ami
    5. Click Next: Configure Security Group > Review and Launch > Launch.
    6. Choose Proceed without a key pair and check the box to acknowledge.
    7. Click Launch Instances.
    8. 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:"
    9. 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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans