Building An Immutable AWS AMI With Packer
Posted By : Faisal Ansari | 01-May-2020
Packer is an open-source tool used for creating custom machine images for multiple cloud platforms like AWS, Azure, GCP from a single source configuration. As the name suggests it combines all your software, packages, configurations while creating your machine images. It is lightweight, runs on every major operating system, and is highly performant. It gives us the ability to automate the machine image creation process. When building images, Packer can use tools like Ansible or Shell scripts to install and configure software onto the image.
Advantage of Using Packer
Packer makes it extremely easy to take advantage of all the following benefits.
Superfast infrastructure deployment.
Multi-provider portability.
Improved stability.
Greater testability.
In this article, we will learn about how the packer works and will build a custom AWS AMI. What are you waiting for? Let's get started!
Packer Commands
It is controlled using a command-line interface. Like many other command-line tools, the packer tool takes the following subcommand to execute:
- Build:: This command takes a template and runs all the various builds specified within a template to generate a set of artifacts. And the artifacts that are created will be outputted at the end of the build.
- Inspect:: This command takes a template and outputs the various components a template defines. It also tells you about things like what variables a template accepts, the builders it defines, the provisioners it defines, the order they will execute, and more.
- Validate:: This command is used to validate the syntax and configuration of a template. The command will return a zero exit status on success, and a non-zero exit status on failure.
- Fix:: This command takes a template and finds backward-incompatible parts of it and brings it up to date so it can be used with the latest version of Packer.
Templates
Templates are JSON configuration files that configure the various components of Packer to create one or more machine images. Templates are portable, static, and readable, and writable. It has a set of keys configuring various components of Packer. The keys applicable are listed below in a template.
- Variables (required):: It is an object of one or more key/value strings that define user variables contained in the template. If it is not specified, then no variables are defined
- Builders (optional):: It is an array of one or more objects that defines the builders that will be Used to create machine images for this template, configuring each builder.
- Provisioners (optional):: It is an array of one or more objects that defines the provisioners that will be used to install and configure the software for the machines created by each of the builders. If it is not specified, then no provisioners will be run.
- Post-processors (optional):: It is an array of one or more objects that defines the various post-processing steps to take with the built images. If not stated, there'll be no post-processing.
Installing Packer
-
Download the required package from here. https://www.packer.io/downloads.html
-
Unzip the package and set the path variable in ~/.bashrc
export PATH=$PATH:/path/to/packer
-
Refresh terminal
source ~/.bashrc
-
Verify packer installation by executing the packer command.
packer version
Building An Image
Packer configuration templates are written in JSON format.
An example.json template for packaging an AWS AMI is given below.
{
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-a25415cb",
"instance_type": "t2.micro",
"ssh_username": "ec2-user",
"ami_name": "packer-example {{timestamp}}"
}],
"provisioners": [{
"type": "shell",
"script": "ansible.sh"
}]
}
Ansible.sh
#!/bin/bash
set -e
sudo yum -y update
sudo yum -y upgrade
sudo yum install -y python-dev python-pip
sudo pip install ansible
sudo wget ‘https://s3.amazonaws.com/packeramidemo/i_playbook.yml'
echo "Running build."
sudo ansible-playbook i_playbook.yml
Building an Image
-
Lets validate and inspect our template using the following commands.
packer validate example.json
packer inspect example.json
-
To build our new AMI, use the following command.
packer build example.json
This will take a while as the update and the AMI creation are a long process. After some time, this AMI is ready to use. If you wanted you could go in the AMIs section and launch this AMI right now and it would work great.
I hope the above setup guide will help you in getting started with Packer.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Faisal Ansari
He is an tech evangelist and has a keen interest in the field of Cloud and IOT.He's also very adaptive to latest technologies and open source tools.At spare time he likes to read and do blogs on latest technologies.