AWS Secrets Manager adapter
Load configuration from an AWS Secrets Manager secret.
Loads the configuration from an AWS Secrets Manager secret.
Install
This adapter needs the @aws-sdk/client-secrets-manager peer dependency, if you don't have it already:
npm install @aws-sdk/client-secrets-managerThe adapter is asynchronous, so use it with loadConfig.
Usage
import { } from 'zod';
import { } from 'zod-config';
import { } from 'zod-config/aws-secrets-manager-adapter';
// JSON secret {"DB_HOST": "...", "DB_PASSWORD": "..."} is loaded as key-value pairs
const = await ({
: .({ : .(), : .() }),
: ({ : 'my-app/database' }),
});
// `key` exposes the whole secret as a string (like an env var)
const = await ({
: .({ : .() }),
: ({ : 'my-app/stripe-key', : 'STRIPE_KEY' }),
});Region, credentials and endpoint come from the AWS SDK defaults (AWS_REGION, AWS_ENDPOINT_URL, IAM role, ...). To configure them yourself, pass client: new SecretsManagerClient({ ... }).
IAM permissions: secretsmanager:GetSecretValue on the secret, plus kms:Decrypt on the key if the secret is encrypted with a customer-managed KMS key.
Remote reads cost a network call each time loadConfig runs. Load the config once and reuse it: see Load once.
Options
| Option | Type | Description |
|---|---|---|
secretId | string | Name or ARN of the secret. Required. |
key | string | Expose the raw secret value (as a string) under this key instead of merging its JSON entries. Parse it in the schema if needed, e.g. z.string().transform(JSON.parse). |
client | SecretsManagerClient | Client to use. Defaults to a shared client that resolves region and credentials through the AWS SDK default chain. |
Also accepts the shared adapter options: regex, silent, keyMatching and transform.