:doc:`KMS <../../kms>` / Client / tag_resource

************
tag_resource
************



.. py:method:: KMS.Client.tag_resource(**kwargs)

  

  Adds or edits tags on a `customer managed key <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-mgn-key>`__.

   

  .. note::

    

    Tagging or untagging a KMS key can allow or deny permission to the KMS key. For details, see `ABAC for KMS <https://docs.aws.amazon.com/kms/latest/developerguide/abac.html>`__ 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 <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-mgn-key>`__, but you cannot tag an `Amazon Web Services managed key <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-managed-key>`__, an `Amazon Web Services owned key <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-owned-key>`__, a `custom key store <https://docs.aws.amazon.com/kms/latest/developerguide/key-store-overview.html>`__, or an `alias <https://docs.aws.amazon.com/kms/latest/developerguide/kms-alias.html>`__.

   

  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 <https://docs.aws.amazon.com/kms/latest/developerguide/tagging-keys.html>`__. For general information about tags, including the format and syntax, see `Tagging Amazon Web Services resources <https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html>`__ 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 <https://docs.aws.amazon.com/kms/latest/developerguide/key-state.html>`__ 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 <https://docs.aws.amazon.com/kms/latest/developerguide/kms-api-permissions-reference.html>`__ (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 <https://docs.aws.amazon.com/kms/latest/developerguide/accessing-kms.html#programming-eventual-consistency>`__.

  

  See also: `AWS API Documentation <https://docs.aws.amazon.com/goto/WebAPI/kms-2014-11-01/TagResource>`_  


  **Request Syntax**
  ::

    response = client.tag_resource(
        KeyId='string',
        Tags=[
            {
                'TagKey': 'string',
                'TagValue': 'string'
            },
        ]
    )
    
  :type KeyId: string
  :param KeyId: **[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.

    

  
  :type Tags: list
  :param Tags: **[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 <https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/allocation-tag-restrictions.html>`__ 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**
  
  *   :py:class:`KMS.Client.exceptions.KMSInternalException`

  
  *   :py:class:`KMS.Client.exceptions.NotFoundException`

  
  *   :py:class:`KMS.Client.exceptions.InvalidArnException`

  
  *   :py:class:`KMS.Client.exceptions.KMSInvalidStateException`

  
  *   :py:class:`KMS.Client.exceptions.LimitExceededException`

  
  *   :py:class:`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': {
            '...': '...',
        },
    }

  