Packer is a lightweight open source tool for creating images and runs on commonly used operating systems such as Windows, Linux, and macOS. This topic describes how to install and use Packer to create a custom image.
Background information
Step 1: Install Packer
Step 2: Define a Packer template
If you want to use Packer to create a custom image, you must create a template file in the JSON format. In the template file, you must specify the image builder and provisioner. For more information, visit Alicloud Image Builder and Provisioners. Packer provides a variety of provisioners that allow you to configure the content generation mode for custom images. In this example, a Shell provisioner is used to define a Packer template.
{
"variables": {
"access_key": "{{env `ALICLOUD_ACCESS_KEY`}}",
"secret_key": "{{env `ALICLOUD_SECRET_KEY`}}"
},
"builders": [{
"type":"alicloud-ecs",
"access_key":"{{user `access_key`}}",
"secret_key":"{{user `secret_key`}}",
"region":"cn-beijing",
"image_name":"packer_basic",
"source_image":"centos_7_02_64_20G_alibase_20170818.vhd",
"ssh_username":"root",
"instance_type":"ecs.n1.tiny",
"internet_charge_type":"PayByTraffic",
"io_optimized":"true"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sleep 30",
"yum install redis.x86_64 -y"
]
}]
}
Parameter | Description |
---|---|
access_key | Your AccessKey ID. For more information, see Create an AccessKey pair.
Note To prevent disclosing the AccessKey pair of your Alibaba Cloud account, we recommend
that you create a RAM user and use the credentials of the RAM user to create an AccessKey
pair. For more information, see Create a RAM user and Create an AccessKey pair.
|
secret_key | Your AccessKey secret. For more information, see Create an AccessKey pair. |
region | The region of the temporary instance used to create the custom image. |
image_name | The name of the custom image. |
source_image | The name of the source image used to create the custom image. You can obtain the name from the public image list of Alibaba Cloud. |
instance_type | The instance type of the temporary instance used to create the custom image. |
internet_charge_type | The billing method for network usage of the temporary instance used to create the custom image. |
provisioners | The type of the provisioner used to create the custom image. For more information, visit Provisioners. |
Step 3: Use Packer to create a custom image
Perform the following operations to create a custom image by using the Packer template file that you specified:
- Run the
export ALICLOUD_ACCESS_KEY=<Your AccessKey ID>
command to import your AccessKey ID. - Run the
export ALICLOUD_SECRET_KEY=<Your AccessKey secret>
command to import your AccessKey secret. - Run the
packer build alicloud.json
command to create a custom image.
alicloud-ecs output will be in this color.
==> alicloud-ecs: Prevalidating alicloud image name...
alicloud-ecs: Found image ID: centos_7_02_64_20G_alibase_20170818.vhd
==> alicloud-ecs: Start creating temporary keypair: packer_59e44f40-c8d6-0ee3-7fd8-b1ba08ea94b8
==> alicloud-ecs: Start creating alicloud vpc
---------------------------
==> alicloud-ecs: Provisioning with shell script: /var/folders/3q/w38xx_js6cl6k5mwkrqsnw7w0000gn/T/packer-shell257466182
alicloud-ecs: Loaded plugins: fastestmirror
---------------------------
alicloud-ecs: Total 1.3 MB/s | 650 kB 00:00
alicloud-ecs: Running transaction check
---------------------------
==> alicloud-ecs: Deleting temporary keypair...
Build 'alicloud-ecs' finished.
==> Builds finished. The artifacts of successful builds are:
--> alicloud-ecs: Alicloud images were created:
cn-beijing: m-2ze12578be1oa4ovs6r9