Getting started with Packer
Packer enables you to create virtual machine disk images with parameters specified in a configuration file. Below, we describe how to create a disk image using Packer.
In this scenario, Packer will create and launch a virtual machine with Debian 11 from Cloud Marketplace and nginx web server. Then, it will delete the VM and create an image of its boot disk. After that, the disk will also be deleted.
To create an image:
If you no longer need the image you created, delete it.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
- Install the Yandex Cloud command line interface.
- Create a cloud network with a single subnet in your folder.
- Get an OAuth token
for a Yandex account or an IAM token for a federated account.
Required paid resources
You pay for storing created images (see pricing for Yandex Compute Cloud).
Install Packer
Note
Yandex Cloud requires Packer 1.5 or higher.
Download a Packer distribution and install it by following the instructions on the official website
You can also download a Packer distribution for your platform from a mirrorPATH
variable:
export PATH=$PATH:/path/to/packer
Configure the Yandex Compute Builder plugin
To configure the plugin
-
Create a
config.pkr.hcl
file with the following contents:packer { required_plugins { yandex = { version = ">= 1.1.2" source = "github.com/hashicorp/yandex" } } }
-
Install the plugin:
packer init <config.pkr.hcl_file_path>
Result:
Installed plugin github.com/hashicorp/yandex v1.1.2 in ...
Prepare the image configuration
- Get the folder ID by running the
yc config list
command. - Get the subnet ID by running the command
yc vpc subnet list
. - Create a JSON file with any name, like
image.json
. This will be your template. Enter the following parameters:
{
"builders": [
{
"type": "yandex",
"token": "<OAuth_or_IAM-token>",
"folder_id": "<folder_ID>",
"zone": "ru-central1-a",
"image_name": "debian-11-nginx-{{isotime | clean_resource_name}}",
"image_family": "debian-web-server",
"image_description": "my custom debian with nginx",
"source_image_family": "debian-11",
"subnet_id": "<subnet_ID>",
"use_ipv4_nat": true,
"disk_type": "network-ssd",
"ssh_username": "debian"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"echo 'updating APT'",
"sudo apt-get update -y",
"sudo apt-get install -y nginx",
"sudo su -",
"sudo systemctl enable nginx.service",
"curl localhost"
]
}
]
}
Where:
token
: OAuth token for a Yandex account or an IAM token for a federated account.folder_id
: ID of the folder to create a VM and its image in.subnet_ID
: ID of the subnet to create a VM and its image in.
Learn more about image configuration parameters in the Yandex Compute Builder documentation
Create an image
Build the image using the template:
packer build image.json
Check the image
Make sure the image was created:
- Go to the management console
. - Select Compute Cloud.
- Open Images. Check if the new disk image is there.
Delete the resources you created
If you no longer need the created image, delete it.
Delete the subnet and cloud network if you created them to run this scenario.