How to create, read and wire Secrets Manager secrets for an ECS service with the console and the AWS CLI, what the execution role needs, how secrets reach your container, and how to get the same setup from application code in your own AWS account.

Secrets Manager is where API keys and database passwords go on AWS, and creating a secret takes one call. Getting the value into a running container takes more: a sensible name, a role that may read it, a reference in the task definition and a plan for changing it later.
This guide shows what that involves with the console and the AWS CLI, how it looks in Terraform for comparison, and how the same secrets are declared from application code.
Secrets are priced per secret and per call on the Secrets Manager pricing page, and ECS reads them at task start, as the ECS guide to Secrets Manager environment variables explains.
The console creates a secret from a form, and the AWS CLI does the same in one command per step, keeping the value out of your shell history when it is read from a variable:
For an ECS service the container definition points at the secret by ARN, and the task execution role, not the task role, needs permission to read it:
{
"containerDefinitions": [
{
"name": "billing",
"secrets": [
{
"name": "STRIPE_SECRET_KEY",
"valueFrom": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:prod/billing/stripe-secret-key-AbCdEf"
}
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:prod/billing/stripe-secret-key-*"
}
]
}
That setup works for one secret in one environment, and the staging copy, the second service, the customer managed key and the record of who changed what are all manual after that, which is the gap Terraform fills.
The secret and the read policy are two resources, with the value left to the CLI so it never lands in state:
resource "aws_secretsmanager_secret" "stripe" {
name = "prod/billing/stripe-secret-key"
recovery_window_in_days = 7
}
variable "execution_role_name" {
type = string
}
resource "aws_iam_role_policy" "read_stripe" {
name = "read-stripe-secret"
role = var.execution_role_name
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Action = ["secretsmanager:GetSecretValue"]
Resource = [aws_secretsmanager_secret.stripe.arn]
}]
})
}
The Secrets Manager with Terraform guide covers customer managed keys, write-only values and rotation in detail.
Encore brings infrastructure from code, so a secret is declared in the service that uses it, its value is set per environment from the CLI, and a deploy to your own AWS account stores it in Secrets Manager and injects it into that service, as the AWS infrastructure docs describe under secrets management. The value is never in the repository, so an AI coding agent working in the project can declare and use a secret without seeing the production value. Local values set for development are readable on the machine that runs encore run.

Run in your terminal to get started locally.
The customers folder also needs an encore.service.ts that exports new Service("customers"). The service reads a payment provider key and an admin token as secrets:
// customers/customers.ts
import { api, APIError, Header } from "encore.dev/api";
import { secret } from "encore.dev/config";
import { timingSafeEqual } from "node:crypto";
const stripeKey = secret("StripeSecretKey");
const adminToken = secret("AdminToken");
interface CreateRequest {
email: string;
}
export const createCustomer = api(
{ expose: true, method: "POST", path: "/customers" },
async ({ email }: CreateRequest): Promise<{ id: string }> => {
if (!email.includes("@")) throw APIError.invalidArgument("a valid email is required");
const res = await fetch("https://api.stripe.com/v1/customers", {
method: "POST",
headers: {
Authorization: `Bearer ${stripeKey()}`,
"Content-Type": "application/x-www-form-urlencoded",
"Idempotency-Key": `customer-${email.toLowerCase()}`,
},
body: new URLSearchParams({ email }),
});
if (!res.ok) throw APIError.unavailable("payment provider rejected the request");
const body = (await res.json()) as { id: string };
return { id: body.id };
},
);
interface AdminRequest {
token: Header<"X-Admin-Token">;
}
export const status = api(
{ expose: true, method: "GET", path: "/admin/status" },
async ({ token }: AdminRequest): Promise<{ ok: boolean }> => {
const a = Buffer.from(token ?? "");
const b = Buffer.from(adminToken());
if (a.length !== b.length || !timingSafeEqual(a, b)) throw APIError.unauthenticated("bad token");
return { ok: true };
},
);
Values are set per environment type, with encore secret set --type dev,local StripeSecretKey for development and encore secret set --type prod StripeSecretKey for production, and encore run reads the local values. The secrets docs cover environment types and per-environment overrides. All declared secrets must have a value before a cloud deploy goes through.
To deploy, follow Connect your cloud account and push, and Encore stores the values in Secrets Manager in your account and injects them as secret environment variables, so there is no policy or task definition to write by hand, and changing a value is another encore secret set.
Best fit: Teams that want API keys and passwords in Secrets Manager in their own AWS account, declared next to the code that uses them.
Run aws secretsmanager create-secret with a name such as prod/billing/stripe-secret-key and the value in --secret-string, or use the console. Update the value later with put-secret-value, which adds a new version without changing the secret's ARN.
Add a secrets entry to the container definition whose valueFrom is the secret ARN, and allow secretsmanager:GetSecretValue on that ARN for the task execution role. ECS reads the value when the task starts and sets it as an environment variable, so a changed secret needs a new task to be picked up.
Secrets Manager charges $0.40 per secret per month and $0.05 per 10,000 API calls, and new versions created by rotation are not charged. Fifteen secrets with light traffic come to about $6 a month.
Declare the secret in code, set its value with encore secret set, and deploy to your own AWS account. Encore stores the value in Secrets Manager and injects it into the service that declares it, with no policy or task definition to write by hand.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.