AI and VoIP Blog

VOIP | AI | Cloud | Kamailio | Open Source


Build Amazon Machine Image (AMI) using Packer


Packer is a tool created by HashiCorp that allows developers and system administrators to create identical machine images for multiple platforms, including Amazon Web Services (AWS), Microsoft Azure, Google Cloud, and VMware. Packer automates the process of creating machine images, making it easier to deploy applications across multiple environments and manage infrastructure as code (IaaC).

What is AMI?
Consider this scenario– You have created a server and installed all the softwares that you need, to test your application. You have spent several hours provisioning this machine. Now your team in another country also wants to test the application. Is there a way to create a new machine without spending several hours again.
Packer to the rescue! Using this tool you can automate the provisioning of a virtual machine by creating a template and adding all the commands that you used to provision the first VM. Packer will launch a VM and run the commands and then create an Image of the VM. This image can be shared with anyone who wants to build the same VM in the cloud. This image is called AMI in the AWS cloud.

Today we will go through the steps to build a Kamailio Amazon Machine Image (AMI) using Packer, following the steps below:

First Install Packer on your machine. Commands for MacOS are given below.

brew tap hashicorp/tap
brew install hashicorp/tap/packer

For other operating systems check the official documentation here.
https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli

Now create a new directory for your Packer configuration files.
Create a new JSON file in the directory you created and name it “kamailio.json”. This file will contain the configuration for building the AMI.

In the kamailio.json file, add the following code:

{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": "",
    "aws_region": "us-east-1",
    "source_ami": "ami-0c55b159cbfafe1f0",
    "instance_type": "t2.micro"
  },

  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "{{user `aws_region`}}",
      "source_ami": "{{user `source_ami`}}",
      "instance_type": "{{user `instance_type`}}",
      "ssh_username": "ubuntu",
      "ami_name": "kamailio-{{isotime \"2006-01-02\"}}",
      "ssh_pty": true,
      "ami_block_device_mappings": [
        {
          "device_name": "/dev/sda1",
          "volume_size": 8,
          "volume_type": "gp2",
          "delete_on_termination": true
        }
      ]
    }
  ],

  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sudo apt-get update",
        "sudo apt-get install kamailio -y"
      ]
    }
  ]
}


This code defines the AWS credentials, the AWS region, the source AMI, the instance type, and the provisioners. The provisioners are responsible for running the commands to install Kamailio on the instance.

If you are wondering what is user keyword, you must define it within the variables section in your template. Then the variable can be accessed using the user keyword in the other blocks within this template. Check this packer documentation for more details.
https://developer.hashicorp.com/packer/docs/templates/legacy_json_templates/user-variables

Now replace the aws_access_key and aws_secret_key values with your AWS access key and secret key. If you want packer to get these values from the environment variables then use the env keyword as shown below.

{
  "variables": {
    "my_secret": "{{env `MY_SECRET`}}"
  }
}

Run the following command to validate the configuration file:

packer validate kamailio.json

If the configuration file is valid, run the following command to build the AMI:

packer build kamailio.json

Packer will start building the AMI. This may take some time depending on your internet speed and the size of the source AMI.
Once the AMI has been built, anyone can launch an EC2 instance using the new AMI. That’s it!

There are many different ways to provision your custom AMI like using a shell script, using Ansible playbook etc. You can check the official documentation for more examples here. https://developer.hashicorp.com/packer/docs/provisioners

Join 753 other subscribers

Leave a comment

Akash Gupta
Senior VoIP Engineer and AI Enthusiast



Discover more from AI and VoIP Blog

Subscribe to get the latest posts sent to your email.



Leave a comment