All Products
Search
Document Center

CloudOps Orchestration Service:ACS-ECS-CreateAndCopyImage

Last Updated:Jan 06, 2025

Template name

ACS-ECS-CreateAndCopyImage

Execute Now

Template description

Creates an Elastic Compute Service (ECS) image and clones the image to other regions. This applies to scenarios in which you need to create ECS images and clone them to other regions, such as system update and backup. When you configure this template, you must specify the following parameters: regionId: the region of the ECS instance for which you want to create a image, instanceId: the ECS instance, targetImageName: the name of the image to be created. You can also specify commandContent to run scripts on the instance. After the image is created, it is cloned to the destination region (targetRegionIds). You can also specify accountIds to clone the image to other Alibaba Cloud accounts. After the template is executed, the IDs of created and cloned images are returned.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Limit

instanceId

The ID of the ECS instance.

String

Yes

regionId

The ID of the region in which the ECS image is to be created.

String

No

{{ ACS::RegionId }}

targetImageName

The name of the ECS image to be created.

String

No

CreateImage_from_{{ACS::ExecutionId}}

commandType

The type of the Cloud Assistant command.

String

No

RunShellScript

commandContent

The Cloud Assistant command.

String

No

""

targetRegionIds

The IDs of the regions to which the ECS image is to be cloned.

List

No

[]

accountIds

The IDs of Alibaba Cloud accounts with which you want to share the custom image.

List

No

[]

scalingConfigurationIds

The IDs of the scaling configurations to be modified.

List

No

[]

launchTemplateNames

The list of launch template names to be updated.

List

No

[]

tags

The tags of the ECS image.

Json

No

[]

rateControl

The rate control settings.

Json

No

{'Mode': 'Concurrency', 'MaxErrors': '0', 'Concurrency': 5}

OOSAssumeRole

The Resource Access Management (RAM) role that is assumed by CloudOps Orchestration Service (OOS).

String

No

""

Output parameters

Parameter

Description

Type

imageIds

List

imageIdAndRegion

List

Permission policy that is required to execute the template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:CopyImage",
                "ecs:CreateImage",
                "ecs:CreateLaunchTemplateVersion",
                "ecs:DeleteLaunchTemplateVersion",
                "ecs:DescribeImages",
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:DescribeLaunchTemplateVersions",
                "ecs:DescribeLaunchTemplates",
                "ecs:DescribeRegions",
                "ecs:ModifyImageSharePermission",
                "ecs:ModifyLaunchTemplateDefaultVersion",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "ess:ModifyScalingConfiguration"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

References

For more information, see ACS-ECS-CreateAndCopyImage.yml at GitHub.

Template content

FormatVersion: OOS-2019-06-01
Description:
  name-en: ACS-ECS-CreateAndCopyImage
  name-zh-cn: the description in Chinese
  en: 'Creates an ECS image and copies the new image to other regions, applicable to scenarios requiring creation and synchronization of ECS images across different regions, such as system updates and backups. During configuration, users need to provide the following required parameter information: Region ID (regionId), which specifies the region of the ECS instance for which the image will be created, ECS Instance ID (instanceId), which identifies the instance to create the image from, and Target Image Name (targetImageName), which names the new ECS image. Optionally, users may execute tasks on the instance via cloud assistant commands (commandContent). The created image will be synchronized to specified target regions (targetRegionIds), and users can also opt to share the image with other Alibaba Cloud accounts (accountIds). Upon execution, the template will return a list of the created and cloned image IDs, facilitating verification of the operations.'
  zh-cn: the description in Chinese
  categories:
    - image_manage
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: the description in Chinese
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  instanceId:
    Label:
      en: InstanceId
      zh-cn: the description in Chinese
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::InstanceId
    AssociationPropertyMetadata:
      RegionId: regionId
  targetImageName:
    Label:
      en: TargetImageName
      zh-cn: the description in Chinese
    Type: String
    Description:
      en: <p class="p">Note:</p> <ul class="ul"> <li class="li">Length is 2~128 English or Chinese characters</li> <li class="li"><font color='red'>must start with big or small letters or Chinese, not http:// and https://. </font></li> <li class="li">Can contain numbers, colons (:), underscores (_), or dashes (-). </li> </ul>
      zh-cn: the description in Chinese  </li> </ul>
    Default: CreateImage_from_{{ACS::ExecutionId}}
  commandType:
    Label:
      en: CommandType
      zh-cn: the description in Chinese
    Type: String
    AllowedValues:
      - RunShellScript
      - RunBatScript
      - RunPowerShellScript
    Default: RunShellScript
  commandContent:
    Description:
      en: <font color='red'><b>Command is not executed when value is empty</b></font>
      zh-cn: the description in Chinese
    Label:
      en: CommandContent
      zh-cn: the description in Chinese
    Type: String
    AssociationProperty: Code
    Default: ''
  targetRegionIds:
    Label:
      en: TargetRegionIds
      zh-cn: the description in Chinese
    Type: List
    AllowedValues:
      - all-regions
      - cn-beijing
      - cn-qingdao
      - cn-zhangjiakou
      - cn-huhehaote
      - cn-hangzhou
      - cn-shanghai
      - cn-shenzhen
      - cn-chengdu
      - cn-hongkong
      - cn-heyuan
      - cn-wulanchabu
      - ap-northeast-1
      - ap-southeast-1
      - ap-southeast-2
      - ap-southeast-3
      - ap-southeast-5
      - ap-south-1
      - us-east-1
      - us-west-1
      - eu-west-1
      - me-east-1
      - eu-central-1
    Default: []
  accountIds:
    Label:
      en: AccountIds
      zh-cn: the description in Chinese
    Type: List
    Default: []
  scalingConfigurationIds:
    Description:
      en: <font color='red'><b>Must correspond to the selected region</b></font>
      zh-cn: the description in Chinese
    Label:
      en: ScalingConfigurationIds
      zh-cn: the description in Chinese
    Type: List
    Default: []
  launchTemplateNames:
    Description:
      en: <font color='red'><b>Must correspond to the selected region</b></font>
      zh-cn: the description in Chinese
    Label:
      en: LaunchTemplateNames
      zh-cn: the description in Chinese
    Type: List
    Default: []
  tags:
    Label:
      en: Tags
      zh-cn: the description in Chinese
    Type: Json
    AssociationProperty: Tags
    AssociationPropertyMetadata:
      ShowSystem: false
    Default: []
  rateControl:
    Label:
      en: RateControl
      zh-cn: the description in Chinese
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 5
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: the description in Chinese
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: whetherToRunCommand
  Action: 'ACS::Choice'
  Description:
    en: Choose next task
    zh-cn: the description in Chinese
  Properties:
    DefaultTask: runCommand
    Choices:
      - When:
          'Fn::Equals':
            - ''
            - '{{ commandContent }}'
        NextTask: createImage
- Name: runCommand
  Action: 'ACS::ECS::RunCommand'
  Description:
    en: Run cloud assistant command on ECS instance
    zh-cn: the description in Chinese
  Properties:
    regionId: '{{ regionId }}'
    instanceId: '{{ instanceId }}'
    commandContent: '{{ commandContent }}'
    commandType: '{{ commandType }}'
  Outputs:
    commandOutput:
      Type: String
      ValueSelector: invocationOutput
- Name: createImage
  Action: 'ACS::ECS::CreateImage'
  Description:
    en: Create new image with the specified image name and instance ID
    zh-cn: the description in Chinese
  Properties:
    regionId: '{{ regionId }}'
    instanceId: '{{ instanceId }}'
    imageName: '{{ targetImageName }}-{{ ACS::CurrentUTCTime }}'
    tags: '{{tags}}'
  Outputs:
    imageId:
      ValueSelector: imageId
      Type: String
- Name: queryAllAvailableRegions
  Action: 'ACS::ExecuteAPI'
  Description:
    en: View all available regions
    zh-cn: the description in Chinese
  Properties:
    Service: ECS
    API: DescribeRegions
    Parameters:
      RegionId: '{{ regionId }}'
  Outputs:
    regionIds:
      Type: List
      ValueSelector: 'Regions.Region[]|.RegionId|select([scan("{{ACS::RegionId}}|test")]|length<1)'
    allRegionsChosen:
      Type: String
      ValueSelector: '.|{{targetRegionIds}}|sort|.[0]'
- Name: whetherCloneToAllRegions
  Action: 'ACS::Choice'
  Description:
    en: Choose next task by targetRegionIds Chosen
    zh-cn: the description in Chinese
  Properties:
    DefaultTask: copyImage
    Choices:
      - When:
          'Fn::Equals':
            - all-regions
            - '{{ queryAllAvailableRegions.allRegionsChosen }}'
        NextTask: copyImageToAllRegions
- Name: copyImage
  Action: 'ACS::ECS::CopyImage'
  OnSuccess: whetherToShareImage
  Description:
    en: Copy image to regions chosen
    zh-cn: the description in Chinese
  Properties:
    regionId: '{{ regionId }}'
    imageId: '{{ createImage.imageId }}'
    targetImageName: '{{ targetImageName }}-{{ ACS::CurrentUTCTime }}'
    targetRegionId: '{{ ACS::TaskLoopItem }}'
  Loop:
    Items: '{{ targetRegionIds }}'
    RateControl: '{{ rateControl }}'
    Outputs:
      imageIdsWithRegion:
        AggregateType: 'Fn::ListJoin'
        AggregateField: imageIdWithRegion
  Outputs:
    imageIdWithRegion:
      ValueSelector: '.|(.imageId),"{{ ACS::TaskLoopItem }}"'
      Type: List
- Name: copyImageToAllRegions
  Action: 'ACS::ECS::CopyImage'
  Description:
    en: Copy image to all available regions
    zh-cn: the description in Chinese
  Properties:
    regionId: '{{ regionId }}'
    imageId: '{{ createImage.imageId }}'
    targetImageName: '{{ targetImageName }}-{{ ACS::CurrentUTCTime }}'
    targetRegionId: '{{ ACS::TaskLoopItem }}'
  Loop:
    Items: '{{ queryAllAvailableRegions.regionIds }}'
    RateControl: '{{ rateControl }}'
    Outputs:
      imageIdsWithRegion:
        AggregateType: 'Fn::ListJoin'
        AggregateField: imageIdWithRegion
  Outputs:
    imageIdWithRegion:
      ValueSelector: '.|(.imageId),"{{ ACS::TaskLoopItem }}"'
      Type: List
- Name: whetherToShareImage
  Action: 'ACS::Choice'
  Description:
    en: Choose next task by accountIds
    zh-cn: the description in Chinese
  Properties:
    DefaultTask: shareImage
    Choices:
      - When:
          'Fn::Equals':
            - []
            - '{{ accountIds }}'
        NextTask: modifyScalingConfiguration
- Name: shareImage
  Action: 'ACS::ExecuteAPI'
  Description:
    en: Shares a custom image to other Alibaba Cloud accounts
    zh-cn: the description in Chinese
  Properties:
    Service: ECS
    API: ModifyImageSharePermission
    Parameters:
      RegionId: '{{ regionId }}'
      ImageId: '{{ createImage.imageId }}'
      AddAccount: '{{ accountIds }}'
- Name: modifyScalingConfiguration
  Action: 'ACS::ExecuteAPI'
  OnError: ACS::NEXT
  Description:
    en: Modify scaling configuration
    zh-cn: the description in Chinese
  Properties:
    Service: ESS
    API: ModifyScalingConfiguration
    Parameters:
      RegionId: '{{ regionId }}'
      ScalingConfigurationId: '{{ ACS::TaskLoopItem }}'
      ImageId: '{{ createImage.imageId }}'
  Loop:
    Items: '{{ scalingConfigurationIds }}'
- Name: updateLaunchTemplate
  Action: 'ACS::ECS::UpdateLaunchTemplate'
  Description:
    en: Update instance launch template
    zh-cn: the description in Chinese
  Properties:
    regionId: '{{ regionId }}'
    imageId: '{{ createImage.imageId }}'
    launchTemplateName: '{{ ACS::TaskLoopItem }}'
  Loop:
    Items: '{{ launchTemplateNames }}'
Outputs:
  imageIds:
    Type: List
    Value: '{{ createImage.imageId }}'
  imageIdAndRegion:
    Type: List
    Value:
      'Fn::If':
        - 'Fn::Equals':
            - all-regions
            - '{{ queryAllAvailableRegions.allRegionsChosen }}'
        - '{{ copyImageToAllRegions.imageIdsWithRegion }}'
        - '{{ copyImage.imageIdsWithRegion }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - regionId
          - instanceId
        Label:
          default:
            zh-cn: the description in Chinese
            en: Select Ecs Instances
      - Parameters:
          - targetImageName
          - tags
        Label:
          default:
            zh-cn: the description in Chinese
            en: Image Configure
      - Parameters:
          - commandType
          - commandContent
        Label:
          default:
            zh-cn: the description in Chinese
            en: Run Command
      - Parameters:
          - targetRegionIds
          - accountIds
        Label:
          default:
            zh-cn: the description in Chinese
            en: Copy Image
      - Parameters:
          - scalingConfigurationIds
          - launchTemplateNames
          - rateControl
          - OOSAssumeRole
        Label:
          default:
            zh-cn: the description in Chinese
            en: Control Options