This page shows how to write Terraform and CloudFormation for AWS KMS Key and write them securely.


Fix issues in your cloud & app configurations

Test for misconfigurations of this resource in your cloud.


Terraform Example (aws_kms_key)

Manages a single-Region or multi-Region primary KMS key.


Example Usage (from GitHub)

resource "aws_kms_key" "sourcekey1" {
    provider                = aws.source1
    description             = "This key is used to encrypt source bucket objects"
    deletion_window_in_days = 10

resource "aws_kms_key" "kmsKeyExposedPolicy" {
  description = "KMS key 2"
  policy      = <<EOF
  "Version": "2012-10-17",
  "Statement": [
resource "aws_kms_key" "kmsKeyExposedPolicy" {
  description         = "KMS key 2"
    policy = <<EOF
  "Version": "2012-10-17",
  "Statement": [
resource "aws_kms_key" "root" {
  description             = "Boundary root key"
  deletion_window_in_days = 10

  tags = merge(var.tags, {
    Name =
resource "aws_kms_key" "kms_key_allow_no_wildcard" {
  description             = "Example Key"
  deletion_window_in_days = 10

  policy = <<EOF

CloudFormation Example (AWS::KMS::Key)

The AWS::KMS::Key resource specifies a symmetric or asymmetric KMS key in AWS Key Management Service (AWS KMS).

Note AWS KMS is replacing the term customer master key (CMK) with AWS KMS key and KMS key. The concept has not changed. To prevent breaking changes, AWS KMS is keeping some variations of this term.

You can use symmetric KMS keys to encrypt and decrypt small amounts of data, but they are more commonly used to generate data keys and data key pairs. You can also use symmetric KMS key to encrypt data stored in AWS services that are integrated with AWS KMS. For more information, see What is AWS Key Management Service? in the AWS Key Management Service Developer Guide.

You can use asymmetric KMS keys to encrypt and decrypt data or sign messages and verify signatures. To create an asymmetric key, you must specify an asymmetric KeySpec value and a KeyUsage value.

Important If you change the value of a Replacement property, such as KeyUsage or KeySpec, on an existing KMS key, the existing KMS key is scheduled for deletion and a new KMS key is created with the specified value. While scheduled for deletion, the existing KMS key becomes unusable. If you don't cancel the scheduled deletion of the existing KMS key outside of CloudFormation, all data encrypted under the existing KMS key becomes unrecoverable when the KMS key is deleted.


Frequently asked questions

What is AWS KMS Key?

AWS KMS Key is a resource for KMS of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS KMS Key?

For Terraform, the Venkateshg07/s3crossreplication, storebot/pr_demo_flat and kanchwala-yusuf/aws-terraform source code examples are useful. See the Terraform Example section for further details.