Getting started with Packer
Packer lets you create virtual machine images with parameters specified in the 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 9 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 created image, delete it.
Before you start
Before deploying your applications, sign up for Yandex.Cloud and create a billing account:
- Go to the management console. Then log in to Yandex.Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one.
If you have an active billing account, you can create or select a folder to run your VM in from the Yandex.Cloud page.
Learn more about clouds and folders.
- Install the Yandex.Cloud command line interface.
- Create a cloud network with one subnet in your folder.
- Get an OAuth token.
Required paid resources
You pay for storing created images (see pricing for Yandex Compute Cloud).
Install Packer
Note
Yandex.Cloud requires Packer 1.5 and higher.
Download and install Packer by following the instructions on the official website.
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 token>",
"folder_id": "<folder ID>",
"zone": "ru-central1-a",
"image_name": "debian-9-nginx-{{isotime | clean_resource_name}}",
"image_family": "debian-web-server",
"image_description": "my custom debian with nginx",
"source_image_family": "debian-9",
"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"
]
}
]
}
Create the image
Build the image using the template:
$ packer build image.json
Check the image
Make sure the image was created:
- Go to management console.
- Open Compute Cloud.
- Open Images. Check if the new disk image is there.
Delete the created resources
If you no longer need the created image, delete it.
Delete the subnet and cloud network if you created them specifically for this scenario.