Getting started with Packer
Packer
In this scenario, Packer will create and launch a Debian 11 virtual machine from Cloud Marketplace, with a nginx
To create an image:
- Get your cloud ready.
- Install Packer.
- Prepare the image configuration.
- Create an image.
- Check the image.
If you no longer need the image you created, delete it.
Get your cloud ready
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 Yandex Cloud 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.
Configure the environment and infrastructure
-
Install the Yandex Cloud command line interface (CLI).
Tip
If you access the cloud with a federated account and want to use the CLI from within your VM, get authenticated in the CLI as a service account.
-
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
The cost of creating a disk image using Packer includes:
- Fee for storing created images (see Yandex Compute Cloud pricing).
- Fee for VM computing resources (see Yandex Compute Cloud pricing).
Install and configure Packer
Warning
Yandex Cloud requires Packer 1.5 or higher.
Do not use popular package managers, such as Homebrew or APT, to install Packer. Their repositories may contain obsolete versions.
You can install Packer from a mirror or the HashiCorp website:
From a mirror
Install a Packer distribution kit for your platform from a mirror
-
Download a Packer distribution kit from the mirror
and extract it into thepacker
directory:mkdir packer wget https://hashicorp-releases.yandexcloud.net/packer/1.11.2/packer_1.11.2_linux_amd64.zip -P ~/packer unzip ~/packer/packer_1.11.2_linux_amd64.zip -d ~/packer
This example uses version
1.11.2
. For the latest Packer version, see the mirror . -
Add Packer to the
PATH
variable:-
Add the following line to the
.profile
file:export PATH="$PATH:/home/<username>/packer"
-
Save your changes.
-
Restart the shell:
exec -l $SHELL
-
-
Make sure you installed Packer:
packer --version
Result:
Packer v1.11.2
-
Create the
packer
folder. -
Download a Packer distribution kit from the mirror
and extract it into thepacker
folder: -
Add
packer
to thePATH
variable:- Click Start and type Change system environment variables in the Windows search bar.
- Click Environment Variables... at the bottom right.
- In the window that opens, find the
PATH
parameter and click Edit. - Add the
packer
folder path to the list. - Click OK.
-
Run a new command line session and make sure you installed Packer:
packer --version
Result:
Packer v1.11.2
-
Download a Packer distribution kit from the mirror
and extract it into thepacker
directory:mkdir packer curl --location --output ~/packer/packer_1.11.2_darwin_amd64.zip https://hashicorp-releases.yandexcloud.net/packer/1.11.2/packer_1.11.2_darwin_amd64.zip unzip ~/packer/packer_1.11.2_darwin_amd64.zip -d ~/packer
This example uses version
1.11.2
. For the latest Packer version, see the mirror . -
Add Packer to the
PATH
variable:echo 'export PATH="$PATH:$HOME/<username>/packer"' >> ~/.bash_profile source ~/.bash_profile
-
Restart the shell:
exec -l $SHELL
-
Make sure you installed Packer:
packer --version
Result:
Packer v1.11.2
From the HashiCorp website
Download and install a Packer distribution kit following this guide on the Packer website
Configure the Yandex Compute Builder plugin
To configure the plugin
-
Create a file named
config.pkr.hcl
with the following contents:packer { required_plugins { yandex = { version = ">= 1.1.2" source = "github.com/hashicorp/yandex" } } }
-
Install the plugin:
packer init <path_to_config.pkr.hcl>
Result:
Installed plugin github.com/hashicorp/yandex v1.1.2 in ...
Prepare the image configuration
-
Get the folder ID.
-
Get the subnet ID and the availability zone it resides in.
-
Prepare the subnet ID by running the
yc vpc subnet list
command. -
Create a JSON file with any name, e.g.,
image.json
. Add this configuration to it:{ "builders": [ { "type": "yandex", "token": "<OAuth_token_or_IAM_token>", "folder_id": "<folder_ID>", "zone": "<availability_zone>", "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:
<availability_zone>
: Availability zone where you are going to create the VM, e.g.,ru-central1-d
.token
: OAuth token for a Yandex account or an IAM token for a federated account.folder_id
: ID of the folder where you are going to create the VM and its image.subnet_id
: ID of the subnet where you are going to create the VM and its image.
Warning
You cannot use both the provisioner "shell"
and metadata
parameters in the configuration file at the same time.
You can learn more about image configuration properties in this Yandex Compute Builder overview article
Create an image
-
Build the image using the configuration properties:
packer build image.json
-
Wait for the build to complete:
... ==> Wait completed after 2 minutes 43 seconds ==> Builds finished. The artifacts of successful builds are: --> yandex: A disk image was created: debian-11-nginx-2024-08-26t15-30-39z (id: fd82d63b9bgc********) with family name debian-web-server
Check the image
Make sure you created the image:
- Navigate to the management console
. - Select Compute Cloud.
- Open the
Images section. Make sure the new disk image is there.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
Run this command:
yc compute image list
Result:
+----------------------+--------------------------------------+-------------------+----------------------+--------+
| ID | NAME | FAMILY | PRODUCT IDS | STATUS |
+----------------------+--------------------------------------+-------------------+----------------------+--------+
| fd82d63b9bgc******** | debian-11-nginx-2024-08-26t15-30-39z | debian-web-server | f2eerqfup7lg******** | READY |
+----------------------+--------------------------------------+-------------------+----------------------+--------+
Delete the resources you created
If you no longer need the image you created, delete it.
Delete the subnet and cloud network if you created them specifically to follow this tutorial.