Updated on 20.4.2023

UpCloud Packer Builder

Hashicorp Packer logo

Packer is an easy-to-use automation solution for creating any type of machine images. It embraces modern configuration management by allowing automated software installation and setup within Packer-built images. UpCloud Packer builder integrates Packer with our Cloud Servers and makes creating private templates fast!

Packer plugin

UpCloud Packer builder is a plugin for Packer to simplify template configuration and make deploying custom Cloud Servers quick and easy.

Packer can be downloaded for most operating systems as well as installed using common package managers. To install the precompiled binary, you will need to download the appropriate package for your OS. On most popular Linux distributions, you can install Packer using your package manager after adding their repository.

# Debian and Ubuntu
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository \
"deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install packer
# CentOS
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo \
https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install packer

The Packer builder leverages the UpCloud Go API to interface with the UpCloud API. You will need to provide a username and password with the access rights to the API functions to authenticate. We recommend setting up a new workspace member account with only the API privileges for security purposes. You can do this at your UpCloud Control Panel. Learn more about creating API credentials at our guide for getting started with UpCloud API.

Enter the API user credentials in your terminal with the following two commands. Replace the username and password with your user details.

export PKR_VAR_UPCLOUD_USERNAME=username
export PKR_VAR_UPCLOUD_PASSWORD=password

You should also save these in your profile file to avoid having to repeat the export command every time you open a new terminal. Simply add the same lines to the ~/.profile or ~/.bashrc file.

Configuring custom images

Packer uses the Hashicorp configuration language (HCL) format for the configuration files to define the template you wish to build.

Here is a sample template, which you can also find on our Packer builder GitHub page in the examples/ directory. It reads your UpCloud API credentials from the environment variables and creates an Ubuntu 20.04 LTS server in the nl-ams1 region.

Once deployed, Packer builds the template by following the provisioner instructions. In this example, Packer updates the server software and adds an SSH key to the root user before creating a custom image based on the Cloud Server installation.

variable "UPCLOUD_USERNAME" {
  type = string
  default = ""
}
variable "UPCLOUD_PASSWORD" {
  type = string
  sensitive = true
  default = ""
}
packer {
    required_plugins {
        upcloud = {
            version = ">=v1.0.0"
            source = "github.com/UpCloudLtd/upcloud"
        }
    }
}

source "upcloud" "test" {
  username = "${var.UPCLOUD_USERNAME}"
  password = "${var.UPCLOUD_PASSWORD}"
  zone = "nl-ams1"
  storage_name = "ubuntu server 20.04"
  template_prefix = "ubuntu-server"
}

build {
  sources = ["source.upcloud.com"]
  provisioner "shell" {
    inline = [
      "apt-get update",
      "apt-get upgrade -y",
      "echo 'ssh-rsa-key' | tee /root/.ssh/authorized_keys"
    ]
  }
}

You will need to provide a username and a password with the access rights to the API functions to authenticate. We recommend setting up a new Member account with only the API privileges for security purposes. You can do this at your UpCloud Control Panel in the My Account menu under the User Accounts tab. Check out our getting started with the UpCloud API tutorial to learn more.

Getting started

Packer is a great way to standardise your cloud deployments and save time! If you are new to Packer, make sure to head over to our tutorial on how to create custom images using the UpCloud Packer builder.

UpCloud Terraform Provider

Terraform is a popular open-source infrastructure-as-code software tool created by HashiCorp. It allows users to define infrastructure as code using a simple, human-readable language to safely and predictably manage cloud infrastructure by codifying APIs into declarative configuration files.

Get started with UpCloud Command-line Interface

UpCloud Command-Line Interface

UpCloud Command-Line Interface, or UpCloud CLI for short, is a text-based user interface to UpCloud’s Infrastructure-as-a-service. It provides a fast command-line tool for accessing and managing your UpCloud resources. Save valuable time with quick commands always right at your fingertips! UpCloud CLI UpCloud CLI allows you to control your Cloud Servers, storage and networking from […]

Back to top