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 in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - On the Yandex Cloud Billing
page, make sure you have a linked billing account with anACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders.
Configure the environment and infrastructure
-
Create a cloud network with a single subnet in your folder.
-
Depending on the type of account you are working under, you will get:
- OAuth token
for a Yandex account. - IAM token for federated or service accounts.
- OAuth token
-
Make sure your account has enough permissions to create resources in Compute Cloud. The minimum role you need for the folder is
compute.editor
.If working under a service account, assign the
compute.editor
role for the folder to it.If you want to create resources in other Yandex Cloud services, e.g., subnets in VPC, assign the relevant service roles.
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 your 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.
-
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": "<catalog_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:
token
: OAuth token for a Yandex account or an IAM token for a federated or service account.folder_id
: ID of the folder where you are going to create the VM and its image.zone
: Availability zone the VM will be created in, e.g.,ru-central1-d
.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 installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID>
command. You can also set a different folder for any specific command using 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
To stop paying for the resources you created:
- Delete the image you created.
- Delete the subnet and cloud network if created specifically for the purpose of this tutorial.