Skip to content

Configuration

Secrets

Keep API keys, passwords and tokens in AWS Secrets Manager in your account, and reference them in the config with $Secret('name'). The config holds only the name, so you can commit it. Secrets belong to an AWS region: every stack in that region can use them.

Create a secret

stacktape secret set stripe-key --region eu-west-1

The command asks for the value without showing it. Use the region you deploy to. In a script, pipe the value in, or pass --value or --file; see secret set.

You can also create, view, edit and delete secrets on the Secrets page of the Stacktape Console, per AWS account and region.

The New secret dialog in the Console: name, AWS account, region, value, optional description and the Create secret button
New secret creates the secret in the AWS account and region shown in the dialog.Open full size

Use it in a container

For web services, private services, worker services, multi-container workloads and Batch jobs, put secrets in secrets, not in environment. ECS or AWS Batch then fetches the value right before the container starts, so it never appears in the CloudFormation template or the task definition. Your code reads it as a normal environment variable, here process.env.STRIPE_KEY.

secrets: [{ name: 'STRIPE_KEY', valueFrom: $Secret('stripe-key') }]

Each valueFrom is exactly one $Secret(...) or $SsmParam(...) directive. SSM parameters used here must be SecureString. Stacktape lets the container read only the secrets and parameters it references. Keep the default AWS managed key on these secrets and parameters. With your own KMS key, the task fails to start: the values are fetched by the task execution role, which Stacktape creates without kms:Decrypt, and iamRoleStatements go to a different role (the one your code runs with). To use your own key anyway, allow kms:Decrypt in the key policy for callers in your account that come through Secrets Manager or SSM (the kms:CallerAccount and kms:ViaService conditions).

Warning:

$Secret() under a container's environment makes CloudFormation write the value into the task definition, where anyone who can describe the task definition reads it. Use secrets.

Use it elsewhere

Lambda functions have no separate secrets field. $Secret() in a function's environment works, but the value is stored in the function's configuration, visible to anyone who can read it in the AWS console. For sensitive keys, read the secret from Secrets Manager in your code instead.

Database fields take $Secret() directly, for example credentials.masterUserPassword of a relational database. The value goes to AWS as a CloudFormation dynamic reference ({{resolve:secretsmanager:...}}), never as plain text in the template.

JSON secrets

When a secret holds a JSON object, pass the key as the second argument: $Secret('stripe', 'apiKey') reads apiKey from { "apiKey": "...", "webhookSecret": "..." }. The first argument is the exact secret name, so a name such as billing.api works too. Stacktape 3 configs that write $Secret('stripe.apiKey') keep working. The deploy fails with an error when the value is not valid JSON or the key is empty.

When a secret does not exist

Before it changes anything, stacktape deploy (and stacktape diff) checks every secret the config references in the target region:

  • In a terminal, it lists the missing secrets and asks for their values, then creates them.
  • In a pipeline or for a coding agent, it stops with an error that names them. Create them with stacktape secret set and run it again.
  • Database passwords (masterUserPassword of a database, defaultUserPassword of Redis, the MongoDB Atlas admin password) are generated with secure random values. A terminal asks first; a pipeline generates them without asking.

A secret scheduled for deletion that the config still references is restored.

Update and rotate

Each deploy pins the secret version that was current at that moment. Changing the value in Secrets Manager does not change running workloads; deploy again to roll out the new version. This applies to rotation too: Stacktape does not set up rotation, and after AWS rotates a secret, the next deploy picks up the new version.

$SsmParam() in a container's secrets is the exception. ECS and Batch read the parameter when a task starts, without a version, so a task that starts after you change the parameter (a restart, a scale-out, a new job) gets the new value while older tasks keep the old one. A deploy notices the new version and replaces all running tasks. A rollback to an earlier deploy still reads the current value. Everywhere else, $SsmParam() is pinned to the version number at deploy time.

Commands that run locally read the current value instead: dev mode, scripts and hooks. Commands that connect to a deployed database, such as stacktape query-sql, use the version in the deployed connection string, so their credentials match the database.

stacktape secret get --show-secrets prints the current value. Before deleting a secret with stacktape secret delete, remove every $Secret() reference to it and deploy, or delete the stacks that use it. Otherwise their next deploy fails.

Secrets Manager or SSM Parameter Store

$SsmParam() reads a parameter from SSM Parameter Store instead. A standard SecureString parameter costs nothing to store and suits credentials that do not rotate. Secrets Manager charges per secret and per API call (pricing), and supports rotation and JSON keys.

$Secret()$SsmParam()
StoreAWS Secrets ManagerSSM Parameter Store
JSON keysYesNo
EncryptionAlways, with KMSSecureString parameters, with KMS
Pinned per deployThe version IDThe version number, except in container secrets

For values that are not secret, such as public URLs, feature flags or sizes, use plain config values or variables.

For different values per stage, use different secret names, such as staging-stripe-key and production-stripe-key, and build the name from stage in a TypeScript config.