OpsInit: Packer On AWS

Reading Time: 3 minutes

What is Packer?

Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration.

Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel. Packer does not replace configuration management like chef or Puppet. In fact, when building images, Packer is able to use tools like Chef or Puppet to install software onto the image.

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 images format changes for each platform. AMI’s for EC2, VMDK/VMX files for VMware etc.


What Packer Does?

  1. Super fast infrastructure deployment: Packer images allow you to launch completely provisioned and configured machines in seconds, rather than several minutes or hours. This benefits not only production but development as well since development virtual machines can also be launched in seconds, without waiting for a typically much longer provisioning time.
  2. Multi provider-portability: Because Packer creates identical images for multiple platforms, we can run production in AWS, staging/QA in a private cloud like OpenStack etc. Each environment is running an identical machine image which gives ultimate portability.
  3. Improved stability: Packer installs and configures all the software for a machine at the time the image is built. It caught the bugs early rather than delaying for several minutes after a machine is launched.
  4. Greater Testability: After the image is built it can be quickly launched and smoke tested to verify that everything is working fine. If well tested, any machine launched from that image will function properly.

What are the benefits of image creation through packer?

  • Continuous Delivery: Packer is lightweight, portable and command-line driven. This makes it the perfect tool to put it in the middle of your continuous delivery pipeline. Packer can be used to generate new machine images for multiple platforms on every change to Chef/Puppet. This brings stability and testability to infrastructure changes.
  • Environment(Dev/Prod) Parity: Packer helps to keep as possible as similar images for different environments like staging, production, and development. It can generate images for different platforms at the same time.
  • Demo Creation: Packer is perfect for creating disposable product demos. As our software changes, we can automatically create appliances with the software pre-installed.

Packer Architecture






Packer Implementation.

//create a json file which packer will validate and build.

"variables": {
"aws_access_key": "{{env `AWS_ACCESS_KEY`}}",
"aws_secret_key": "{{env `AWS_SECRET_KEY`}}",
"aws_region": "{{env `REGION`}}",
"aws_source_ami": "{{env `AMI`}}",
"aws_instance_type": "t2.medium",
"organization": "{{env `ORGANIZATION`}}",
"ssh_username": "{{env `MAIN_USER`}}"
"builders": [{
"name": "knoldus-blog",
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "{{user `aws_region`}}",
"source_ami": "{{user `aws_source_ami`}}",
"instance_type": "{{user `aws_instance_type`}}",
"ami_name": "{{user `organization`}}-blog",
"ssh_username": "{{user `ssh_username`}}",
"ssh_pty": "true",
"tags": {
"Name": "{{user `orgazination`}}-blog",
"Role": "Packer Image",
"BuildDate": "{{isotime}}"
"provisioners": [{
"type": "shell",
"script": "demo-script.sh",
"environment_vars": [
"AWS_ACCESS_KEY={{user `aws_access_key`}}",
"AWS_SECRET_KEY={{user `aws_secret_key`}}",
"AWS_DEFAULT_REGION={{user `aws_region`}}"
view raw contennt.json hosted with ❤ by GitHub

// export all the ENV variables for this json file and execute this file via the command:

packer validate example.json

once template validation is successful.

//build packer image via the command:

packer build example.json


Builders Inside Packer json file

Builders are responsible for creating machines and generating images from them for various platforms. For example, there are separate builders for EC2, VMware etc.

Packer has many builders by default and they can also be extended to add new builders.

Provisioners Inside Packer json file

Provisioners are builtin and third Party software to install and configure the machine image after booting. Provisioners prepare the system for use, possibilities for provisioners are endless. Common use cases for provisioners include:

  • installing packages
  • patching the kernel
  • downloading application code and
  • creating users.

Provisioners supported by Packer are:

  1. Ansible
  2. Shell.
  3. Chef.
  4. Salt.
  5. PowerShell.
  6. Windows cmd and
  7. File.







Written by 

Neelam is a software consultant with experience of more than 6 months. She likes to keep up with the trending technologies. She is familiar with languages such as C, C++, Java, Scala, Angular 4 and is currently working on lagom with java. Her hobbies includes playing Table Tennis and listening music.