All Products
Search
Document Center

Elastic Compute Service:common Alicloud ECS image builderparameters used to implement DevOps

Last Updated:Mar 22, 2024

Packer is an open source tool provided by HashiCorp and is used to create images that have the same configurations across multiple platforms. In DevOps practices, Packer automates the build and deployment processes. Packer allows you to use predefined templates to create portable and reusable base images. This ensures image consistency in development, test, and production environments. This topic describes Alicloud Elastic Compute Service (ECS) parameters that are used to implement DevOps when you use Packer to create custom images in ECS. For example, you can use the parameters to add tags to custom images, create images based only on the system disk, and specify a snapshot timeout period.

Parameter used to add tags to custom images

  • Scenario: If you have multiple custom images, you can add tags to the images for easy management and retrieval. For example, you can add a tag to record the version number of an image or the type of applications that are contained in the image. Alicloud ECS image builder (Alicloud Image Builder) provides the tags parameter. If you specify a tag when you use Packer to create a custom image, the tag is added to the generated image. For more information, see Overview.

  • Packer field: tags{"key":"value"}.

  • Usage: When you query images with tags in the ECS console or by calling the DescribeImages operation, you can view the tags. You can also filter images by tag. Tags added to images can be used together with Terraform to standardize enterprise-level DevOps processes.

  • Example: In the following configurations, the "version":"v1.0.0" and "app":"web" tags are added to the generated image and the corresponding snapshot.

    {
      "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"
        }
      }]
    }

Parameter used to specify whether to create an image based only on a system disk

  • Scenario: By default, Packer creates images directly from ECS instances. If the instances have data disks, the images that are generated based on the instances contain data disk snapshots.

    Note

    You can use one of the following methods to create an instance that has data disks:

    • Method 1: Configure data disk parameters in image_disk_mappings. For more information, see Alicloud ECS in Packer documentation.

    • Method 2: Select an instance type that comes with data disks, such as ecs.d1ne.2xlarge. In most cases, the data disks are local disks, which cannot be used to create snapshots. Consequently, the instance type that comes with data disks cannot be used to create images.

  • Packer field: image_ignore_data_disks. Data type: Boolean.

  • Usage: If you want to create an image from an instance that contains data disks but do not want the image created to contain data disk snapshots, you can add "image_ignore_data_disks": "true" to the configuration file. This way, you can create an image based only on the system disk.

  • Example:

    {
         "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-hangzhou",
           "image_name":"packer_basic",
           "source_image":"aliyun_3_9_x64_20G_alibase_20231219.vhd",
           "associate_public_ip_address":true,
           "ssh_username":"root",
           "instance_type":"ecs.s6-c1m2.large",
           "internet_charge_type":"PayByTraffic",
           "io_optimized":true,
           "skip_image_validation":true,
           "image_disk_mappings": [
            {
               "disk_snapshot_id": "s-bp1xxxxxx",
               "disk_device": "dev/xvdb"
            }],
           "image_ignore_data_disks": "true"
        }
      ]
    }

Parameter used to specify the snapshot timeout period

  • Scenario: Images can be created from snapshots. The duration of creating a snapshot for a disk varies based on the disk size. For a large disk, an extended period of time is required to create a snapshot, which may cause a timeout. To prevent a timeout error, you can set the wait_snapshot_ready_timeout field to increase the timeout period.

  • Packer field: wait_snapshot_ready_timeout. Data type: Integer. Default value: 3600. Unit: seconds.

  • Usage: Set the wait_snapshot_ready_timeout field to 7200 seconds.

  • Example:

    {
         "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-hangzhou",
           "image_name":"packer_basic",
           "source_image":"aliyun_3_9_x64_20G_alibase_20231219.vhd",
           "associate_public_ip_address":true,
           "ssh_username":"root",
           "instance_type":"ecs.s6-c1m2.large",
           "internet_charge_type":"PayByTraffic",
           "io_optimized":true,
           "skip_image_validation":true,
           "wait_snapshot_ready_timeout": 7200
        }
      ]
    }

Parameter used to specify whether to connect to an instance by using a private IP address

  • Scenario: By default, Packer creates an elastic IP address (EIP) and associates the EIP with the temporary ECS instance when Packer creates a custom image. Packer uses the EIP to connect to the instance and then installs software or runs commands on the instance. If Packer can use private IP addresses to connect to the instance, the public IP address is not required.

  • Packer field: ssh_private_ip. Data type: Boolean.

  • Usage: You can set the ssh_private_ip field to true. Then, Packer uses a private IP address to connect to the instance, but does not associate an EIP with or assign a public IP address to the instance.

    Important

    When Packer uses private IP addresses, the machine on which Packer is running must belong to the same vSwitch as the machine that Packer creates.

  • Example:

    {
      "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-hangzhou",
           "image_name":"packer_basic",
           "source_image":"aliyun_3_9_x64_20G_alibase_20231219.vhd",
           "ssh_username":"root",
           "instance_type":"ecs.s6-c1m2.large",
           "internet_charge_type":"PayByTraffic",
           "io_optimized":true,
           "skip_image_validation":true,
           "ssh_private_ip": true,
           "vpc_id":"vpc-xxxx",
           "vswitch_id":"vsw-xxxx"
        }
      ]
    }

Parameter used to specify whether to stop an instance

  • Scenario: By default, after Packer runs provisioners, it stops instances and then creates images from the instances. However, instances must be in the Running state in specific scenarios such as when Windows instances run Sysprep.

    For information about how to use Sysprep, see What do I do if instances cannot access the AD domains of each other or instances cannot be added to the same AD domain at the same time?

  • Packer field: disable_stop_instance. Data type: Boolean.

  • Usage: You can set the disable_stop_instance field to true. Then, Packer does not stop the instance. You must add a command to the configuration (provisioners) to stop the instance or manually stop the instance.

  • Example:

    {
         "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-hangzhou",
           "image_name":"packer_basic",
           "source_image":"aliyun_3_9_x64_20G_alibase_20231219.vhd",
           "ssh_username":"root",
           "instance_type":"ecs.s6-c1m2.large",
           "internet_charge_type":"PayByTraffic",
           "io_optimized":true,
           "skip_image_validation":true,
           "associate_public_ip_address":true,
           "disable_stop_instance": true
        }
      ]
    }

Parameter used to specify the UserData file path to enable WinRM

  • Scenario: For security purposes, the Windows Remote Management (WinRM) feature is disabled for Windows images by default. However, Packer must use the WinRM feature to connect to a Windows instance and run commands on the instance. You can use the UserData file to enable WinRM when you create a Windows instance.

  • Packer field: user_data_file.

  • Usage: Use the "user_data_file":"examples.ps1" configuration to specify the path of the UserData file.

  • Example: The UserData file is stored in the following 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 sample code:

      • "communicator": "winrm" indicates that WinRM is used to connect to the instance.

      • "winrm_port": 5985 indicates that the communication port is port 5985.

      • "winrm_username": "Administrator" indicates that you are connected to the instance as an administrator.

      • "winrm_password": "Test1234" indicates that Password Test1234 is used.

    • "image_force_delete":"true" indicates that existing images are deleted if they have the same name as the image to be created.

Parameters used to create an image based on an on-premises ISO file and import the image to Alibaba Cloud ECS

  • Scenario: If an on-premises ISO file runs in a non-QEMU-based virtualization environment, you can use Packer to create an image based on the file and then import the image to Alibaba Cloud ECS.

  • Packer field: builders{"type":"qemu"},post-processors{"type":"alicloud-import"}.

  • Example: If the on-premises environment is based on QEMU, you can use Packer to create an image and then import the image to Alibaba Cloud ECS. For more information, see Use Packer to create and import an on-premises image, which includes two important steps:

    1. Use an on-premises virtualization environment or a builder, such as QEMU Builder, to create an on-premises image.

    2. Define Alicloud Import Post-Processor to import the generated on-premises image to Alibaba Cloud ECS.

Before you import an ISO file to Alibaba Cloud ECS, you must install an on-premise virtualization environment and then create an image based on the file. The image must be in a format supported by Alibaba Cloud, such as QCOW2, VHD, or RAW. Then, you can import the image to Alibaba Cloud ECS. For more information, see Import an image.

References

For information about more parameters and examples, see Alicloud ECS and Examples.