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
In a new browser tab, navigate to https://packer.io/downloads.html.
Right-click the 64-bit link in the Linux section, and choose Copy link address.
Paste the link address into a text file, as we’ll need it a little later.
In the AWS browser tab, navigate to Cloud9.
Click Open IDE.
In the Cloud9 terminal, become the
root
user:sudo su
Change directory to
/usr/local/bin
:cd /usr/local/bin
Download the Packer installer, replacing
<PACKER_LINK>
with the one you copied earlier:wget <PACKER_LINK>
Extract the file:
unzip packer_1.5.5_linux_amd64.zip
Remove the Packer ZIP file:
rm packer_1.5.5_linux_amd64.zip
Exit the
root
user session:exit
Verify Packer works:
packer --version
- Create a packer.json File
In Cloud9, click File > New File.
Click File > Save As.
Name your file "packer.json".
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" ] } ] }
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.
base_ami
- In a new browser tab, navigate to EC2.
- Click Launch Instance.
- Copy the AMI ID at the end of the "Amazon Linux AMI" line, and paste it into a text file.
vpc_id
- Navigate to VPC > Your VPCs.
- Copy the VPC ID, and paste it into a text file.
subnet_id
- Click Subnets in the left-hand menu.
- Check the box to the left of the name for the first subnet in the list.
- Ensure Auto-assign Public IP is set to yes for this subnet.
- Copy the Subnet ID, and paste it into a text file.
Run the Command
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
Once the command has completed, copy the AMI ID from the output.
In another browser tab, navigate to EC2 > AMIs to verify your new AMI is listed.
- Build an EC2 Instance Using the AMI
- Click Launch.
- Check the box to select a t2.small instance type.
- Click Next: Configure Instance Details > Next: Add Storage > Next: Add Tags.
- Add a tag:
- Key: Name
- Value: test-ami
- Click Next: Configure Security Group > Review and Launch > Launch.
- Choose Proceed without a key pair and check the box to acknowledge.
- Click Launch Instances.
- 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:"
- 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.
- Click Launch.