What is Packer
Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration , written in Go. Packer is cross platform and supports most of the major operating systems, and has support and integrations with other devops tools ( like Terraform,Chef or Puppet).
A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines. Machine image formats change for each platform. In this blog, we mainly focus on creating machine images from Amazon Machine Images. An Amazon Machine Image (AMI) provides the information required to launch an instance.Packer configuration or template files are are JSON files containing basic information about the AMI to be created, and can contain inline scripts to provision software on the resulting AMI.
CI-CD pipeline to build AMI using Packer( Source: lobster1234.github.io)
Why Packer:
Instead of installing the software after booting up an instance, user can create an AMI with all necessary software on.
Speed up the boot times for instances.
We can avoid manual build of image which is error prone also.Using packer, we can automate the creation of images, create pipelines for building and deploying images as per DevOps practices.
Packer helps to maintain consistency across different environments by standardizing images and keeping same configuration for things like patching, vulnerability remediation,security, networking etc.
Using Packer to Create an AMI:
In this demonstration, we will be creating custom AMI from base AMI in AWS environment. We will see, how we can automate the process to create base AMI in Linux platform.
Install Packer on the AWS Linux 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.
Login to EC2 server and become the root user and execute below commands as per given sequence to install packer.
sudo su
cd /usr/local/bin
wget <PACKER_DOWNLOAD_LINK>
unzip packer_1.6.0_linux_amd64.zip
rm packer_1.6.0_linux_amd64.zip
exit
packer --version
Create a packer.json File:
Name your file "packer_test.json".
Provide the following JSON for your file, replacing <USERNAME> with a username of your choice:
{
"_comment" : "Simple Packer Template using Amazon Linux ",
"variables": {
"instance_size": "t2.micro",
"ami_name": "ami-test-build",
"base_ami": "ami-02c4e2ce5b03c9ee9",
"ssh_username": "ec2-user",
"vpc_id": "",
"subnet_id": ""
},
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-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 java-1.8.0 java-1.8.0-openjdk-devel tomcat8-webapps -y",
"sudo yum remove java-1.7.0-openjdk -y",
"sudo yum install -y git"
]
}
]
}
Validate the packer_test.json file by running the following command in the terminal
window at the bottom of the page:
bash-3.2$ packer validate packer_test.json
Template validated successfully.
The source_ami used is latest release 2018 (at the time of writing) of Amazon Linux in eu-west-1 region. Please refer this URL for amazon Linux AMI Release Notes.
type: Each Builder has a mandatory type field, as we are building this image on AWS we are going to use amazon-ebs type filed.
region: Where we want to build this image, as AMI ID differ based on region ssh_username: We need to tell packer which ssh username to utilize. ami_name: Now we need to tell packer AMI name to create.
Packer will build the AMI by creating an instance off of the source_ami, run any installation scripts (to install JDK, GIT software), and register the image created after this process as a new AMI. Temporary EC2 instance used to create the AMI will then be terminated by Packer.
For Subnet and VPC Id, please use that as per environment.
Run Packer:
Run below command to build AMI:
packer build -var 'ami_name=ami-<USERNAME>' -var 'base_ami=<AMI_ID>' -var 'vpc_id=<VPC_ID>' -var 'subnet_id=<SUBNET_ID>' packer_test.json
Once the command has completed, copy the AMI ID from the output and use that to launch EC2 Instance.
Summary:
Packer is one of most popular tool nowadays used in cloud hosted devops projects. In most of the cases, CI/CD pipeline jobs ( like Jenkins jobs) are being used to invoke packer build.
In a very simple scenario, we can create Jenkins Free Style Project, clone the code from git repo and add the below command under Build/Execute Shell.
/Downloads/packer build -var Build=$Build src/main/resources/packer.json
Build is a string parameter for this parameterized build and packer binary should be installed in Jenkins node where the job will be running ( binary path: /Downloads/packer)
Hope, this blog will give you some starting point to use Packer in your project . :)
Comments