This topic describes the benefits of using Packer to create custom images. Packer is an easy-to-use tool that automates the creation of images. To use Packer to create a custom image, you only need to specify the basic image information, the software to be installed, and the relevant configurations in a JSON template.

Prerequisites

An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.

Background information

Packer is a tool provided by HashiCorp to automate the creation of images. A JSON template is used to ensure that the images created each time are identical. Packer simplifies image testing and updating and minimizes the image O&M and management costs. For more information, visit the official Packer website.

Operation conditions

This topic highlights the benefits of Packer in DevOps scenarios by comparing the procedure to create a custom image from an ECS instance and the procedure to create a custom image by using Packer. The examples provided in this topic are based on the following conditions:

  • Region: China (Beijing). For more information, see Regions and zones.
  • Operating system: CentOS 7.3 64-bit. The public image centos_7_03_64_20G_alibase_20170818.vhd is used in the examples. You can view the image lists of other operating systems in the ECS console or by calling the DescribeImages operation.
  • Custom service: Redis.
  • Whether to retain temporary resources: No.
Note Paid resources such as instances, public IP addresses, and snapshots will be created in the following procedures. To avoid additional fees, we recommend that you release these resources after custom images are created.

Create a custom image from an ECS instance

This example shows how to create a custom image from an ECS instance by using the ECS console.

  1. Log on to the ECS console.
  2. In the left-side navigation pane, choose Instances & Images > Instances.
  3. In the top navigation bar, select a region.
  4. Create an ECS instance. For more information, see Create an instance by using the provided wizard.
    To minimize charges and simplify the procedure, you can create the instance with the following configurations:
    • Billing Method: Pay-As-You-Go. For more information, see Pay-as-you-go.
    • Instance Type: ecs.t5-lc1m1.small. For more information, see Instance families.
    • Public Image: CentOS 7.3 64-bit.
    • VPC: the default VPC.
    • Security Group: the default security group.
    • Public IP Address: If your instance does not need to access the Internet, you can choose not to assign a public IP address to the instance, and then connect to the instance by using the management terminal VNC. For more information, see Connect to a Linux instance from the console.
  5. Remotely connect to an ECS instance. For more information, see Overview.
  6. Run the yum install redis.x86_64 -y command to install Redis.
  7. Return to the homepage of the ECS console and select the China (Beijing) region.
  8. Create an image from the instance you created. For more information, see Create a custom image from an instance.
  9. In the left-side navigation pane, choose Instances & Images > Images.
  10. On the Images page, view the progress of image creation.
  11. Optional:After the image is created, release temporary resources created in this procedure, including the instance. If an Elastic IP address (EIP) is used, you can also release this EIP.

Create a custom image by using Packer

This example shows how to use Packer to create a custom image. Before you perform the operations, make sure you have installed Packer. For more information, visit Install Packer or see Use Packer to create a custom image. To create a custom image by using Packer, follow these steps:

  1. Create the alicloud.json file that contains the following information:
    {
      "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_03_64_20G_alibase_20170818.vhd",
        "ssh_username":"root",
        "instance_type":"ecs.t5-lc1m1.small",
        "internet_charge_type":"PayByTraffic",
        "io_optimized":"true"
      }],
      "provisioners": [{
        "type": "shell",
        "inline": [
          "sleep 30",
          "yum install redis.x86_64 -y"
        ]
      }]
    }
    Table 1. Packer parameters
    Parameter Example Description
    variables{"variable1":"value"} variables{"access_key":"{{env `ALICLOUD_ACCESS_KEY`}}"} Defines the variables used in the image builder. For more information, visit Builders. These variables are used to prevent information such as AccessKey pairs from being disclosed. The variables use user-defined values at runtime.
    builders{"type":"value"} builders{"type":"alicloud-ecs"} The image builder defined by Packer. For more information, visit Builders. Alibaba Cloud supports alicloud-ecs, also known as Alicloud Image Builder, which is used to create custom images in Alibaba Cloud ECS.
    provisioners{"type":"value"} provisioners{"type":"shell"} The image provisioners defined by Packer to specify the operations that need to be performed on the temporary instance. The shell provisioner is used in this example. After Packer connects to the Linux instance, Packer automatically runs shell commands to install services based on the shell provisioner configuration. For example, Packer runs the yum install redis.x86_64 -y command to install Redis.
    Table 2. Alibaba Cloud parameters
    Parameter Data type Example Description Importance
    access_key String LTAInPyXXXXQXXXX The AccessKey ID of your account. For more information, see Create an AccessKey pair.
    Note To avoid 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.
    High
    secret_key String CM1ycKrrCekQ0dhXXXXXXXXXl7yavUT The AccessKey secret of your account. High
    region String cn-beijing The region in which to create the custom image. For more information, see Regions and zones. High
    image_name String packer_basic The name of the custom image to be created. This name must be globally unique in Alibaba Cloud ECS. Low
    source_image String centos_7_03_64_20G_alibase_20170818.vhd The ID of the Alibaba Cloud public image based on which to create the custom image. The created custom image will contain the same operating system as the public image. High
    instance_type String ecs.t5-lc1m1.small The instance type of the temporary ECS instance used to create the custom image. For more information, see Instance families. Low
    internet_charge_type String PayByTraffic The billing method for network usage of the temporary instance. Recommended value: PaybyTraffic. Low
    io_optimized Boolean true Specifies whether the temporary instance is I/O optimized. Recommended value: true. Low
  2. Run the following command to create a custom image:
    packer build alicloud.json
    Note It may take a few minutes for the image to be created. After the image is created, it is displayed in the image list of the specified Alibaba Cloud region. You can view the image in the ECS console or by calling the DescribeImages operation.

    A log is generated during the image creation process. This log records all the image creation operations, including checking parameters, creating temporary resources, pre-installing software, creating target resources, and releasing temporary resources.

    alicloud-ecs output will be in this color.
    
    ==> alicloud-ecs: Prevalidating image name...
        alicloud-ecs: Found image ID: centos_7_03_64_20G_alibase_20170818.vhd
    ==> alicloud-ecs: Creating temporary keypair: packer_xxx
    ==> alicloud-ecs: Creating vpc
    ==> alicloud-ecs: Creating vswitch...
    ==> alicloud-ecs: Creating security groups...
    ==> alicloud-ecs: Creating instance.
    ==> alicloud-ecs: Allocating eip
    ==> alicloud-ecs: Allocated eip xxx
        alicloud-ecs: Attach keypair packer_xxx to instance: i-xxx
    ==> alicloud-ecs: Starting instance: i-xxx
    ==> alicloud-ecs: Using ssh communicator to connect: ***
    ==> alicloud-ecs: Waiting for SSH to become available...
    ==> alicloud-ecs: Connected to SSH!
    ==> alicloud-ecs: Provisioning with shell script: /var/folders/k_/nv2r4drx3xxxxxxxxxxxndb40000gn/T/packer-shell260049331
        alicloud-ecs: Loaded plugins: fastestmirror
        alicloud-ecs: Determining fastest mirrors
        alicloud-ecs: Resolving Dependencies
        alicloud-ecs: --> Running transaction check
        alicloud-ecs: ---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
        alicloud-ecs: --> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.el7.x86_64
        alicloud-ecs: --> Running transaction check
        alicloud-ecs: ---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
        alicloud-ecs: --> Finished Dependency Resolution
        alicloud-ecs:
        alicloud-ecs: Dependencies Resolved
        alicloud-ecs:
        alicloud-ecs: ================================================================================
        alicloud-ecs:  Package           Arch            Version                  Repository     Size
        alicloud-ecs: ================================================================================
        alicloud-ecs: Installing:
        alicloud-ecs:  redis             x86_64          3.2.12-2.el7             epel          544 k
        alicloud-ecs: Installing for dependencies:
        alicloud-ecs:  jemalloc          x86_64          3.6.0-1.el7              epel          105 k
        alicloud-ecs:
        alicloud-ecs: Transaction Summary
        alicloud-ecs: ================================================================================
        alicloud-ecs: Install  1 Package (+1 Dependent package)
        alicloud-ecs:
        alicloud-ecs: Total download size: 648 k
        alicloud-ecs: Installed size: 1.7 M
        alicloud-ecs: Downloading packages:
        alicloud-ecs: --------------------------------------------------------------------------------
        alicloud-ecs: Total                                              2.2 MB/s | 648 kB  00:00
        alicloud-ecs: Running transaction check
        alicloud-ecs: Running transaction test
        alicloud-ecs: Transaction test succeeded
        alicloud-ecs: Running transaction
        alicloud-ecs:   Installing : jemalloc-3.6.0-1.el7.x86_64                                  1/2
        alicloud-ecs:   Installing : redis-3.2.12-2.el7.x86_64                                    2/2
        alicloud-ecs:   Verifying  : redis-3.2.12-2.el7.x86_64                                    1/2
        alicloud-ecs:   Verifying  : jemalloc-3.6.0-1.el7.x86_64                                  2/2
        alicloud-ecs:
        alicloud-ecs: Installed:
        alicloud-ecs:   redis.x86_64 0:3.2.12-2.el7
        alicloud-ecs:
        alicloud-ecs: Dependency Installed:
        alicloud-ecs:   jemalloc.x86_64 0:3.6.0-1.el7
        alicloud-ecs:
        alicloud-ecs: Complete!
    ==> alicloud-ecs: Stopping instance: i-xxx
    ==> alicloud-ecs: Waiting instance stopped: i-xxx
    ==> alicloud-ecs: Creating image: packer_basic
        alicloud-ecs: Detach keypair packer_xxx from instance: i-xxx
    ==> alicloud-ecs: Cleaning up 'EIP'
    ==> alicloud-ecs: Cleaning up 'instance'
    ==> alicloud-ecs: Cleaning up 'security group'
    ==> alicloud-ecs: Cleaning up 'vSwitch'
    ==> alicloud-ecs: Cleaning up 'VPC'
    ==> 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-bp67acfmxazb4ph***