Skip to content

copy_image

copy_image(
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    client_token: Optional[CopyImageClientToken] = None,
    description: Optional[ImageDescriptionRequest] = None,
    encrypted: Optional[Boolean] = None,
    kms_key_id: Optional[KmsKeyId] = None,
    name: Optional[ImageNameRequest] = None,
    source_image_id: Optional[String] = None,
    source_region: Optional[String] = None,
    destination_outpost_arn: Optional[String] = None,
    copy_image_tags: Optional[Boolean] = None,
    tag_specifications: Optional[TagSpecificationList] = None,
    snapshot_copy_completion_duration_minutes: Optional[Long] = None,
    destination_availability_zone: Optional[String] = None,
    destination_availability_zone_id: Optional[String] = None,
    dry_run: Optional[Boolean] = None,
) -> CopyImageResult

Initiates an AMI copy operation. You must specify the source AMI ID and both the source and destination locations. The copy operation must be initiated in the destination Region.

CopyImage supports the following source to destination copies:

  • Region to Region

  • Region to Outpost

  • Parent Region to Local Zone

  • Local Zone to parent Region

  • Between Local Zones with the same parent Region (only supported for certain Local Zones)

CopyImage does not support the following source to destination copies:

  • Local Zone to non-parent Regions

  • Between Local Zones with different parent Regions

  • Local Zone to Outpost

  • Outpost to Local Zone

  • Outpost to Region

  • Between Outposts

  • Within same Outpost

  • Cross-partition copies (use CreateStoreImageTask instead)

Destination specification

  • Region to Region: The destination Region is the Region in which you initiate the copy operation.

  • Region to Outpost: Specify the destination using the DestinationOutpostArn parameter (the ARN of the Outpost)

  • Region to Local Zone, and Local Zone to Local Zone copies: Specify the destination using the DestinationAvailabilityZone parameter (the name of the destination Local Zone) or DestinationAvailabilityZoneId parameter (the ID of the destination Local Zone).

Snapshot encryption

  • Region to Outpost: Backing snapshots copied to an Outpost are encrypted by default using the default encryption key for the Region or the key that you specify. Outposts do not support unencrypted snapshots.

  • Region to Local Zone, and Local Zone to Local Zone: Not all Local Zones require encrypted snapshots. In Local Zones that require encrypted snapshots, backing snapshots are automatically encrypted during copy. In Local Zones where encryption is not required, snapshots retain their original encryption state (encrypted or unencrypted) by default.

For more information, including the required permissions for copying an AMI, see Copy an Amazon EC2 AMI in the Amazon EC2 User Guide.

Parameters:

  • client_token (Optional[CopyImageClientToken], default: None ) –

    Unique, case-sensitive identifier you provide to ensure idempotency of the request. For more information, see Ensuring idempotency in Amazon EC2 API requests in the Amazon EC2 API Reference.

  • description (Optional[ImageDescriptionRequest], default: None ) –

    A description for the new AMI.

  • encrypted (Optional[Boolean], default: None ) –

    Specifies whether to encrypt the snapshots of the copied image.

    You can encrypt a copy of an unencrypted snapshot, but you cannot create an unencrypted copy of an encrypted snapshot. The default KMS key for Amazon EBS is used unless you specify a non-default Key Management Service (KMS) KMS key using KmsKeyId. For more information, see Use encryption with EBS-backed AMIs in the Amazon EC2 User Guide.

  • kms_key_id (Optional[KmsKeyId], default: None ) –

    The identifier of the symmetric Key Management Service (KMS) KMS key to use when creating encrypted volumes. If this parameter is not specified, your Amazon Web Services managed KMS key for Amazon EBS is used. If you specify a KMS key, you must also set the encrypted state to true.

    You can specify a KMS key using any of the following:

    • Key ID. For example, 1234abcd-12ab-34cd-56ef-1234567890ab.

    • Key alias. For example, alias/ExampleAlias.

    • Key ARN. For example, arn:aws:kms:us-east-1:012345678910:key/1234abcd-12ab-34cd-56ef-1234567890ab.

    • Alias ARN. For example, arn:aws:kms:us-east-1:012345678910:alias/ExampleAlias.

    Amazon Web Services authenticates the KMS key asynchronously. Therefore, if you specify an identifier that is not valid, the action can appear to complete, but eventually fails.

    The specified KMS key must exist in the destination Region.

    Amazon EBS does not support asymmetric KMS keys.

  • name (Optional[ImageNameRequest], default: None ) –

    The name of the new AMI.

  • source_image_id (Optional[String], default: None ) –

    The ID of the AMI to copy.

  • source_region (Optional[String], default: None ) –

    The name of the Region that contains the AMI to copy.

  • destination_outpost_arn (Optional[String], default: None ) –

    The Amazon Resource Name (ARN) of the Outpost for the new AMI.

    Only specify this parameter when copying an AMI from an Amazon Web Services Region to an Outpost. The AMI must be in the Region of the destination Outpost. You can't copy an AMI from an Outpost to a Region, from one Outpost to another, or within the same Outpost.

    For more information, see Copy AMIs from an Amazon Web Services Region to an Outpost in the Amazon EBS User Guide.

    Only one of DestinationAvailabilityZone, DestinationAvailabilityZoneId, or DestinationOutpostArn can be specified.

  • copy_image_tags (Optional[Boolean], default: None ) –

    Specifies whether to copy your user-defined AMI tags to the new AMI.

    The following tags are not be copied:

    • System tags (prefixed with aws:)

    • For public and shared AMIs, user-defined tags that are attached by other Amazon Web Services accounts, except tags with the ec2:SharedTag/ prefix. For more information about tag sharing, see Sharing tags in the Amazon EC2 User Guide.

    Default: Your user-defined AMI tags are not copied.

  • tag_specifications (Optional[TagSpecificationList], default: None ) –

    The tags to apply to the new AMI and new snapshots. You can tag the AMI, the snapshots, or both.

    • To tag the new AMI, the value for ResourceType must be image.

    • To tag the new snapshots, the value for ResourceType must be snapshot. The same tag is applied to all the new snapshots.

    If you specify other values for ResourceType, the request fails.

    To tag an AMI or snapshot after it has been created, see CreateTags.

  • snapshot_copy_completion_duration_minutes (Optional[Long], default: None ) –

    Specify a completion duration, in 15 minute increments, to initiate a time-based AMI copy. The specified completion duration applies to each of the snapshots associated with the AMI. Each snapshot associated with the AMI will be completed within the specified completion duration, with copy throughput automatically adjusted for each snapshot based on its size to meet the timing target.

    If you do not specify a value, the AMI copy operation is completed on a best-effort basis.

    This parameter is not supported when copying an AMI to or from a Local Zone, or to an Outpost.

    For more information, see Time-based copies for Amazon EBS snapshots and EBS-backed AMIs.

  • destination_availability_zone (Optional[String], default: None ) –

    The Local Zone for the new AMI (for example, cn-north-1-pkx-1a).

    Only one of DestinationAvailabilityZone, DestinationAvailabilityZoneId, or DestinationOutpostArn can be specified.

  • destination_availability_zone_id (Optional[String], default: None ) –

    The ID of the Local Zone for the new AMI (for example, cnn1-pkx1-az1).

    Only one of DestinationAvailabilityZone, DestinationAvailabilityZoneId, or DestinationOutpostArn can be specified.

  • dry_run (Optional[Boolean], default: None ) –

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Raises:

Examples:

To copy an AMI to another region This example copies the specified AMI from the us-east-1 region to the current region.

>>> client.copy_image(description='', name='My server', source_image_id='ami-5731123e', source_region='us-east-1')
Source code in docs_src/capo_ec2/_services/ec2.py
def copy_image(
    self,
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    client_token: Optional[
        "capo_ec2.types.copy_image_client_token.CopyImageClientToken"
    ] = None,
    description: Optional[
        "capo_ec2.types.image_description_request.ImageDescriptionRequest"
    ] = None,
    encrypted: Optional["capo_ec2.types.boolean.Boolean"] = None,
    kms_key_id: Optional["capo_ec2.types.kms_key_id.KmsKeyId"] = None,
    name: Optional["capo_ec2.types.image_name_request.ImageNameRequest"] = None,
    source_image_id: Optional["capo_ec2.types.string.String"] = None,
    source_region: Optional["capo_ec2.types.string.String"] = None,
    destination_outpost_arn: Optional["capo_ec2.types.string.String"] = None,
    copy_image_tags: Optional["capo_ec2.types.boolean.Boolean"] = None,
    tag_specifications: Optional[
        "capo_ec2.types.tag_specification_list.TagSpecificationList"
    ] = None,
    snapshot_copy_completion_duration_minutes: Optional[
        "capo_ec2.types.long.Long"
    ] = None,
    destination_availability_zone: Optional["capo_ec2.types.string.String"] = None,
    destination_availability_zone_id: Optional[
        "capo_ec2.types.string.String"
    ] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
) -> "capo_ec2.types.copy_image_result.CopyImageResult":
    """<p>Initiates an AMI copy operation. You must specify the source AMI ID and both the source and destination locations. The copy operation must be initiated in the destination Region.</p> <p class="title"> <b>CopyImage supports the following source to destination copies:</b> </p> <ul> <li> <p>Region to Region</p> </li> <li> <p>Region to Outpost</p> </li> <li> <p>Parent Region to Local Zone</p> </li> <li> <p>Local Zone to parent Region</p> </li> <li> <p>Between Local Zones with the same parent Region (only supported for certain Local Zones)</p> </li> </ul> <p class="title"> <b>CopyImage does not support the following source to destination copies:</b> </p> <ul> <li> <p>Local Zone to non-parent Regions</p> </li> <li> <p>Between Local Zones with different parent Regions</p> </li> <li> <p>Local Zone to Outpost</p> </li> <li> <p>Outpost to Local Zone</p> </li> <li> <p>Outpost to Region</p> </li> <li> <p>Between Outposts</p> </li> <li> <p>Within same Outpost</p> </li> <li> <p>Cross-partition copies (use <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateStoreImageTask.html">CreateStoreImageTask</a> instead)</p> </li> </ul> <p class="title"> <b>Destination specification</b> </p> <ul> <li> <p>Region to Region: The destination Region is the Region in which you initiate the copy operation.</p> </li> <li> <p>Region to Outpost: Specify the destination using the <code>DestinationOutpostArn</code> parameter (the ARN of the Outpost)</p> </li> <li> <p>Region to Local Zone, and Local Zone to Local Zone copies: Specify the destination using the <code>DestinationAvailabilityZone</code> parameter (the name of the destination Local Zone) or <code>DestinationAvailabilityZoneId</code> parameter (the ID of the destination Local Zone).</p> </li> </ul> <p class="title"> <b>Snapshot encryption</b> </p> <ul> <li> <p>Region to Outpost: Backing snapshots copied to an Outpost are encrypted by default using the default encryption key for the Region or the key that you specify. Outposts do not support unencrypted snapshots.</p> </li> <li> <p>Region to Local Zone, and Local Zone to Local Zone: Not all Local Zones require encrypted snapshots. In Local Zones that require encrypted snapshots, backing snapshots are automatically encrypted during copy. In Local Zones where encryption is not required, snapshots retain their original encryption state (encrypted or unencrypted) by default.</p> </li> </ul> <p>For more information, including the required permissions for copying an AMI, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/CopyingAMIs.html">Copy an Amazon EC2 AMI</a> in the <i>Amazon EC2 User Guide</i>.</p>

    Args:
        client_token: <p>Unique, case-sensitive identifier you provide to ensure idempotency of the request. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html">Ensuring idempotency in Amazon EC2 API requests</a> in the <i>Amazon EC2 API Reference</i>.</p>
        description: <p>A description for the new AMI.</p>
        encrypted: <p>Specifies whether to encrypt the snapshots of the copied image.</p> <p>You can encrypt a copy of an unencrypted snapshot, but you cannot create an unencrypted copy of an encrypted snapshot. The default KMS key for Amazon EBS is used unless you specify a non-default Key Management Service (KMS) KMS key using <code>KmsKeyId</code>. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIEncryption.html">Use encryption with EBS-backed AMIs</a> in the <i>Amazon EC2 User Guide</i>.</p>
        kms_key_id: <p>The identifier of the symmetric Key Management Service (KMS) KMS key to use when creating encrypted volumes. If this parameter is not specified, your Amazon Web Services managed KMS key for Amazon EBS is used. If you specify a KMS key, you must also set the encrypted state to <code>true</code>.</p> <p>You can specify a KMS key using any of the following:</p> <ul> <li> <p>Key ID. For example, 1234abcd-12ab-34cd-56ef-1234567890ab.</p> </li> <li> <p>Key alias. For example, alias/ExampleAlias.</p> </li> <li> <p>Key ARN. For example, arn:aws:kms:us-east-1:012345678910:key/1234abcd-12ab-34cd-56ef-1234567890ab.</p> </li> <li> <p>Alias ARN. For example, arn:aws:kms:us-east-1:012345678910:alias/ExampleAlias.</p> </li> </ul> <p>Amazon Web Services authenticates the KMS key asynchronously. Therefore, if you specify an identifier that is not valid, the action can appear to complete, but eventually fails.</p> <p>The specified KMS key must exist in the destination Region.</p> <p>Amazon EBS does not support asymmetric KMS keys.</p>
        name: <p>The name of the new AMI.</p>
        source_image_id: <p>The ID of the AMI to copy.</p>
        source_region: <p>The name of the Region that contains the AMI to copy.</p>
        destination_outpost_arn: <p>The Amazon Resource Name (ARN) of the Outpost for the new AMI.</p> <p>Only specify this parameter when copying an AMI from an Amazon Web Services Region to an Outpost. The AMI must be in the Region of the destination Outpost. You can't copy an AMI from an Outpost to a Region, from one Outpost to another, or within the same Outpost.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/ebs/latest/userguide/snapshots-outposts.html#copy-amis">Copy AMIs from an Amazon Web Services Region to an Outpost</a> in the <i>Amazon EBS User Guide</i>.</p> <p>Only one of <code>DestinationAvailabilityZone</code>, <code>DestinationAvailabilityZoneId</code>, or <code>DestinationOutpostArn</code> can be specified.</p>
        copy_image_tags: <p>Specifies whether to copy your user-defined AMI tags to the new AMI.</p> <p>The following tags are not be copied:</p> <ul> <li> <p>System tags (prefixed with <code>aws:</code>)</p> </li> <li> <p>For public and shared AMIs, user-defined tags that are attached by other Amazon Web Services accounts, except tags with the <code>ec2:SharedTag/</code> prefix. For more information about tag sharing, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#sharing-tags">Sharing tags</a> in the <i>Amazon EC2 User Guide</i>.</p> </li> </ul> <p>Default: Your user-defined AMI tags are not copied.</p>
        tag_specifications: <p>The tags to apply to the new AMI and new snapshots. You can tag the AMI, the snapshots, or both.</p> <ul> <li> <p>To tag the new AMI, the value for <code>ResourceType</code> must be <code>image</code>.</p> </li> <li> <p>To tag the new snapshots, the value for <code>ResourceType</code> must be <code>snapshot</code>. The same tag is applied to all the new snapshots.</p> </li> </ul> <p>If you specify other values for <code>ResourceType</code>, the request fails.</p> <p>To tag an AMI or snapshot after it has been created, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateTags.html">CreateTags</a>.</p>
        snapshot_copy_completion_duration_minutes: <p>Specify a completion duration, in 15 minute increments, to initiate a time-based AMI copy. The specified completion duration applies to each of the snapshots associated with the AMI. Each snapshot associated with the AMI will be completed within the specified completion duration, with copy throughput automatically adjusted for each snapshot based on its size to meet the timing target.</p> <p>If you do not specify a value, the AMI copy operation is completed on a best-effort basis.</p> <note> <p>This parameter is not supported when copying an AMI to or from a Local Zone, or to an Outpost.</p> </note> <p>For more information, see <a href="https://docs.aws.amazon.com/ebs/latest/userguide/time-based-copies.html">Time-based copies for Amazon EBS snapshots and EBS-backed AMIs</a>.</p>
        destination_availability_zone: <p>The Local Zone for the new AMI (for example, <code>cn-north-1-pkx-1a</code>).</p> <p>Only one of <code>DestinationAvailabilityZone</code>, <code>DestinationAvailabilityZoneId</code>, or <code>DestinationOutpostArn</code> can be specified.</p>
        destination_availability_zone_id: <p>The ID of the Local Zone for the new AMI (for example, <code>cnn1-pkx1-az1</code>).</p> <p>Only one of <code>DestinationAvailabilityZone</code>, <code>DestinationAvailabilityZoneId</code>, or <code>DestinationOutpostArn</code> can be specified.</p>
        dry_run: <p>Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.</p>

    Raises:
        capo_ec2.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To copy an AMI to another region
        This example copies the specified AMI from the us-east-1 region to the current region.

        >>> client.copy_image(description='', name='My server', source_image_id='ami-5731123e', source_region='us-east-1')
    """

    def _handler(
        req: "OperationRequest[capo_ec2.types.copy_image_request.CopyImageRequest]",
    ) -> OperationResponse["capo_ec2.types.copy_image_result.CopyImageResult"]:
        import capo_ec2._operations.amazon_ec2.copy_image

        output, http_response = (
            capo_ec2._operations.amazon_ec2.copy_image.copy_image(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("CopyImage")
    )
    input_: capo_ec2.types.copy_image_request.CopyImageRequest = {}
    if client_token is None:
        client_token = str(uuid.uuid4())
    input_["client_token"] = client_token
    if description is not None:
        input_["description"] = description
    if encrypted is not None:
        input_["encrypted"] = encrypted
    if kms_key_id is not None:
        input_["kms_key_id"] = kms_key_id
    if name is not None:
        input_["name"] = name
    if source_image_id is not None:
        input_["source_image_id"] = source_image_id
    if source_region is not None:
        input_["source_region"] = source_region
    if destination_outpost_arn is not None:
        input_["destination_outpost_arn"] = destination_outpost_arn
    if copy_image_tags is not None:
        input_["copy_image_tags"] = copy_image_tags
    if tag_specifications is not None:
        input_["tag_specifications"] = tag_specifications
    if snapshot_copy_completion_duration_minutes is not None:
        input_["snapshot_copy_completion_duration_minutes"] = (
            snapshot_copy_completion_duration_minutes
        )
    if destination_availability_zone is not None:
        input_["destination_availability_zone"] = destination_availability_zone
    if destination_availability_zone_id is not None:
        input_["destination_availability_zone_id"] = (
            destination_availability_zone_id
        )
    if dry_run is not None:
        input_["dry_run"] = dry_run

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output