How to create custom images using UpCloud Packer builder
Packer is an easy-to-use automation solution for creating any type of machine image. 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 even faster!
UpCloud Packer builder is a plugin for Packer to simplify template configuration and make deploying custom Cloud Servers quick and easy. In this guide, we’ll show the steps required to install Packer on Linux and how to create your first custom template on UpCloud. Packer is also available for macOS and Windows with their own installation instructions on their download page.
Installing Packer
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 run the following commands to install Packer using your native package manager.
On Debian and Ubuntu, add the HashiCorp signing key and package repository, and then install Packer. The first command installs the tools that these steps need.
sudo apt update && sudo apt install -y gpg wget lsb-release
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packerOn CentOS Stream, AlmaLinux and Rocky Linux, add the HashiCorp repository with dnf and install Packer from it.
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo dnf -y install packerAfterwards, verify that Packer is working, for example, with the command below.
packer --versionPacker v1.16.2On CentOS Stream 9, AlmaLinux 9 and Rocky Linux 9, run Packer as a regular user, or as root with its full path, /usr/bin/packer. For the root user, packer runs a different program from the cracklib package, which prints 0 0 instead of a version.
The Packer builder leverages the UpCloud Go API to interface with the UpCloud API. There are two methods to authenticate: using an API token (recommended) or using the username and password of a dedicated UpCloud subaccount. We recommend setting up a new subaccount with only the API privileges for security purposes, rather than using your main account credentials. Learn more about tokens in our getting started with UpCloud API guide, or see the HTTP Basic authentication guide for subaccount setup.
Option 1: Using API tokens (Recommended)
API tokens provide a more secure authentication method as they have configurable expiration dates, can be restricted to specific IP addresses, and can be easily revoked if compromised. For full details on creating and managing tokens, see our API Tokens guide.
Once you have created a token through the UpCloud Control Panel, export it as an environment variable:
export UPCLOUD_TOKEN=ucat_Your_API_TokenReplace the token above with your actual UpCloud API token.
Option 2: Using username and password
If you prefer to use the traditional authentication method or don't have access to API tokens yet, 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=passwordYou 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.
Installing Packer is all that you really need to build custom templates on UpCloud. Continue ahead in the next section to get started with configuring your templates.
Configuring templates
Packer uses the Hashicorp configuration language (HCL) format for the configuration files to define the template you wish to build. You can find an example configuration on our GitHub repository at packer-plugin-upcloud.
To start, you might want to make a new directory for arranging your Packer configurations, for example, ~/packer.
mkdir ~/packerNext create a new template with your favourite editor, for example using the following command.
nano ~/packer/upcloud-template.pkr.hclThen copy and paste the example template underneath.
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" "example" {
username = "${var.UPCLOUD_USERNAME}"
password = "${var.UPCLOUD_PASSWORD}"
zone = "nl-ams1"
storage_name = "Ubuntu Server 24.04 LTS (Noble Numbat)"
template_prefix = "ubuntu-server"
}
build {
sources = ["source.upcloud.example"]
provisioner "shell" {
inline = [
"apt-get update",
"apt-get upgrade -y",
"echo 'ssh-rsa-key' | tee /root/.ssh/authorized_keys"
]
}
}The basic template is almost ready to deploy. However, you should take a look at the parameters in the source and build segments.
The username and password are rather self-explanatory and should be the same for every template. Since we already set our actual UpCloud API credentials in the environmental variables, there’s no need to include them in the configuration file. Packer will then find the credentials from your environmental variables at run time and set them in the username and password variables as defined in your configuration.
The important parts are the target zone and the source storage_name. These tell Packer which public template you wish to use as the basis for generating your own and where it should be made available.
Set zone to the location where you wish to deploy Cloud Servers with the custom template, because Packer creates the template in that location. If you want to deploy Cloud Servers from it in other locations too, add a clone_zones list to the source block. Packer then copies the finished template to each of those locations.
clone_zones = ["de-fra1", "fi-hel1"]The currently available locations are the following:
Europe
- Amsterdam
nl-ams1 - Copenhagen
dk-cph1 - Frankfurt
de-fra1 - Helsinki
fi-hel1 - Helsinki
fi-hel2 - London
uk-lon1 - Madrid
es-mad1 - Stavanger
no-svg1 - Stockholm
se-sto1 - Warsaw
pl-waw1
Americas
- Chicago
us-chi1 - New York
us-nyc1 - San Jose
us-sjo1
Asia-Pacific
- Singapore
sg-sin1 - Sydney
au-syd1
The second bit you should select is the public template that will be used to generate your custom template. The example configuration above uses the Ubuntu 24.04 image, but you can use any Linux template you wish available as a public template on UpCloud.
In storage_name, give the template's full name, because a shorter name can also match a different template. For example, ubuntu server 24.04 also matches Ubuntu Server 24.04 LTS (with NVIDIA drivers & CUDA).
With the basic configuration done, the customization to the template can then be added to the provisioners segment. The example provisioner runs the basic update and upgrades commands in the shell.
Additionally, if you want to log into a server deployed with the template, you need to include an SSH key to your root user by replacing the ssh-rsa-key with your public key or provisioning another username. The Packer generates a temporary SSH key while building the template which cannot be used afterwards.
You can find instructions on how to use different types of provisioners to customize your template in the Packer documentation for provisioners.
Once you have made the configurations, save the file and exit the editor.
Building templates using Packer
With the template configuration ready, continue by initialising Packer using the following command.
packer init ~/packer/upcloud-template.pkr.hclThis will tell Packer to prepare to build the template and check for the required plugin. Packer will download the UpCloud Packer builder and save it in the ~/.config/packer/plugins directory. You should see a confirmation such as in the example output below.
Installed plugin github.com/UpCloudLtd/upcloud v1.11.1 in "/home/user/.config/packer/plugins/github.com/UpCloudLtd/upcloud/packer-plugin-upcloud_v1.11.1_x5.0_linux_amd64"With Packer initialised and the UpCloud plugin installed, validate your Packer configuration using the next command.
packer validate ~/packer/upcloud-template.pkr.hclPacker then checks the syntax of your template configuration file and that it can find your credentials. If everything is in order, it confirms that the configuration is valid.
The configuration is valid.You are then ready to build your template. Use the command below to generate your custom template based on the configuration file.
packer build ~/packer/upcloud-template.pkr.hcl==> upcloud.example: Creating temporary ssh key...
==> upcloud.example: Getting storage...
==> upcloud.example: Creating server based on storage "Ubuntu Server 24.04 LTS (Noble Numbat)"...
==> upcloud.example: Server "packer-custom-20261123-124757" created and in 'started' state
==> upcloud.example: Auto-selecting ip '185.20.139.167' as Server IP
==> upcloud.example: Using SSH communicator to connect: 185.20.139.167
==> upcloud.example: Waiting for SSH to become available...
==> upcloud.example: Connected to SSH!
...
==> upcloud.example: Stopping server "packer-custom-20261123-124757"...
==> upcloud.example: Server "packer-custom-20261123-124757" is now in 'stopped' state
==> upcloud.example: Cloning completed...
==> upcloud.example: Creating template for storage "01c71a3b-5f33-4df9-ab78-3a3326f43be9"...
==> upcloud.example: Template for storage "01c71a3b-5f33-4df9-ab78-3a3326f43be9" created...
==> upcloud.example: Stopping server "packer-custom-20261123-124757"...
==> upcloud.example: Deleting server "packer-custom-20261123-124757"...
==> Builds finished. The artifacts of successful builds are:
--> upcloud.example: Storage template created, UUID: 0136f326-2f12-4b38-a0b3-1ab8dee6c492When the deployment process finishes, you should see an output similar to the example above.
Your new custom template is then available to deploy via your UpCloud Control Panel or using any of our cloud management tools. For example, you can deploy the template to a new Cloud Server using Terraform and the following configuration.
resource "upcloud_server" "example" {
hostname = "example"
zone = "nl-ams1"
plan = "PREMIUM-2xCPU-4GB"
metadata = "true"
template {
storage = "0136f326-2f12-4b38-a0b3-1ab8dee6c492"
size = 100
}
network_interface {
type = "public"
}
connection {
host = self.network_interface[0].ip_address
type = "ssh"
user = "root"
private_key = file("~/.ssh/id_rsa")
}
}Find out more about Terraform and how to get started at our other guides.
Note that deploying Custom images built by Packer successfully requires the Metadata service to be enabled. If you do not wish to use it, you can always disable it afterwards.

Conclusions
Congratulations, you should now have your own custom template visible in the Control Panel under Block Storage > Custom images. With the simple configuration process and fast deployment, you can have a purpose-built template ready in minutes.
Test it out by pressing the Deploy button and start a new server from the custom template to verify it was built to your specifications.