

# Encryption at rest for OpenTelemetry metrics and profiles

## What is a CloudWatch Dataset
<a name="cmk-encryption-dataset-overview"></a>

OpenTelemetry (OTel) metrics and profile data that you send to Amazon CloudWatch are stored in a resource called a Dataset. Every AWS account has a `default` dataset in each Region, and it holds all of this data. The `default` dataset is the only supported dataset. You can't create additional datasets. Everything on this page, including encryption, keys, key policies, and every action and configuration, applies to the dataset.

Datasets can be encrypted and tagged like other AWS resources. The dataset ARN has the following format:

`arn:{{{partition}}}:cloudwatch:{{{region}}}:{{{account-id}}}:dataset/default`

To view the current encryption configuration of your dataset, use the [GetDataset](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetDataset.html) API or the [get-dataset](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-dataset.html) AWS CLI command:

```bash
aws cloudwatch get-dataset \
    --dataset-identifier default
```

If a customer managed key is associated with the dataset, the response includes the key ARN. If no customer managed key is associated, the dataset is encrypted with an AWS owned key.

## Options for encryption at rest
<a name="cmk-encryption-options"></a>

CloudWatch always encrypts the data in your dataset at rest on the server side. By default, CloudWatch encrypts all data at rest using AWS owned keys. You don't need to take any action to protect your data using AWS owned keys. For more information, see [AWS owned keys](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-owned-cmk) in the AWS Key Management Service Developer Guide.

If you want to manage the keys that are used to encrypt your Dataset data, you can use a customer managed key in AWS Key Management Service (AWS KMS). For more information, see [Customer managed keys](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk) in the AWS Key Management Service Developer Guide.

**Important**  
When you use a customer managed key, AWS KMS charges apply. For more information about pricing, see [AWS Key Management Service pricing](https://aws.amazon.com/kms/pricing/) on the AWS website.

## How CloudWatch uses a customer managed key for Dataset encryption
<a name="cmk-encryption-how-it-works"></a>

**Important**  
Customer managed key encryption applies to the `default` dataset. The `default` dataset is the only supported dataset — you cannot create additional datasets.

When you associate a customer managed key with the `default` dataset, CloudWatch uses the key to encrypt the data that it stores in that dataset.

Metrics and profile data are stored in the same dataset. Because of that, they share the same dataset APIs and key policies to manage encryption. The identities that write to or read from the dataset must be allowed `kms:Decrypt` on the key. For more information about profiles, see [Profiles in Amazon CloudWatch](CloudWatch_Profiles.md). For the key policy, see [Configuring key policy permissions](#cmk-encryption-permissions).

CloudWatch uses the service principal (`cloudwatch.amazonaws.com`) directly with key policy permissions. CloudWatch does not use grants or IAM roles to access your AWS KMS key.

CloudWatch doesn't cache data keys. For every request that writes to or reads from the dataset, CloudWatch checks that the caller is allowed `kms:Decrypt` on the key. CloudWatch caches a successful check for up to 15 minutes and a failed check for up to 60 seconds. Because of that, changes to a key policy can take up to 15 minutes to take effect.

CloudWatch uses the following encryption context for all AWS KMS cryptographic operations:
+ Key: `aws:cloudwatch:arn`
+ Value: `arn:{{{partition}}}:cloudwatch:{{{region}}}:{{{account-id}}}:dataset/default`

## Configuring a customer managed key for Dataset
<a name="cmk-encryption-key-requirements"></a>

The AWS KMS key that you use with CloudWatch Dataset must meet the following requirements:
+ The key must be a symmetric encryption key (SYMMETRIC\_DEFAULT) with key usage ENCRYPT\_DECRYPT. Asymmetric keys are not supported.
+ Multi-Region keys are not supported.
+ The key must be in the same AWS Region as the Dataset.
+ You must specify the key as a fully qualified key ARN. Key aliases and key IDs are not supported.

## Configuring key policy permissions
<a name="cmk-encryption-permissions"></a>

To use a customer managed key with CloudWatch Dataset, the key policy must grant CloudWatch permission to use the key. One key policy covers all the data in the `default` dataset. The following example key policy grants CloudWatch the necessary permissions and includes confused deputy protection.

CloudWatch checks that the caller is allowed `kms:Decrypt` on the key for every request that writes to or reads from the dataset, and for the calls that associate or remove the key. The `AllowCallerDecrypt` statement in the following example allows your own IAM principals to use the key, but only for requests that CloudWatch makes for your `default` dataset. In it, list every IAM principal that writes to or reads from the dataset, and every principal that calls `AssociateDatasetKmsKey` or `DisassociateDatasetKmsKey`. For the CloudWatch agent, this is its IAM role, such as the Amazon EC2 instance role or the Amazon EKS node role. Replace {{YourApplicationRole}} with these principals, and keep the statements that give your key administrators access to the key.

The `CloudWatchAgentServerPolicy` and `CloudWatchAgentAdminPolicy` managed policies don't include `kms:Decrypt`. If your key policy keeps the default statement that allows IAM policies to grant access to the key, you can grant `kms:Decrypt` to these principals with an IAM policy instead. The example key policy doesn't include that statement.

**Example Key policy for CloudWatch Dataset encryption**  

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowCloudWatchDatasetDescribeKey",
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudwatch.amazonaws.com"
            },
            "Action": "kms:DescribeKey",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "{{111122223333}}"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
                }
            }
        },
        {
            "Sid": "AllowCloudWatchDatasetEncryption",
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudwatch.amazonaws.com"
            },
            "Action": [
                "kms:GenerateDataKey",
                "kms:Encrypt",
                "kms:Decrypt",
                "kms:ReEncrypt*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "{{111122223333}}",
                    "kms:EncryptionContext:aws:cloudwatch:arn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
                }
            }
        },
        {
            "Sid": "AllowCallerDecrypt",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{111122223333}}:role/{{YourApplicationRole}}"
            },
            "Action": "kms:Decrypt",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "kms:ViaService": "cloudwatch.{{us-east-1}}.amazonaws.com",
                    "kms:EncryptionContext:aws:cloudwatch:arn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
                }
            }
        }
    ]
}
```

Replace the example account ID (`111122223333`) and the example AWS Region (`us-east-1`) with your own values.

For more information about key policies, see [Key policies in AWS KMS](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html) in the AWS Key Management Service Developer Guide.

## To associate a customer managed key with a Dataset
<a name="cmk-encryption-associate"></a>

Use the [AssociateDatasetKmsKey](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_AssociateDatasetKmsKey.html) API to associate a customer managed key with a Dataset. You must specify `default` as the dataset identifier.

To associate a customer managed key by using the AWS CLI, run the [associate-dataset-kms-key](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/associate-dataset-kms-key.html) command. Replace {{key-id}}, the example account ID, and the example Region with your own values:

```bash
aws cloudwatch associate-dataset-kms-key \
    --dataset-identifier default \
    --kms-key-arn arn:aws:kms:{{us-east-1}}:{{111122223333}}:key/{{key-id}}
```

CloudWatch begins using the key for subsequent encryption operations. Profiles stored before the association can remain encrypted with an AWS owned key.

## Changing or removing encryption configuration
<a name="cmk-encryption-change-disassociate"></a>

You can change or remove the customer managed key that encrypts your Dataset data.

### To change the customer managed key
<a name="cmk-encryption-change-key"></a>

To replace the customer managed key, call `AssociateDatasetKmsKey` again with a new key ARN. The caller must have `kms:Decrypt` permission on both the current key and the new key. CloudWatch begins using the new key for subsequent encryption operations. Profiles already stored under the previous key can remain encrypted under it.

**Note**  
The currently associated key might be deleted, scheduled for deletion, pending import, unavailable, or disabled. In these cases, CloudWatch doesn't require `kms:Decrypt` permission on it. The rotation proceeds. If the key was only disabled, consider re-enabling it rather than rotating, because re-enabling allows CloudWatch to resume decrypting existing data in the dataset that's encrypted with that key.

**Important**  
If you use CloudWatch Profiles, keep the previous key enabled, and keep its key policy statements for CloudWatch, for at least 7 days after you change the key.  
CloudWatch keeps profile data for 7 days. Profiles stored under the previous key can stay encrypted with it for up to 7 days. During this period, the previous key might be disabled or deleted, or its statements for CloudWatch might be removed. If so, profile queries whose time range includes those profiles fail with HTTP status 403. Profile queries over other time ranges keep working.

### To remove the customer managed key
<a name="cmk-encryption-disassociate"></a>

To remove the customer managed key and revert to AWS owned key encryption, call [DisassociateDatasetKmsKey](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_DisassociateDatasetKmsKey.html), or run the [disassociate-dataset-kms-key](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/disassociate-dataset-kms-key.html) command. The caller must have `kms:Decrypt` permission on the currently associated key. CloudWatch encrypts new data with an AWS owned key. Profiles already stored under the key can remain encrypted under it.

```bash
aws cloudwatch disassociate-dataset-kms-key \
    --dataset-identifier default
```

**Note**  
If the currently associated key is deleted, scheduled for deletion, pending import, unavailable, or disabled, CloudWatch doesn't require `kms:Decrypt` permission on it. The disassociation proceeds. If the key was only disabled, consider re-enabling it instead of disassociating, because re-enabling allows CloudWatch to resume decrypting your existing data in the dataset.

**Important**  
After you remove a customer managed key, CloudWatch continues to require `kms:Decrypt` on the previous key for at least 3 hours, and sometimes longer. During this time, requests that write to or read from the dataset fail for callers that aren't allowed `kms:Decrypt` on the previous key.  
If you use CloudWatch Profiles, keep the previous key enabled for at least 7 days after you remove it from the dataset. For the same period, also keep its key policy statements for CloudWatch. Profiles stored under the previous key can stay encrypted with it for up to 7 days, and profile queries over those time ranges fail if the key can't be used.

## Recovering from a deleted or unusable KMS key
<a name="cmk-encryption-recovering"></a>

If the customer managed key is disabled, deleted, scheduled for deletion, or unavailable, or if a caller is no longer allowed `kms:Decrypt` on it, CloudWatch rejects that caller's requests to write to or read from the dataset with HTTP status 403. A caller whose earlier successful check is still cached can keep sending and querying for up to 15 minutes. If AWS KMS throttles the check or doesn't respond, the request fails with a server error, such as HTTP status 503. Retry the request.

For profile data in the dataset, the following also applies:
+ Profiles that CloudWatch received but can't process while the key is unusable are retried for 1 day and then lost, so restore the key as soon as you can.
+ You might remove only the key policy statements for the CloudWatch service principal and keep `AllowCallerDecrypt`. In that case, CloudWatch still accepts profiles but can't store them, and they are lost after 1 day. Profile queries over time ranges stored under the key fail with HTTP status 403. Profile queries over other time ranges keep working.

Your recovery options depend on the key state:

Key is disabled  
Re-enable the key in AWS KMS. This is the preferred recovery path because CloudWatch can resume decrypting your existing data after the key is active again. CloudWatch also keeps trying to process the profiles it received while the key was unusable, for 1 day after it received them.

Key is pending deletion  
Cancel the key deletion in AWS KMS before the waiting period expires. After the key returns to an enabled state, CloudWatch resumes normal operations.

Key has been permanently deleted or is otherwise unrecoverable  
Remove the stale association or rotate to a new key. For more information, see [Remove the stale key association](#cmk-encryption-recovering-disassociate) and [Rotate to a new key](#cmk-encryption-recovering-rotate). Note that existing data encrypted with the deleted key is permanently unreadable.

If the key is permanently deleted or otherwise unrecoverable, you can remove the association or rotate to a new key without needing access to the old key:

### Remove the stale key association
<a name="cmk-encryption-recovering-disassociate"></a>

Call `DisassociateDatasetKmsKey` to remove the association with the unusable key. CloudWatch reverts to AWS owned key encryption for new data. Existing data that was encrypted with the deleted key is no longer readable.

```bash
aws cloudwatch disassociate-dataset-kms-key \
    --dataset-identifier default
```

### Rotate to a new key
<a name="cmk-encryption-recovering-rotate"></a>

Call `AssociateDatasetKmsKey` with a new, valid KMS key ARN. CloudWatch validates the new key and associates it with your dataset. Access to the previously associated key is not required. Existing data that was encrypted with the deleted key is no longer readable, but new data is encrypted with the new key.

In the following command, replace {{new-key-id}}, the example account ID, and the example Region with your own values:

```bash
aws cloudwatch associate-dataset-kms-key \
    --dataset-identifier default \
    --kms-key-arn arn:aws:kms:{{us-east-1}}:{{111122223333}}:key/{{new-key-id}}
```

### Error responses for unusable keys
<a name="cmk-encryption-recovering-errors"></a>

When you associate a key, CloudWatch first checks the new key: that it exists, is enabled, is a symmetric encryption key, and that its key policy allows the CloudWatch service principal. It then checks that you are allowed `kms:Decrypt` on the currently associated key, if there is one, and on the new key. When you remove a key, CloudWatch checks that you are allowed `kms:Decrypt` on the currently associated key. If the currently associated key is deleted, disabled, pending deletion, pending import, or unavailable, CloudWatch skips the check on that key and the operation proceeds.

`AccessDeniedException`  
The new key doesn't exist, is disabled or pending deletion, isn't a symmetric encryption key, or its key policy doesn't allow the CloudWatch service principal. Or you aren't allowed `kms:Decrypt` on the new key or on the currently associated key; that message includes the AWS KMS request ID.

`ValidationException`  
A key that CloudWatch checks is in another state, such as `Creating` or `Updating`. Or the key ARN is an alias, a key ID, or a key in another AWS Region.

`ResourceNotFoundException`  
You called `DisassociateDatasetKmsKey` and no key is associated.

`InternalFailure`  
AWS KMS throttled the request or returned an error. Retry the request.

For more information about KMS key states, see [Key states of AWS KMS keys](https://docs.aws.amazon.com/kms/latest/developerguide/key-state.html) in the *AWS Key Management Service Developer Guide*.

## Scoping down key policy access
<a name="cmk-encryption-scoping-access"></a>

You can use conditions in the key policy to limit access to your AWS KMS key.

Encryption context condition  
Use the `kms:EncryptionContext:aws:cloudwatch:arn` condition key to restrict key usage to your `default` Dataset.  

```JSON
"Condition": {
    "StringEquals": {
        "kms:EncryptionContext:aws:cloudwatch:arn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
    }
}
```

Confused deputy protection  
Use the `aws:SourceArn` and `aws:SourceAccount` conditions to prevent cross-account confused deputy attacks.  

```JSON
"Condition": {
    "StringEquals": {
        "aws:SourceAccount": "{{111122223333}}"
    },
    "ArnLike": {
        "aws:SourceArn": "arn:aws:cloudwatch:{{us-east-1}}:{{111122223333}}:dataset/default"
    }
}
```

kms:ViaService condition  
Use the `kms:ViaService` condition key to restrict key usage to requests that come from CloudWatch.  

```JSON
"Condition": {
    "StringEquals": {
        "kms:ViaService": "cloudwatch.{{us-east-1}}.amazonaws.com"
    }
}
```

## Monitoring CloudWatch interaction with AWS KMS
<a name="cmk-encryption-monitoring"></a>

You can use AWS CloudTrail to track the requests that CloudWatch sends to AWS KMS on your behalf. The AWS CloudTrail log entries use the service principal `cloudwatch.amazonaws.com` and a `ViaService` value of `cloudwatch.{{{region}}}.amazonaws.com`.

The following CloudTrail event names appear in log entries for CloudWatch Dataset encryption operations:
+ `GenerateDataKey`
+ `Encrypt`
+ `Decrypt`
+ `DescribeKey`
+ `ReEncrypt`

Each log entry includes the encryption context, which you can use to identify the specific Dataset that the operation applies to.

To find these events, see [Searching for AWS KMS events in CloudTrail](https://docs.aws.amazon.com/kms/latest/developerguide/logging-using-cloudtrail.html#searching-kms-ct) in the AWS Key Management Service Developer Guide.

For more information about monitoring AWS KMS key usage, see [Monitoring AWS Key Management Service](https://docs.aws.amazon.com/kms/latest/developerguide/monitoring-overview.html) in the AWS Key Management Service Developer Guide.