AWS KMS External Key

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

aws_kms_external_key (Terraform)

The External Key in AWS KMS can be configured in Terraform with the resource name aws_kms_external_key. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

kms_external_key_test.tf#L12
main.tf#L7
resource "aws_kms_external_key" "this" {
  deletion_window_in_days = var.deletion_window_in_days
  description             = var.description
  enabled                 = var.enabled
  key_material_base64     = var.key_material_base64
  policy                  = var.policy
kms_external_key_test.tf#L12
main.tf#L19
resource "aws_kms_external_key" "this" {
  count = var.use_aws_key_material ? 0 : 1

  deletion_window_in_days = var.deletion_window_in_days
  description             = var.description
  enabled                 = var.is_enabled

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a single-Region or multi-Region primary KMS key that uses external key material. To instead manage a single-Region or multi-Region primary KMS key where AWS automatically generates and potentially rotates key material, see the aws_kms_key resource.

Note: All arguments including the key material will be stored in the raw state as plain-text. Read more about sensitive data in state.

AWS::KMS::Key (CloudFormation)

The Key in KMS can be configured in CloudFormation with the resource name AWS::KMS::Key. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

template.yml#L9
    Type: AWS::KMS::Key
    Properties:
      Description: Symmetric key for demo
      Enabled: True
      EnableKeyRotation: True
      KeySpec: SYMMETRIC_DEFAULT
serverless.yml#L88
      Type: AWS::KMS::Key
      DependsOn:
        - CreateRecordLambdaFunction
        - GetRecordLambdaFunction
      Properties:
        Description: 'QLDB Ledger CMK'
aws_kms_cmk_loss.yml#L69
                  "type": "AWS::KMS::Key"
              }
          ],
          "eventType": "AwsApiCall",
          "recipientAccountId": "123456789012"
      }
07-kms.yml#L6
    Type: AWS::KMS::Key
    Properties:
      Description: KMS Key used for protecting Kolide Fleet's SSL private key
      EnableKeyRotation: true
      KeyPolicy:
        Version: '2012-10-17'
serverless.yml#L29
      Type: AWS::KMS::Key
      Properties:
        Description: RSA_4096 asymmetric CMK
        KeySpec: RSA_4096
        KeyUsage: ENCRYPT_DECRYPT
        KeyPolicy:
EKMSKeyEnableKeyRotation.json#L6
      "Type": "AWS::KMS::Key",
      "Properties": {
        "EnableKeyRotation": false
      }
    },
    "EKMSKeyEnableKeyRotationSecondary": {
AWS-Customer-Master-Key-rotation-is-not-enabled.json#L6
   "rule":"$.Resources.*[?(@.Type=='AWS::KMS::Key')].Properties.EnableKeyRotation any null or $.Resources.*[?(@.Type=='AWS::KMS::Key')].Properties.EnableKeyRotation anyFalse",
   "id":"6ae8d0a5-4794-438c-aafa-200f94b45f1f",
   "enabled":true,
   "recommendation": {
      "name": "Recommended solution for enabling Customer Master Keys.",
      "description": "It is recommended that Customer Master Keys rotation is enabled. Please make sure your template has \"EnableKeyRotation\" attribute and it is set to \"true\"",
positive2.json#L4
      "Type": "AWS::KMS::Key",
      "Properties": {
        "Enabled": true,
        "KeyPolicy": {
          "Version": "2012-10-17",
          "Id": "key-default-1",
positive2.json#L4
      "Type": "AWS::KMS::Key",
      "Properties": {
        "Enabled": false,
        "KeyPolicy": {
          "Id": "key-default-1",
          "Statement": [
kms.json#L12
      "CloudTrailEvent": "{\"eventVersion\":\"1.05\",\"userIdentity\":{\"type\":\"IAMUser\",\"principalId\":\"AIDAXGJAWKEZ3D5NC2A3X\",\"arn\":\"arn:aws:iam::494526681395:user/hzhuang\",\"accountId\":\"494526681395\",\"accessKeyId\":\"ASIAXGJAWKEZ5R4FWDTG\",\"userName\":\"hzhuang\",\"sessionContext\":{\"sessionIssuer\":{},\"webIdFederationData\":{},\"attributes\":{\"mfaAuthenticated\":\"false\",\"creationDate\":\"2020-05-14T01:47:32Z\"}}},\"eventTime\":\"2020-05-14T01:47:46Z\",\"eventSource\":\"kms.amazonaws.com\",\"eventName\":\"GetKeyPolicy\",\"awsRegion\":\"ap-southeast-1\",\"sourceIPAddress\":\"202.66.38.130\",\"userAgent\":\"aws-internal/3 aws-sdk-java/1.11.742 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.242-b08 java/1.8.0_242 vendor/Oracle_Corporation\",\"requestParameters\":{\"keyId\":\"aa5b2dc0-88ba-4e21-b339-82d8a18e9e6b\",\"policyName\":\"default\"},\"responseElements\":null,\"requestID\":\"dbd92b4e-b29e-493d-bd21-8fdd63806075\",\"eventID\":\"16731af3-a991-4b56-a7ad-348a1a2a73b6\",\"readOnly\":true,\"resources\":[{\"accountId\":\"494526681395\",\"type\":\"AWS::KMS::Key\",\"ARN\":\"arn:aws:kms:ap-southeast-1:494526681395:key/aa5b2dc0-88ba-4e21-b339-82d8a18e9e6b\"}],\"eventType\":\"AwsApiCall\",\"recipientAccountId\":\"494526681395\"}"
    },
    {
      "EventId": "5adb4de1-2822-451e-adb4-dd92bf6a84a5",
      "EventName": "ListAliases",
      "ReadOnly": "true",

Parameters

Explanation in CloudFormation Registry

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 External Key?

AWS KMS External 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 External Key?

For Terraform, the gilyas/infracost, niveklabs/aws and infracost/infracost source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the MarkBiesheuvel/demo-templates, AWS-South-Wales-User-Group/qldb-kms-demo and panther-labs/panther-analysis source code examples are useful. See the CloudFormation Example section for further details.