Store app secrets in AWS Secrets Manager with least privilege access
Keep API keys and generated passwords in Secrets Manager, encrypted with your own KMS key, where exactly one role can read exactly those secrets. Nothing sits in a .env file or in the repository.
App role
IAM policy
Secrets Manager
KMS key
How it works
Where secrets end up without a plan
Secrets start in a .env file, get pasted into the hosting console, and sooner or later one lands in a commit or a chat message. After that you cannot say who has it. Putting them in one place with one reader means you know who can read each value, and you can change a value without a code change.
Create a key for the secrets
The secrets are encrypted with a KMS key that belongs to this app, with yearly rotation on. Reading a secret then needs two permissions, one on the secret and one on the key, so a loose policy on either side is not enough by itself.
Generate what you can
Values that only your app needs to know, such as a session signing key, are generated by OpenTofu and never typed by a person. The generated value is also held in the OpenTofu state, so the state must live in an encrypted backend with restricted access.
Fill in the rest by hand
Secrets that come from someone else, such as a payment provider key, are created empty and you set the value once with aws secretsmanager put-secret-value. The mistake people make is passing these values in as OpenTofu variables, which writes them into the state file and often into shell history.
Let one role read only these
The policy lists the exact secret ARNs and is attached to the one role your app runs as. The common shortcut is a resource of * on secretsmanager:GetSecretValue, which lets the app read every secret in the account, including other apps' database passwords. The key permission is limited to requests that come through Secrets Manager.
Read them from the app
Have your platform pass the secret to the container as an environment variable by ARN, or call GetSecretValue at startup with the AWS SDK. Either way the app uses its role, so there is no access key to store.
What you set
name_prefix
Prefix for secret names, app and environment For example web-app/production
reader_role_name
Existing role the app runs as For example web-app-apprunner-instance
manual_secret_names
Secrets you fill in by hand For example ["stripe-api-key", "smtp-password"]
What it costs to run
About $2 to $5 a month: $0.40 per secret, $1 for the KMS key, and a few cents for API calls. Estimate.
The configuration
# Application secrets in Secrets Manager, readable by one role only.
#
# What this creates:
# - a KMS key, with yearly rotation, that encrypts the secrets
# - one secret with a generated value (for example a session signing key)
# - empty secrets for values you get from someone else (API keys), which
# you fill in by hand so they never pass through code or state
# - an IAM policy on ONE existing role that allows reading ONLY these secrets
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region to deploy into."
type = string
default = "us-east-1"
}
variable "name_prefix" {
description = "Prefix for secret names, usually app and environment, for example web-app/production."
type = string
}
variable "reader_role_name" {
description = "Name of the existing IAM role your app runs as. Only this role gets read access."
type = string
}
variable "generated_secret_name" {
description = "Name of the secret whose value is generated here."
type = string
default = "session-secret"
}
variable "manual_secret_names" {
description = "Names of secrets you fill in by hand, such as third party API keys."
type = set(string)
default = []
}
variable "recovery_window_in_days" {
description = "Days a deleted secret can still be restored. Set to 0 only for tests you want to delete at once."
type = number
default = 30
}
data "aws_region" "current" {}
# --- Encryption key ----------------------------------------------------------
# A key of your own, instead of the account default, so that reading a secret
# takes two permissions: the secret and the key.
resource "aws_kms_key" "secrets" {
description = "Encrypts secrets under ${var.name_prefix}"
enable_key_rotation = true
deletion_window_in_days = 30
}
resource "aws_kms_alias" "secrets" {
name = "alias/${replace(var.name_prefix, "/", "-")}-secrets"
target_key_id = aws_kms_key.secrets.key_id
}
# --- Secrets -----------------------------------------------------------------
# The generated value is also kept in the OpenTofu state, so keep the state
# in an encrypted backend that only your team can read.
resource "random_password" "generated" {
length = 48
special = false
}
resource "aws_secretsmanager_secret" "generated" {
name = "${var.name_prefix}/${var.generated_secret_name}"
kms_key_id = aws_kms_key.secrets.arn
recovery_window_in_days = var.recovery_window_in_days
}
resource "aws_secretsmanager_secret_version" "generated" {
secret_id = aws_secretsmanager_secret.generated.id
secret_string = random_password.generated.result
}
# Created empty on purpose. Set each value once with:
# aws secretsmanager put-secret-value --secret-id <name> --secret-string <value>
# OpenTofu manages the container, not the value, so the value is never in
# the state file.
resource "aws_secretsmanager_secret" "manual" {
for_each = var.manual_secret_names
name = "${var.name_prefix}/${each.value}"
kms_key_id = aws_kms_key.secrets.arn
recovery_window_in_days = var.recovery_window_in_days
}
# --- Read access for one role -------------------------------------------------
locals {
secret_arns = concat(
[aws_secretsmanager_secret.generated.arn],
[for secret in aws_secretsmanager_secret.manual : secret.arn],
)
}
data "aws_iam_policy_document" "read" {
# The exact secret ARNs, not a wildcard. A new secret is unreadable until
# it is added here.
statement {
sid = "ReadTheseSecrets"
effect = "Allow"
actions = ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"]
resources = local.secret_arns
}
# The role may use the key only when the request comes through Secrets
# Manager, not to decrypt anything else.
statement {
sid = "DecryptThroughSecretsManager"
effect = "Allow"
actions = ["kms:Decrypt"]
resources = [aws_kms_key.secrets.arn]
condition {
test = "StringEquals"
variable = "kms:ViaService"
values = ["secretsmanager.${data.aws_region.current.name}.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "read" {
name = "read-app-secrets"
role = var.reader_role_name
policy = data.aws_iam_policy_document.read.json
}
output "generated_secret_arn" {
description = "ARN of the secret with the generated value."
value = aws_secretsmanager_secret.generated.arn
}
output "manual_secret_arns" {
description = "ARNs of the secrets you fill in by hand, by name."
value = { for name, secret in aws_secretsmanager_secret.manual : name => secret.arn }
}
output "kms_key_arn" {
description = "Key that encrypts the secrets."
value = aws_kms_key.secrets.arn
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.