Nov 18, 2023
16 min read

AWS KMS key rotation: Tutorial & best practices

AWS KMS key rotation: Tutorial & best practices
Learn how AWS KMS key rotation enhances security and meets compliance requirements, as well as the top use cases & best practices.

As DevOps engineers, we're no strangers to the paramount importance of cloud security. And, as the 10 immutable laws of security tell us: “encrypted data is only as secure as the decryption key”. In other words, effective key management is a crucial aspect of overall security posture. For the Amazon Web Services (AWS) platform, AWS KMS is a popular native key management solution, and AWS Key Management Service (KMS) key rotation is a key component in keeping keys on the platform secure.

This article delves into AWS KMS key rotation's fundamentals, its principles and outlines the best practices to ensure your data remains secure. By the end, you'll clearly understand how to implement key rotation effectively within your AWS environment.

Summary of AWS KMS key rotation concepts

The table below summarizes the AWS KMS key rotation concepts this article will explore in more detail.

Purpose of AWS KMS key rotation

Key rotation regularly replaces an encryption key with a new one. This practice enhances security by reducing the risk associated with long-lived keys. AWS KMS facilitates this process by automating your key rotation.

Key rotation primarily serves two critical purposes:

  1. Enhanced security: Regularly changing encryption keys minimizes the window of vulnerability in case a key is compromised. Even if an attacker obtains a key, it will become obsolete after rotation.
  2. Compliance requirements: Many compliance standards, such as PCI DSS and HIPAA, mandate regular key rotation as a security best practice. AWS KMS Key Rotation helps you meet these requirements effortlessly.

10 common AWS KMS key rotation use cases

There are multiple reasons teams use AWS KMS key rotation. Let’s take a look at ten common use cases.

  • Following security best practices: Regularly rotating keys is considered a security best practice. It reduces the potential impact of a compromised key because it becomes useless after rotation.
  • Meeting compliance requirements: Many compliance standards and regulations, such as PCI DSS and HIPAA, require the periodic rotation of encryption keys. Implementing key rotation helps your organization stay compliant with these regulations.
  • Data lifecycle management: In applications with a defined lifecycle and older data becomes less sensitive, you may rotate keys periodically. Newer, more sensitive data can be encrypted with the latest key, while older data remains encrypted with the older, less sensitive key.
  • Rekeying for security events: If there's a security event or a suspicion of a key compromise, you can immediately initiate an unscheduled key rotation to replace the potentially compromised key. This rapid response can help mitigate potential data breaches. It should be noted that it doesn't affect the data keys encrypted with said key. In the event of a compromised data key, the protected data must be manually encrypted with the new key.
  • Managing insider threats: If an employee with access to a key leaves the organization, rotating the key ensures that the former employee cannot access the data even if they retained a copy of the old key.
  • Upgrading encryption algorithms: Over time, new encryption algorithms and methods provide higher levels of security. You can use these new algorithms by rotating keys without changing the data format or storage systems.
  • Protection against cryptographic attacks: Cryptographic attacks and vulnerabilities can be discovered over time. Regularly rotating keys help protect your data from newly discovered vulnerabilities or exploits by using updated and secure algorithms and key lengths.
  • Vendor or third-party integration: Key rotation can be essential if you're using third-party services that have access to your encrypted data. For instance, if you grant an external service access to your data via AWS KMS, rotating keys ensure that even if that service retains an older key, it becomes obsolete after rotation, enhancing security.
  • Key custodianship: Key rotation allows organizations to change key custodians without disrupting the application. If a personnel change occurs or a need to transfer key ownership, rotating keys can facilitate this process.
  • Preventing stale keys: Over time, keys can become stale or unused. Key rotation ensures that keys are validated regularly, avoiding the scenario where a long-unused key suddenly needs to be accessed and might not work as expected.

AWS KMS pricing model

As of this writing, each AWS KMS key you create in AWS KMS costs $1/month (prorated hourly). The $1 per month charge is the same for symmetric, asymmetric, HMAC, and multi-region keys (each primary and each replica multi-region key). Suppose you enable automatic key rotation; each newly generated backing key costs an additional $1 per month (prorated hourly). This fee covers the cost to AWS KMS for retaining all versions of the key material. For more information, you can view the latest AWS KMS prices here.

AWS KMS advantages

AWS KMS is a native solution that offers teams several advantages and can help simplify the overall key management lifecycle. Here are five AWS KMS advantages to consider:

  • Automated key rotation: AWS KMS can automate the process of key rotation. You can configure KMS to automatically create new versions of keys at regular intervals, eliminating the need for manual intervention. This automation ensures that key rotation happens on schedule without requiring manual effort from your team. All AWS-managed keys enable this feature by default for the first year.
  • Integration with AWS services: KMS integrates with various AWS services. This integration enables key rotation for services such as Amazon S3, Amazon RDS, and Amazon DynamoDB without significant application changes. KMS takes care of the key rotation process, allowing you to focus on your core application logic.
  • Centralized management: KMS provides a centralized management interface for your encryption keys within AWS. You can manage key rotation policies, view key rotation history, and monitor key usage from a single console. This centralized management simplifies managing key rotation across multiple services and applications.
  • Compliance and auditing: KMS helps you meet compliance requirements related to key rotation. Using KMS, you can demonstrate to auditors and regulators that your organization follows best practices for key management and security. KMS also provides detailed audit logs if cloudTrail is enabled on the account, allowing you to track key rotation activities for compliance and auditing purposes.
  • Versioning and rollback: KMS supports key versioning, meaning multiple versions of a key can exist simultaneously. If issues arise after rotating a key, you can roll back to a previous version, ensuring uninterrupted access to your encrypted data.

AWS KMS limitations

While AWS KMS is a powerful tool for managing encryption keys and securing data, it does have some limitations and constraints that users should be aware of:

  • Regional constraints: KMS keys are regional resources. KMS keys created in one AWS region cannot encrypt or decrypt data in another region. If your application spans multiple regions, you must manage keys independently in each region.
  • Limited support for non-AWS environments: KMS provides support and integration within the AWS ecosystem but has limited usability in multi or hybrid cloud environments. Consider incorporating tools such as Doppler for managing your hybrid cloud secrets.
  • Key versioning: KMS allows you to create multiple versions of a key. However, you can only have a limited number of key versions (the default limit is 100,000 per key). If you reach this limit, you must delete older versions before creating new ones.
  • Key size limitations: An asymmetric KMS key can encrypt the maximum data size of 4 KB. If you need to encrypt larger data, use a symmetric data key (which can be generated by KMS) and then encrypt your data using that key.
  • Custom key store limitation: AWS KMS allows you to create custom key stores, which are CloudHSM clusters managed by your organization. However, each AWS account is limited to one custom key store per region.
  • Rate limits: Rate limits exist on various KMS operations, such as creating keys, encrypting data, and generating data keys. These limits are in place to prevent abuse and ensure fair usage of the service.
  • Limited integration with non-AWS services: While KMS integrates with many AWS services, it may not be directly compatible with certain non-AWS services or on-premises applications. Integration with these services might require additional configurations or workarounds.
  • Key deletion policy: When you delete a customer master key (CMK) in KMS, it is scheduled for deletion and enters a pending deletion state. The key remains in this state for at least seven days. This policy is in place to prevent accidental data loss.
  • Limited support for asymmetric keys: KMS primarily supports symmetric keys for encryption. While it offers limited support for asymmetric keys, it's important to note that certain operations, such as key rotation, are not supported.
  • Limited control over key expiration: KMS keys do not have a built-in expiration mechanism. To enforce key rotation and set specific expiration policies, you must manage these processes externally to KMS.

5 practical key rotation best practices

Getting key rotation right requires a mix of strategy and tactics. In this section, we’ll explore five practical key rotation best practices, including specific AWS KMS key rotation examples, that can help organizations improve their overall data security.

Enable key rotation

AWS managed KMS keys automatically rotate every 365 days. This setting can not be disabled. You can use the enable-key-rotation command to enable key rotation for an AWS Customer Managed KMS key using the AWS Command Line Interface (CLI). Here's the syntax for enabling key rotation:

Replace <your-key-id> with the Amazon Resource Name (ARN) of the KMS key for which you want to enable key rotation.

You can also use the AWS console to accomplish this task. This link provides details for console use.

For example, if your key ID is arn:aws:kms:us-west-2:123456789012:key/abcd1234-a123-456a-a12b-a123b4cd56ef, the command would be:

After running this command, rotation will be enabled for the specified KMS key. Remember that you need the necessary permissions to perform this operation. Ensure your AWS CLI user has the required IAM permissions to enable key rotation on the specified KMS key.

Define a rotation policy

Key rotation is specified in key policies as part of key rotation configuration. Here's an example of defining a key rotation policy for a KMS key using the AWS Command Line Interface (CLI): Replace <your-key-id> with the ID of your KMS key and <your-key-arn> with the Amazon Resource Name (ARN) of your KMS key.

In this example, the "Action": "kms: EnableKeyRotation" statement enables key rotation for the specified KMS key for the service principal kms.amazonaws.com. You can customize the policy JSON to fit your specific use case and security requirements.

Make sure you have the necessary permissions to modify the key policy. Adjust the policy document to include other statements or conditions for your specific scenario. Also, be cautious when changing key policies, as they directly affect the security and access control of your KMS keys.

Monitor rotation status

You can use the get-key-rotation-status command to check the key rotation status of an AWS KMS key using the AWS Command Line Interface (CLI). Here's the syntax for checking the key rotation status:

Replace <your-key-id> with the Amazon Resource Name (ARN) or key ID of the KMS key for which you want to check the rotation status.

For example, if your key ID is arn:aws:kms:us-west-2:123456789012:key/abcd1234-a123-456a-a12b-a123b4cd56ef, the command would be:

This command will return the key rotation status for the specified KMS key, indicating whether the key rotation is enabled (Enabled) or disabled (Disabled).

Automate key rotation on an alternate schedule

Key rotation defaults to annually. These instructions can guide you if you need to rotate keys more frequently.

To automate key rotation with AWS KMS using the AWS Command Line Interface (CLI), you can use AWS CloudWatch Events and AWS Lambda. CloudWatch Events can be set up to detect the scheduled key rotation event, and a Lambda function can be triggered to handle the key rotation process. Here's an example of how you can set this up:

Step 1: Create a CloudWatch rule

First, create a CloudWatch Events rule that triggers based on a schedule. For example, you can create a rule that triggers every day at a specific time.

This command creates a CloudWatch Events rule named "RotateKMSKeys" that triggers every day at midnight (UTC).

Step 2: Create a Lambda function

Next, create a Lambda function that handles the key rotation logic. You must create a ZIP archive containing your function code and any dependencies. Here is a sample lambda function implemented in Python:

Assuming you have a file named lambda_function.zip containing your Lambda code (lambda_function.py), you can create the Lambda function using the following command:

Replace <your-lambda-role-arn> with the ARN of the IAM role that your Lambda function will assume.

Step 3: Add permissions to the Lambda function

Give your Lambda function permission to invoke KMS key rotation. You can attach a policy to the Lambda function's execution role.

Example policy (lambda-kms-policy.json):

Apply the policy to the Lambda function's execution role:

Replace <your-lambda-role-name> with the name of your Lambda execution role and <your-kms-key-arn> with the ARN of the KMS key you want to rotate.

Step 4: Create a CloudWatch Events target

Associate the CloudWatch Events rule with your Lambda function as a target.

Replace <your-lambda-function-arn> with the ARN of your Lambda function.

Your CloudWatch Events rule will trigger the Lambda function at the specified schedule, enabling key rotation for your KMS key. Ensure your Lambda function logic includes the code to allow key rotation for the KMS key.

Please note that you need appropriate IAM permissions to perform these actions. Be careful when configuring automated key rotation, as it directly affects the security and access controls of your data.

Develop a holistic strategy

KMS is one component of a robust data security program. Securing our data environments requires more robust tooling as they become more complex. Integrating KMS with tools like Doppler can create robust and resilient security controls to manage increasingly complex environments.

Doppler is a secrets management solution that integrates into your AWS environment to enhance the security and management of secrets, including API keys, passwords, and other sensitive information.

Here's how you can incorporate Doppler into your holistic approach to KMS and secrets management in AWS:

  • Centralize secrets management: Use Doppler to centralize and manage all your application secrets securely in one place across all platforms.
  • Integrate Doppler with AWS services: Ensure that sensitive information is stored in Doppler instead of being hardcoded in code or configuration files.
  • Integrate with AWS KMS: Utilize Doppler’s integration with AWS KMS to encrypt secrets before storing them. Leverage KMS keys to encrypt and decrypt secrets stored in Doppler, ensuring an additional layer of security.
  • Enforce strict access control: Use Doppler's access control features to manage who can access different secrets. Integrate Doppler with AWS IAM to enforce fine-grained access controls based on IAM roles and policies.
  • Configure monitoring and alerts: Set up monitoring and alerts within Doppler to detect unauthorized access or changes to secrets. Integrate Doppler with AWS CloudWatch to monitor Doppler-related events and trigger alerts when necessary.
  • Integrate infrastructure as code (IaC): Incorporate Doppler into your IaC templates (e.g., AWS CloudFormation) to automate the setup of secrets and their integration with services. Use Doppler’s CLI and API to manage secrets during the deployment process programmatically.
  • Track changes with Doppler’s audit trail: Leverage Doppler’s audit trail capabilities to track changes made to secrets. Integrate Doppler's audit logs with AWS CloudTrail for a comprehensive audit trail, ensuring compliance and accountability.
  • Plan for disaster recovery: Implement backup and disaster recovery procedures for secrets stored in Doppler.
  • Build the solution into your continuous integration/continuous deployment (CI/CD) pipelines: Integrate Doppler with your CI/CD pipelines to inject secrets into your applications automatically during the build and deployment processes. Implement secure practices for managing secrets in CI/CD workflows, ensuring that sensitive information is never exposed unintentionally.

By integrating Doppler into your AWS environment following these guidelines, you can create a robust and secure secrets management system. This approach ensures that sensitive information is protected, access is controlled, and auditability is maintained, thereby enhancing the overall security posture of your AWS infrastructure.

Final thoughts on AWS KMS key rotation

Key management is an essential aspect of cybersecurity, and AWS KMS key rotation is an important security practice for the AWS cloud. Organizations can establish a robust and secure data security program by following key rotation best practices and effectively integrating the right tools. This approach ensures that sensitive information is protected, access is controlled, and auditability is maintained, thereby enhancing the overall security posture of AWS infrastructures.