Create an ECS custom image using Packer
Packer is a lightweight tool for creating machine images that runs on all major operating systems. This tutorial shows you how to build an ECS custom image by creating a Packer template file in JSON format.
About this tutorial
The steps and examples in this tutorial are available in Alibaba Cloud Shell. Open this tutorial in Cloud Shell to quickly build an ECS custom image from a Packer template file.
Install Packer
Download the latest version of Packer for your operating system from the Packer website. You can install Packer using the following example or by following the official Packer installation guide.
mkdir -p $HOME/bin
curl https://shell-image-softwares-cn-shanghai.oss-cn-shanghai.aliyuncs.com/packer/packer_1_3_5.tar.gz | tar zxv -C $HOME/binDefine a Packer template
To create a custom image with Packer, you must first create a template file in JSON format. In the template file, you can specify the Alicloud Image Builder and Provisioners. Packer provides multiple provisioners to configure how content is generated for the custom image.
This tutorial uses the common Shell provisioner as an example and defines a Packer template named alicloud.json with the following content:
{
"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": [
"yum install redis.x86_64 -y"
]
}]
}Run the cat alicloud.json command to view the template details.
This example creates a custom image that includes Redis.
Customize your template file with the following parameters:
Parameter name | Description |
access_key | Your AccessKey ID. |
secret_key | Your AccessKey secret. |
region | The region where temporary resources are created to build the custom image. |
image_name | The name of the custom image. |
source_image | The name of the base image. You can find this name in the list of Alibaba Cloud public images. |
instance_type | The instance type for the temporary instance that is created to build the image. |
internet_charge_type | The billing method for the public bandwidth of the temporary instance. |
provisioners | The type of Packer provisioner used to create the custom image. |
Create a custom image using Packer
Before you create an ECS custom image with the Packer template file, ensure that you have the required permissions for ECS operations. If you have the required permissions, run the following command in Alibaba Cloud Shell to build the image.
packer build alicloud.jsonThe following is a sample output:
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-2ze12578be1oa4ovs6r9More information
For more information, see the packer-provider repository for Alibaba Cloud on GitHub.
To learn more about using Packer, see the official Packer documentation.