This topic compares the procedures of how to create a custom image by using an instance and how to build a custom image by using Packer. With Packer, you can automate the creation of ECS images by specifying basic image building information, software, and settings in a JSON configuration file.

What is Packer?

Packer is an open source image building tool created by HashiCorp that automates the creation of images. The image building process is based on a JSON configuration file. This means that you can build an unlimited number of identical images. Packer also facilitates the testing and updating images, which helps to reduce O&M time and lower overall costs. For more information, visit the official Packer website.

Image building methods

The following table describes the image building methods supported by Alibaba Cloud.

Table 1. Image building methods
Method Prerequisite Advantage Disadvantage
Create a custom image by using a snapshot If you have created a system disk snapshot, you can create a custom image by using the ECS console or calling the corresponding API.
  • The operations are easy to complete.
  • Resources are reused.
  • Images are created in real time based on the target production environment.
  • Images are based on Alibaba Cloud public images.
  • Software pre-installation and subsequent settings can be complicated.
  • Correct and consistent manual operations cannot be guaranteed.
  • High maintenance costs may be incurred.
Create a custom image by using an instance If you have created an ECS instance, you can create a custom image by using the ECS console or calling the corresponding API.
../../../../dita-oss-bucket/SP_2/DNECS19100339/EN-US_TP_9698.html#concept_idd_4cm_xdb Packer requests can be identified. User information is verified by using the AccessKey.
  • You do not need to create an instance or a snapshot.
  • The JSON file is reusable and modifiable.
  • Complete operation logs are recorded.
  • Temporary resources are automatically released.
  • ISO files are automatically converted and then immediately imported to Alibaba Cloud ECS.
  • Images are based on Alibaba Cloud public images and local ISO files.
Users are recommended to have prior knowledge of Packer.

Background information

The examples provided in this topic use the following information:

  • Target region: China (Beijing). For more information, see Regions and zones.
  • Operating system: CentOS 7.3 64-bit. The two procedures in this topic use the public image centos_7_03_64_20G_alibase_20170818.vhd as an example. You can query the image ID list of other operating systems by using the ECS console or calling DescribeImages.
  • Custom service: Redis.
  • Whether to reserve temporary resources: No.
Note Resources created in the following procedures (such as an instance, public IP address, and snapshot) may incur fees if retained under your account. We recommend that you release these resources after you have created a custom image to avoid excessive fees.

Create a custom image by using an instance

This example demonstrates how to create a custom image by using the ECS console. The following figure shows the procedure.

  1. Log on to the ECS console.
  2. In the left-side navigation pane, click Instances.
  3. Select the target region.
  4. Purchase an ECS instance. For more information, see Create an instance by using the wizard.

    To minimize charges and simplify the procedure, you can use the following 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 type families.
    • Public Image: CentOS 7.3 64bit.
    • Network: VPC.
    • Security Group: default security group.
    • Network Billing Method: If your instance does not need to access the Internet, you do not need to assign a public IP address. For more information, see Connect to an instance by using the Management Terminal.
  5. Connect to the ECS instance. For more information, see Overview
  6. Run the yum install redis.x86_64 -y command to install Redis.
  7. Select China (Beijing) in the ECS console.
  8. Create an image. For more information, see Create a custom image by using an instance.
  9. Choose Snapshots and Images > Custom Images to view the image status.
  10. Optional. After the image is created, release the temporary resources, such as the created instance and EIP.

Build a custom image by using Packer

This example demonstrates how to create a custom image by using Packer. The following figure shows the procedure.



Prerequisites

Packer is installed. For more information, see Install Packer or ../../../../dita-oss-bucket/SP_2/DNECS19100339/EN-US_TP_9698.html#concept_idd_4cm_xdb.

Procedure

  1. Create an 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 2. Packer parameter description
    Parameter Example value Description
    variables{"variable1":"value"} variables{"access_key":"{{env `ALICLOUD_ACCESS_KEY`}}"} Defines the variables used in image builders. These variables are used to eliminate the disclosure of such information as the AccessKey (access_key and secret_key). The values of variables are input by the system during runtime.
    builders{"type":"value"} builders{"type":"alicloud-ecs"} The image builders defined by Packer. 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 operations that need to be performed in the temporary instance. In this example, Shell Provisioner is used to indicate a shell command (for example, yum install redis.x86_64 -y) that is automatically run to install Redis after you connect to the Linux instance.
    Table 3. Alibaba Cloud parameter description
    Parameter Data type Example value Description
    access_key String LTAInPyXXXXQXXXX Your AccessKey ID. For more information, see Create an AccessKey.
    Note To avoid disclosing the AccessKey of your Alibaba Cloud account, we recommend that you Create a RAM user.
    secret_key String CM1ycKrrCekQ0dhXXXXXXXXXl7yavUT Your AccessKeySecret.
    region String cn-beijing The region to which the target custom image belongs. For more information, see Regions and zones.
    image_name String packer_basic The name of the target custom image. This name must be globally unique.
    source_image String centos_7_03_64_20G_alibase_20170818.vhd The ID of the Alibaba Cloud public image that runs the specified operating system.
    instance_type String ecs.t5-lc1m1.small The type of the temporary instance used to create the custom image. For more information, see Instance type families.
    internet_charge_type String PaybyTraffic The billing method for the Internet bandwidth of the temporary instance. Recommended value: PaybyTraffic.
    io_optimized Boolean true The I/O-optimized attribute of the temporary instance. Recommended value: true.
  2. Run the following command to build an image:
    packer build alicloud.json
    Note It takes a few minutes to build an image. After the image is built, it is displayed in the corresponding Alibaba Cloud region. You can view the image by using the ECS console or calling DescribeImages.

    At the same time the image is built, a log is generated. This log includes all image building actions, such as checking the 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_/nv2r4drx3bs08l6tcx06ndb40000gn/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

What to do next