Accelerating Deployment: Creating HashiCorp Nomad Machine Images on Vultr Cloud via Packer
Tags: nomad, packer, infrastructureascode, devops
Published: 2024-03-03 06:16:12


![packer vultr logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je5trq6nqroqvq4mhr3u.png)


Immutable infrastructure creation in the cloud requires on-demand machine images. Whenever we need to make changes to the system, the old image is torn down and a new one is deployed. Packer is a tool provided by HashiCorp to build machine images easily, which can then be used to deploy cloud VMs. In this post, we will see how we can create a machine image in Vultr Cloud using Packer.

[Vultr](https://www.vultr.com/?ref=9581131) is a cloud computing platform which enables us to deploy virtual machines and various cloud services like loadbalancers, CDNs etc. . In Vultr, the machine image is called snapshots. We will install Nomad and Docker within the snapshot. Later, when a cluster is created, this snapshot ID can be utilized to swiftly spawn the cluster with all the packages pre-installed.

Firstly, let us install packer using hashicorp [documentation](https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli#installing-packer). Once packer is setup, we need to create a [vultr](https://www.vultr.com/?ref=9581131) account. Vultr cloud is providing free $250 credits for first time signup. Generate api key from dashboard and keep it safe, as it is required for creating the snapshot.

Once both packer and vultr is ready, let us use the script and config file. The source code is available [here](https://github.com/justinepdevasia/nomad-terraform-vultr-cloud/tree/main/packer).

Two files are used in creating the image,

1. [setup.sh](https://github.com/justinepdevasia/nomad-terraform-vultr-cloud/blob/main/packer/setup.sh) - contains some basic shell script to install docker and nomad in an ubuntu machine.
2. [ubuntu.pkr.hcl](https://github.com/justinepdevasia/nomad-terraform-vultr-cloud/blob/main/packer/ubuntu.pkr.hcl) - packer template to build the image

Let us go through the contents of ubuntu.pkr.hcl file.

```hcl
 variable "vultr_api_key" {
   type      = string
   default   = "${env("VULTR_API_KEY")}"
   sensitive = true
 }

 packer {
   required_plugins {
     vultr = {
       version = ">=v2.3.2"
       source = "github.com/vultr/vultr"
     }
   }
 }

 source "vultr" "ubuntu-nomad" {
   api_key              = "${var.vultr_api_key}"
   os_id                = "2179"
   plan_id              = "vc2-1c-2gb"
   region_id            = "bom"
   snapshot_description = "Ubuntu 23.04 Nomad ${formatdate("YYYY-MM-DD hh:mm", timestamp())}"
   ssh_username         = "root"
   state_timeout        = "25m"
 }

 build {
   sources = ["source.vultr.ubuntu-nomad"]

   provisioner "shell" {
     script = "setup.sh"
   }
 }
```

- The variable **vultr_api_key** is used to connect with vultr cloud securely. It acquires the value from **VULTR_API_KEY** env variable.

- The above `packer` stanza download the correct vultr plugin to use in the machine image creation.

- The `source` stanza contains information about the cloud provider, the region in which image required, base os used etc..

- The `build` stanza connects to a source and use a provisioner to build the image. Here it execute the build process by running the setup.sh file provided by us.

Now let us see how to run this setup to build our customized os image.

1. Clone the git repo https://github.com/justinepdevasia/nomad-terraform-vultr-cloud

2. open terminal inside the repo and ``cd packer``

3. run command ``export VULTR_API_KEY="your-vultr-api-key"``

4. run command ``packer init .``

5. run command ``packer build .``


![packer build ongoing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbdmygcc5zwgfctgd338.png)


This will trigger a packer build session, the application will connect to vultr cloud, spawn a temporary vm and install the packages. Once setup.sh file is fully executed, snapshot creation is triggered and we get back the snapshot id.


![packer build completion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2e5xyauh05ui47l7vfv.png)


This snapshot id can be used to spawn new vms with all the packages, in this case nomad and docker in built.
Finally we can see the created snapshot in Vultr cloud dashboard.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k364ufke0pc3dbm9l5sc.png)


Packer has really enabled the creation of machine images a smooth and enjoyable process. Combining it with the simplicity and ease of use of [vultr](https://www.vultr.com/?ref=9581131), we get a powerful system for automation of cloud VM image creation. With this approach we can pre-install any package in a snapshot and spawn the VMs without any additional installation after launch.

In the upcoming article, we will discuss how to use this snapshot to deploy a Nomad cluster using Terraform.