Lesson 1 of 3 AWS Secrets Manager, Parameter Store and ACM

Storing and Rotating Credentials with AWS Secrets Manager

This lesson explains how Secrets Manager stores credentials such as database passwords and API keys, encrypts them with KMS and rotates them automatically, so applications never hard-code secrets.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the features of Secrets Manager.
  • Explain how rotation works.
  • Describe how applications and services retrieve secrets.

Features

Secrets Manager stores credentials, such as database passwords and API keys, so they are not kept in code or configuration files. Every secret is encrypted with a KMS key, either the AWS managed key or a customer managed key that you control. Its most important feature is automatic rotation: on a schedule, a rotation function creates a new credential, updates it in the database or service and stores it, while applications continue to read the current version. Rotation is built in for RDS, Aurora, Redshift and DocumentDB, and a custom Lambda function handles any other credential.

Secrets can be replicated to other Regions, so applications there, or in a disaster recovery Region, read a local copy. Cross-account access needs a resource policy on the secret and permission to use its customer managed KMS key, because the AWS managed key cannot be shared. Every retrieval is logged in CloudTrail. Secrets are charged per secret per month plus API calls. The following table summarises the features.

FeatureDetail
EncryptionEvery secret is encrypted with a KMS key, AWS managed or customer managed
Automatic rotationNative for RDS, Aurora, Redshift and DocumentDB; Lambda rotation functions for anything else
ReplicationReplica secrets in other Regions for disaster recovery and multi-Region apps
Cross-account accessA resource policy on the secret plus access to its customer managed KMS key
AuditingEvery retrieval is logged in CloudTrail
CostPer secret per month plus API calls

How rotation works

Secrets Manager rotates a secret by means of a Lambda function that proceeds through a fixed sequence of steps. The steps are as follows.

  1. Create

    Create a new credential, labelled AWSPENDING.

  2. Set

    Set it on the database or service.

  3. Test

    Check that the new credential works.

  4. Finish

    Mark it current (AWSCURRENT); the old one becomes AWSPREVIOUS.

RDS and Aurora can also manage the master password in Secrets Manager themselves. Applications that fetch the secret at run time pick up new values without redeploying, so never copy secrets into code, AMIs or images.

The following diagram shows the four rotation steps and the labels they move.

How Secrets Manager rotates a secret. A rotation Lambda function creates a new credential labelled AWSPENDING, sets it on the database, tests that it works, and finishes by marking it AWSCURRENT, at which point the old value becomes AWSPREVIOUS. Applications that fetch the secret at run time pick up the new value without redeploying.
Because the old value stays available as AWSPREVIOUS until the next rotation, connections opened before the change keep working.
Put in order

Rotating a secret

Exam skills 1.3.7 · 1.2.3

Put the steps of Secrets Manager rotation in order.

Log in to try this activity Interactive activities are free with an account.

Getting secrets into applications

Applications and services retrieve secrets from Secrets Manager in several ways, so that no secret is written into code or configuration. Applications call GetSecretValue with their IAM role, ideally through a caching client that keeps the value in memory for a period and reduces cost and latency.

Several services integrate directly. Amazon ECS task definitions inject secrets into containers as environment variables through the secrets field. Lambda functions read them through the AWS Parameters and Secrets Lambda Extension, which caches values. AWS CloudFormation dynamic references, written as {{resolve:secretsmanager:...}}, pass secrets to resources such as database instances without exposing them in templates.

Match the pairs

Getting secrets in

Exam skills 1.2.3

Match each workload with how it should receive secrets.

Log in to try this activity Interactive activities are free with an account.
Worked example

Rotating a database password without downtime

An Aurora database password is stored in an application's configuration file and has not changed in three years. The security team requires 30-day rotation and a copy in the DR Region.

StepAction
1Store the credentials in Secrets Manager with managed rotation every 30 days
2Change the application to read the secret at runtime, with a caching client
3Replicate the secret to the DR Region
4Remove the password from the configuration file

Rotation creates and tests the new password before marking it current, and the caching client picks it up within its refresh interval, so the application never fails to connect.

Check yourself

How can applications in a disaster recovery Region read the same secret?

Show answer

With multi-Region secrets: Secrets Manager replicates the secret to the chosen Regions and keeps the replicas in sync, including after rotation.

Exam Tips

Remember for the exam

  • For automatic rotation of credentials, use Secrets Manager.
  • To make secrets available in a disaster recovery Region, use Secrets Manager replication.
  • To share a secret with another account, use a resource policy on the secret plus access to its customer managed KMS key.
  • So that applications pick up rotated passwords automatically, retrieve the secret at run time, with caching.