KMS / Client / tag_resource

tag_resource#

KMS.Client.tag_resource(**kwargs)#

Adds or edits tags on a customer managed key.

Note

Tagging or untagging a KMS key can allow or deny permission to the KMS key. For details, see ABAC for KMS in the Key Management Service Developer Guide.

Each tag consists of a tag key and a tag value, both of which are case-sensitive strings. The tag value can be an empty (null) string. To add a tag, specify a new tag key and a tag value. To edit a tag, specify an existing tag key and a new tag value.

You can use this operation to tag a customer managed key, but you cannot tag an Amazon Web Services managed key, an Amazon Web Services owned key, a custom key store, or an alias.

You can also add tags to a KMS key while creating it ( CreateKey) or replicating it ( ReplicateKey).

For information about using tags in KMS, see Tagging keys. For general information about tags, including the format and syntax, see Tagging Amazon Web Services resources in the Amazon Web Services General Reference.

The KMS key that you use for this operation must be in a compatible key state. For details, see Key states of KMS keys in the Key Management Service Developer Guide.

Cross-account use: No. You cannot perform this operation on a KMS key in a different Amazon Web Services account.

Required permissions: kms:TagResource (key policy)

Related operations

  • CreateKey

  • ListResourceTags

  • ReplicateKey

  • UntagResource

Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency.

See also: AWS API Documentation

Request Syntax

response = client.tag_resource(
    KeyId='string',
    Tags=[
        {
            'TagKey': 'string',
            'TagValue': 'string'
        },
    ]
)
Parameters:
  • KeyId (string) –

    [REQUIRED]

    Identifies a customer managed key in the account and Region.

    Specify the key ID or key ARN of the KMS key.

    For example:

    • Key ID: 1234abcd-12ab-34cd-56ef-1234567890ab

    • Key ARN: arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab

    To get the key ID and key ARN for a KMS key, use ListKeys or DescribeKey.

  • Tags (list) –

    [REQUIRED]

    One or more tags. Each tag consists of a tag key and a tag value. The tag value can be an empty (null) string.

    Warning

    Do not include confidential or sensitive information in this field. This field may be displayed in plaintext in CloudTrail logs and other output.

    You cannot have more than one tag on a KMS key with the same tag key. If you specify an existing tag key with a different tag value, KMS replaces the current tag value with the specified one.

    • (dict) –

      A key-value pair. A tag consists of a tag key and a tag value. Tag keys and tag values are both required, but tag values can be empty (null) strings.

      Warning

      Do not include confidential or sensitive information in this field. This field may be displayed in plaintext in CloudTrail logs and other output.

      For information about the rules that apply to tag keys and tag values, see User-Defined Tag Restrictions in the Amazon Web Services Billing and Cost Management User Guide.

      • TagKey (string) – [REQUIRED]

        The key of the tag.

      • TagValue (string) – [REQUIRED]

        The value of the tag.

Returns:

None

Exceptions

  • KMS.Client.exceptions.KMSInternalException

  • KMS.Client.exceptions.NotFoundException

  • KMS.Client.exceptions.InvalidArnException

  • KMS.Client.exceptions.KMSInvalidStateException

  • KMS.Client.exceptions.LimitExceededException

  • KMS.Client.exceptions.TagException

Examples

The following example tags a KMS key.

response = client.tag_resource(
    # The identifier of the KMS key you are tagging. You can use the key ID or the Amazon Resource Name (ARN) of the KMS key.
    KeyId='1234abcd-12ab-34cd-56ef-1234567890ab',
    # A list of tags.
    Tags=[
        {
            'TagKey': 'Purpose',
            'TagValue': 'Test',
        },
    ],
)

print(response)

Expected Output:

{
    'ResponseMetadata': {
        '...': '...',
    },
}