Jump to content

Packer Tutorial For Beginners – Automate AMI Creation


Recommended Posts

Packer is an opensource VM image automation tool from Hashicorp. It heps you automate the process of Virtual machine image creation on cloud as well as on-prem virtualized environments.

Packer Use Cases

Following are the main use cases for Packer.

  1. Golden Image Creation: With packer, you can add all the configurations required for creating a golden VM image to be used across organizations.
  2. Monthly VM Patching: You can integrate Packer in your monthly VM image patching pipeline.
  3. Immutable Infrastructure: If you want to create an immutable infrastructure using VM images as a deployable artifact, you can use Packer in your CI/CD lifecycle.

Packer Tutorial For Beginners

In this beginner tutorial, we have covered the steps required to get started with packaging AMI on AWS cloud.

Packer Image Creation Workflow

Step 1: Download the latest Packer executable from the Packer downloads page. https://www.packer.io/downloads.html. It is available for Windows, Linux, and other Unix platforms.

packer min

For Linux, you can use get the download link from the download button and download it using wget.

wget https://releases.hashicorp.com/packer/1.7.0/packer_1.7.0_linux_amd64.zip

Step 2: Unzip the downloaded Packer package and set the path variable in ~/.bashrc

export PATH=$PATH:/path/to/packer

Reload bashrc file

source ~/.bashrc

Alternatively, can move the Packer executable to the bin folder. So that it will be available in the path by default.

sudo mv packer /usr/local/bin/

Step 3: Verify packer installation by executing the packer command.

packer version

You should see the output as shown below.

Verifying Packer installation

Building Virtual Machine Image (AMI) Using Packer

Packer configuration templates are written in JSON format.

A template has the following three main parts.

  1. variables – Where you define custom variables.
  2. builders – Where you mention all the required AMI parameters.
  3. provisioners – Where you can integrate a shell script, ansible play, or a chef cookbook for configuring a required application in the AMI.

An example 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-west-1",
    "source_ami": "ami-sd3543ds",
    "instance_type": "t2.medium",
    "ssh_username": "ec2-user",
    "ami_name": "packer-demo {{timestamp}}"
  }],

  "provisioners": [
  {
    "type": "shell",
    "script": "demo-script.sh"
  }
  ]
}

In the above example configuration, we are passing the AWS access keys and secret keys as variables. It is not recommended to use access keys as variables. If you have the credentials in ~/.aws/credentials file, you don’t need to pass access keys as variables.

Also, we have used a shell provisioner which calls a demo-script.sh file.

packer supports the following provisioners.

  1. Ansible
  2. Chef
  3. Salt
  4. Shell
  5. Powershell
  6. Windows cmd
  7. File – For copying file from local directory to VM image.

Building a Packer Template

To build a packer template, you just need to execute the build command with the JSON template.

For example,

packer build apache.json

Before jumping into building the template, let’s look ar some more important concepts and at last we can try out an example with shell provisioner.

Different Scenarios for Using Packer Variables

You can make use of packer variables for dynamic configuration for packaging AMI. Let’s discuss these scenarios one by one.

Using Variable Within Template

The variables block holds all the default variables within a template. Asn example is shown below.

  "variables": {
    "instance_type": "t2.medium",
    "region": "us-west-1"
    }

The declared variables can be accessed in other parts of the template using "{{user `your-variable-name`}}" syntax. An example is shown below.

  "instance_type": "{{user `instance_type`}}",
  "region": "{{user `region`}}"

Using Environment Variables in Templates

Packer lets you use the system environment variables. First, you need to declare the environment variables in the variable section to use them in the other parts of the template.

Let’s say you want to use the SCRIPT_PATH environment variable that holds the path to a shell script that has to be used in the shell provisioner. You can declare that variable as shown below.

  "variables": {
    "script_path": "{{env `SCRIPT_PATH`}}",
  }

After the declaration, you can use the script_path variable in the provisioner as shown below.

  "provisioners": [
  {
    "type": "shell",
    "script": "{{user `script_path` }}/demo-script.sh"
  }
  ]

Using Command-Line Variables

First, you need to declare the variable name in the variable block as shown below.

"app_name": "{{app_name_cmd_var}}"

You can pass variables during the run time using -var flag followed by the variable declaration.

You can use this variable in the template using the normal interpolation we used in the above examples.

For example,

packer build -var 'app_name_cmd_var=apache' apache.json

Using a JSON File

You can pass a JSON file with variables block to the build command as shown below.

packer build -var-file=variables.json apache.json

In the above example, variables.json is the variable file and apache.json is the packer template.

Packaging an Image

In this example, we will bake a t2.micro AMI using a shell provisioner. A shell script which has an update and HTTPD install instruction.

We assume that you have the AWS access keys and region set in the ~/.aws/credentials file.

Here we are going to user Oregon region and a Redhat AMI with AMI id ami-6f68cf0f

Follow the steps given below to set up the project.

1. Create a folder call packer

mkdir packer

2. Create a script file named demo-script.sh and copy the following contents on to it.

#!/bin/bash
sudo yum -y update
sudo yum install -y httpd

The above script just updates the repository and installs httpd.

3. Create a httpd.json file with the following contents.

  {
    "variables": {
      "ami_id": "ami-6f68cf0f",
      "app_name": "httpd"
    },

    "builders": [{
      "type": "amazon-ebs",
      "region": "eu-west-1",
      "source_ami": "{{user `ami_id`}}",
      "instance_type": "t2.micro",
      "ssh_username": "ec2-user",
      "ami_name": "PACKER-DEMO-{{user `app_name` }}",
      "tags": {
          "Name": "PACKER-DEMO-{{user `app_name` }}",
          "Env": "DEMO"

        }
    }],

    "provisioners": [
      {
        "type": "shell",
        "script": "demo-script.sh"
      }
    ]

  }

We have our template ready. Next step is to execute the template to for packaging the AMI with HTTPD installation.

4. Lets validate and inspect our template using the following commands.

packer validate httpd.json
packer inspect httpd.json

Note: If you are using command line variables or a file as a variable, you should pass it while validating it. An example is shown below.

packer validate -var='env=TEST' httpd.json

The above command should validate and inspect without any errors.

5. To build our new AMI, use the following command.

packer build httpd.json

The above command will build a new AMI.

You can also debug the image creation. Check out this thread for packer debugging.

Few Tips

1. You can capture the output of the image build to a file using the following command.

packer build httpd.json 2>&1 | sudo tee output.txt

2. Then you can extract the new AMI id to a file named ami.txt using the following command.

tail -2 output.txt | head -2 | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' > sudo ami.txt

In this packer tutorial for beginners, we covered the basics of image creation. Let us know in the comment section if you face any issues in the setup.

Packer Tutorial For Beginners

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...