Skip to main content

PutKeyPolicy

Attaches a key policy to the specified KMS key.

Description

The `PutKeyPolicy` operation attaches a key policy to the specified KMS key. If the key already has a policy attached, the new policy replaces the existing one.

note
  • Currently, QKMS supports only one key policy per KMS key.
  • The only valid policy name is "default".
  • The key policy size limit is 32 kilobytes (32768 bytes).
  • For help writing and formatting a key policy, see Key Policies.
  • Changes to the key policy take effect immediately.

Request Syntax

Headers

NameDescriptionRequiredType
Content-TypeMust be "application/x-amz-json-1.1"Yesstring
X-Amz-TargetMust be "TrentService.PutKeyPolicy"Yesstring

Request Body

NameDescriptionRequiredType
KeyIdThe identifier of the KMS key to attach the key policy to. This can be the key ID or key ARN of the KMS key.Yesstring
PolicyNameThe name of the key policy. Currently, the only valid name is "default".Yesstring
PolicyThe key policy to attach to the KMS key. The key policy must be in JSON format.Yesstring
BypassPolicyLockoutSafetyCheckA flag to indicate whether to bypass the key policy lockout safety check. Setting this value to true increases the risk that the KMS key becomes unmanageable. Do not set this value to true indiscriminately.Noboolean
POST / HTTP/1.1
Content-Type: application/x-amz-json-1.1
X-Amz-Target: TrentService.PutKeyPolicy
{ "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab", "PolicyName": "default", "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Enable IAM User Permissions\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::111122223333:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\"}]}", "BypassPolicyLockoutSafetyCheck": false }
Values in italics indicate user input and should be replaced with actual values.

Examples

Example 1: Attach a key policy to a KMS key

POST / HTTP/1.1
Content-Type: application/x-amz-json-1.1
X-Amz-Target: TrentService.PutKeyPolicy
{ "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab", "PolicyName": "default", "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Enable IAM User Permissions\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::111122223333:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\"}]}" }
Values in italics indicate user input and should be replaced with actual values.
HTTP/1.1 200 OK
Content-Type: application/x-amz-json-1.1
{}
Values in italics indicate variable response values.

Example 2: Attach a key policy with safety check bypass

POST / HTTP/1.1
Content-Type: application/x-amz-json-1.1
X-Amz-Target: TrentService.PutKeyPolicy
{ "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab", "PolicyName": "default", "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Enable IAM User Permissions\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::111122223333:user/username\"},\"Action\":\"kms:*\",\"Resource\":\"*\"}]}", "BypassPolicyLockoutSafetyCheck": true }
Values in italics indicate user input and should be replaced with actual values.
HTTP/1.1 200 OK
Content-Type: application/x-amz-json-1.1
{}
Values in italics indicate variable response values.

Special Errors

Error CodeDescription
DependencyTimeoutExceptionThe system timed out while trying to fulfill the request.
InvalidArnExceptionThe request was rejected because a specified ARN was not valid.
KMSInternalExceptionAn internal error occurred.
KMSInvalidStateExceptionThe request was rejected because the key state is not valid for this operation.
LimitExceededExceptionThe request was rejected because a quota was exceeded.
MalformedPolicyDocumentExceptionThe request was rejected because the specified policy document was malformed.
NotFoundExceptionThe request was rejected because the specified entity or resource could not be found.
UnsupportedOperationExceptionThe request was rejected because a specified parameter is not supported or a specified resource is not valid for this operation.

Permissions

To use the `PutKeyPolicy` operation, you must have the following permissions:

  • `kms:PutKeyPolicy` on the KMS key (specified in the policy)

Try It Out

Test PutKeyPolicy

Attach a key policy to a KMS key.

Coming Soon

This feature is currently under development and will be available soon.