This topic describes how to configure the basic DevOps parameters of a custom image by using Packer in Alibaba Cloud ECS.

Tag a custom image

  • Field name: tags{"key":"value"}.
  • Applicable scenario: If you have created multiple custom images, you can tag such items as image versions and applications for easy image management and retrieval. Alicloud Image Builder provides the tags parameter. By default, the generated images contain Alibaba Cloud ECS tags. For more information, see Limits.
  • Configuration: When you query images either through the ECS console or by calling the DescribeImages API action, the image tags are displayed together with the target images. These images can also be filtered by tags. Image tags and Terraform can then help you standardize an enterprise-class DevOps process. We recommend that you read Alibaba Cloud DevOps tutorials. For information about Terraform and Packer, see Continuous Delivery.
  • Example: The following configuration file contains the generated image and the corresponding snapshot, each of which is attached with the version=v1.0.0 tag and the app=web tag:
    {
      "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",
        "tags": {
          "version": "v1.0.0",
          "app": "web"
        }
      }]
    }

Build an image by only using a system disk snapshot

  • Field name: image_ignore_data_disks.
  • Data type: Boolean.
  • By default, Packer directly creates images based on ECS instances. If the target ECS instances have data disks, the corresponding images will include data disk snapshots. To build an image from an instance that has data disks, you can use either of the following methods:
    • Method 1: Set the data disk parameters by using the image_disk_mappings parameter. For more information, see Alicloud Image Builder.
    • Method 2: Select an instance type that has data disks by default. However, note that data disks are typically local disks, for example, ecs.d1ne.2xlarge. Local disks cannot be used to create snapshots. Therefore, you cannot directly create images by using such instances.
  • Configuration: If you need to select an instance type that has data disks by default, but you do not want to include these data disks during image creation, you can set "image_ignore_data_disks": "true" in the configuration file to create a system disk image.

Set a snapshot timeout value

  • Field name: wait_snapshot_ready_timeout.
  • Data type: Integer.
  • Default value: 3600s.
  • Applicable scenario: Images are created based on snapshots. The time required to create a snapshot depends on the disk size.
  • Configuration: If the disk size is too large and causes a timeout error, run the wait_snapshot_ready_timeout command to set a higher timeout value.

Connect to an instance by using a private IP address

  • Field name: ssh_private_ip.
  • Data type: Boolean.
  • Applicable scenario: By default, Packer create an EIP and attach this EIP to the target instance. Then, Packer connects to the instance to install software or run commands by using the public IP address corresponding to the EIP. If you use a private IP address to connect to the instance directly, you can disassociate the EIP from the instance.
  • Configuration: After you set "ssh_private_ip": "true", Packer does not assign an EIP or a public IP address, but connect to the instance by using the private IP address.

Stop an instance

  • Field name: disable_stop_instance.
  • Data type: Boolean.
  • Applicable scenario: By default, after Packer runs provisioners, it stops instances and then create images. However, in some scenarios (for example, when you run Sysprep in a Windows instance), the corresponding instance must be in the Running state. For information about the scenarios of Sysprep, see ECS Windows SID modification operating instructions.
  • Configuration: After you set "disable_stop_instance": "true", Packer does not stop the instance, but determines that the command provided in the configuration (provisioners) automatically stops the instance.

Enable WinRM by using the UserData file

  • Field name: user_data_file.
  • Applicable scenario: For security purposes, the Windows Remote Management (WinRM) function is disabled in Windows images by default. However, when you connect to a Windows instance and run commands in the instance, you need to use the WinRM function. To make sure the function is enabled when you create an instance, you can enable WinRM by using the UserData file.
  • Configuration: You can set "user_data_file":"examples.ps1" to specify the path of the UserData file.
  • Example: In the following code, the UserData file is located in the relative path examples/alicloud/basic/winrm_enable_userdata.ps1.
    {
      "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_test",
        "source_image":"win2008r2_64_ent_sp1_zh-cn_40G_alibase_20181220.vhd",
        "instance_type":"ecs.n1.tiny",
        "io_optimized":"true",
        "internet_charge_type":"PayByTraffic",
        "image_force_delete":"true",
        "communicator": "winrm",
        "winrm_port": 5985,
        "winrm_username": "Administrator",
        "winrm_password": "Test1234",
        "user_data_file": "examples/alicloud/basic/winrm_enable_userdata.ps1"
      }],
      "provisioners": [{
        "type": "powershell",
        "inline": ["dir c:\\"]
      }]
    }
    Note
    • In the preceding example, "communicator": "winrm" means you are connected to the instance through WinRM, "winrm_port": 5985 means the communication port is port 5985, "winrm_username": "Administrator" means you are connected to the instance as an administrator, and "winrm_password": "Test1234" is the password used to connect to the instance.
    • The image_force_delete parameter setting means to delete the existing same-name image (if any).

Create an image by using a local ISO file

  • Field name: builders{"type":"qemu"}, post-processors{"type":"alicloud-import"}.
  • Applicable scenario: If the local ISO file runs in a virtualized environment, you can use Packer to create images.
  • Example: If the on-premises environment is QEMU, you can create and import on-premises images by using Packer, as follows:
    1. Use the on-premises virtualized environment or a corresponding Builder, for example, Qemu Builder.
    2. Define Alicloud Import Post-Processor to import the generated local image file to Alibaba Cloud ECS.

If you want to import a custom image, you must first install an on-premises virtualized environment, and then convert the ISO file to an image file (for example, a QCOW2, VHD, or RAW file) supported by Alibaba Cloud. For more information, see Notes for importing images.

What to do next

Learn more about Alicloud Image Builder and Examples.