Getting started with Packer
Packer
In this scenario, Packer will create and launch a virtual machine with Debian 11 from Cloud Marketplace and nginx
To create an image:
- Prepare your cloud.
- Install Packer.
- Prepare the image configuration.
- Create an image.
- Check the 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 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 CLI.
Tip
If you access the cloud using a federated account and want to use the CLI from within your VM, authenticate with 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 for your platform from a mirror
-
Download a Packer distribution 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 that Packer is installed:
packer --version
Result:
Packer v1.11.2
-
Create the
packer
folder. -
Download a Packer distribution 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 path to the
packer
folder to the list. - Click OK.
-
Run a new command line session and make sure that Packer is installed:
packer --version
Result:
Packer v1.11.2
-
Download a Packer distribution 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 that Packer is installed:
packer --version
Result:
Packer v1.11.2
From the HashiCorp website
Download and install a Packer distribution following the instructions on the official 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 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 to it the following configuration:{ "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 to create the VM in. For example:ru-central1-d
.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.
Warning
You cannot use both the provisioner "shell"
and metadata
parameters in the configuration file at the same time.
Learn more about image configuration parameters in the Yandex Compute Builder documentation
Create an image
-
Build the image using the configuration parameters:
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 the image was created:
- Go to the management console
. - Select Compute Cloud.
- Open the
Images section. Check if the new disk image is there.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder 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
If you no longer need the created image, delete it.
Delete the subnet and cloud network if these were created specifically to complete the guide.