Environment Management
Environment-aware configurations ensure your infrastructure adapts to different deployment stages with appropriate settings for development, staging, and production environments.
Environment Context Files
Each environment has its own context file:
Project configuration (recommended)
Keep per-environment settings in your project config and reference them from your CDK app.
See:
Example: project.json with configurations
{
"name": "orders",
"targets": {
"deploy": {
"executor": "@mhshahzad/nx-cdk-deploy:deploy",
"options": {
"stack": "orders"
},
"configurations": {
"dev": {
"account": "111111111111",
"region": "us-east-1"
},
"staging": {
"account": "222222222222",
"region": "us-east-1"
},
"prod": {
"account": "333333333333",
"region": "us-east-2"
}
}
}
}
}
Your CDK app can read these values (e.g., via context or env vars) to select the right AWS environment.
Example: cdk.json context (optional)
{
"app": "npx ts-node --prefer-ts-exts src/main.ts",
"context": {
"orders:dev": { "account": "111111111111", "region": "us-east-1" },
"orders:staging": { "account": "222222222222", "region": "us-east-1" },
"orders:prod": { "account": "333333333333", "region": "us-east-2" }
}
}
In CI or locally, pass a selector (e.g., configuration name) that your app uses to look up the correct context.
Environment Variables and .env Files
Use .env files to store per-env values like table throughput, DLQ retention, API stage names.
Common patterns:
.env(defaults).env.dev,.env.staging,.env.prod
Your CDK code can load these with your preferred dotenv mechanism. See:
CDK Bootstrap per Account/Region
Run once per account+region pair:
cdk bootstrap aws://<ACCOUNT_ID>/<REGION>
Examples:
cdk bootstrap aws://111111111111/us-east-1 # dev
cdk bootstrap aws://222222222222/us-east-1 # staging
cdk bootstrap aws://333333333333/us-east-2 # prod
Deploy per Environment
nx deploy orders --configuration=dev
nx deploy orders --configuration=staging
nx deploy orders --configuration=prod
The deploy executor synthesizes and deploys to the specified AWS environment.
CI/CD Considerations
- Use OIDC to assume roles securely in CI.
- Parameterize the environment via job matrices (dev/staging/prod).
- Cache Nx/Node modules to speed up builds.
Best Practices
- Isolation: separate accounts for dev/staging/prod.
- Naming: include environment in resource names (e.g.,
orders-dev-table). - Policies: apply least-privilege IAM per environment.
- Config drift: keep all env config in code; avoid manual console edits.
- Cost controls: use smaller capacities in non-prod; enable TTL/retention where possible.