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. You only need to specify the basic information of the instance, the software to be installed, and the relevant configurations in a JSON template to create a Packer image.

Prerequisites

You must have registered an Alibaba Cloud account before you follow the instructions provided in the tutorial. If not, create a new Alibaba Cloud account first.

Background information

Packer by HashiCorp is a tool that automates 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, minimizing the image O&M and management costs. For more information, see Packer by HashiCorp.

Operating conditions

This topic compares creating a custom image from an ECS instance and creating a custom image by using Packer. It highlights the benefits of Packer in DevOps scenarios. 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 in this topic. 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 Resources such as instances, public IP addresses, and snapshots created in the following procedures incur fees. To avoid additional fees, we recommend that you release these resources after you create a custom image.

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 use the following instance settings:
    • 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.
    • Network Type: VPC.
    • Security Group: the default security group.
    • Network Billing Method: If your instance does not need to access the Internet, you can choose not to assign a public IP address to it, and then connect to the instance by using the Management Terminal. For more information, see Connect to a Linux instance from the console.
  5. Remotely connect to an ECS instance. For more information about detailed steps, 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. For more information, see Create a custom image by using an instance.
  9. In the left-side navigation pane, choose Instances & Images > Images.
  10. On the Images page, view the image creation progress.
  11. Optional:After the image is created, release 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 performing operations in this example, make sure you have installed Packer. For more information, see Install Packer or 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, see builders. These variables are used to prevent information such as AccessKey pairs from being disclosed. The values of variables are specified by the system at runtime.
    builders{"type":"value"} builders{"type":"alicloud-ecs"} The image builder defined by Packer. For more information, see builders. Alibaba Cloud supports alicloud-ecs, also known as Alicloud Image Builder, which is used by Alibaba Cloud ECS to create custom images.
    provisioners{"type":"value"} provisioners{"type":"shell"} The image provisioners defined by Packer to specify operations that need to be performed on the temporary instance. This example uses Shell Provisioner, which indicates that a shell code snippet is used to automatically install Redis after you connect to the Linux instance. An example of such shell code snippet is yum install redis.x86_64 -y.
    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 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. 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 where the custom image is to be created. For more information, see Regions and zones. High
    image_name String packer_basic The name of the custom image you want to create. This name must be globally unique. Low
    source_image String centos_7_03_64_20G_alibase_20170818.vhd The ID of the Alibaba Cloud public image that is used to create a custom image. The created custom image uses the same operating system as the public image. High
    instance_type String ecs.t5-lc1m1.small The 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 Internet bandwidth allocated to the temporary instance. Recommended value: PaybyTraffic. Low
    io_optimized Boolean true 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 to create the image. After the image is created, it is displayed in the image list of the specified Alibaba Cloud region. You can view it by using the ECS console or by calling the DescribeImages operation.

    A log is generated when the image is being created. It records all the image creation operations, including parameter checks, 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-xxx