Overview
Multi-environment deployment allows you to:- Isolate Resources: Each environment has its own separate resources
- Environment-Specific Settings: Different configurations per environment
- Cost Optimization: Use cost-saving settings in dev, high-availability in prod
- Safe Testing: Test changes in dev before deploying to production
Prerequisites
Multi-environment deployment requires:- Using the
parameter.tsfile (notcdk.jsonalone) - Defining environment-specific parameters
- Using the
-c envNameCDK context option
Configuration
Define Environments in parameter.ts
Editcdk/parameter.ts to define your environments:
If you define a “default” environment in
parameter.ts, it will override settings in cdk.json.Deployment Commands
Deploy Specific Environment
Deploy a specific environment using the-c envName option:
Deploy Default Environment
If no environment is specified, the “default” environment is used:Bootstrap Per Environment
Bootstrap each environment separately if deploying to different AWS accounts:Stack and Resource Naming
Stack Naming Convention
Environment-specific stacks are prefixed with the environment name:- dev environment:
dev-BedrockChatStack,dev-FrontendWafStack - staging environment:
staging-BedrockChatStack,staging-FrontendWafStack - prod environment:
prod-BedrockChatStack,prod-FrontendWafStack - default environment:
BedrockChatStack,FrontendWafStack(no prefix)
Dynamic Stacks (No Prefix)
Resource Naming
Only some resources receive environment prefixes: Prefixed Resources:- DynamoDB export table:
dev_ddb_export - Frontend Web ACL:
dev-FrontendWebAcl
- Most resources maintain original names
- Resources are isolated by being in different stacks
Resource Tagging
All resources are tagged with the environment name:Environment Isolation
Each environment creates completely separate resources:Separate Data
DynamoDB tables, S3 buckets, OpenSearch collections
Separate Users
Cognito user pools with independent users
Separate APIs
API Gateway endpoints and Lambda functions
Separate Frontend
CloudFront distributions with unique URLs
Environments in the same AWS account share the region but have isolated resources. You can also deploy to different AWS accounts for complete separation.
Multi-Account Deployment
Deploy environments to different AWS accounts for maximum isolation:Configure AWS Profiles
Deploy to Specific Account
Cost Optimization Strategies
Development Environment
Optimize costs for non-production environments:Production Environment
Prioritize availability and performance:Environment-Specific Workflows
Typical Development Workflow
CI/CD Pipeline Example
Managing Environment Outputs
Each environment deployment produces unique outputs:Environment Migration
To migrate data between environments:Export Bots from Dev
-
Use AWS CLI to export DynamoDB data:
-
Export S3 documents:
Import to Production
-
Import DynamoDB data:
- Trigger knowledge base re-indexing in the application
Consider using AWS Data Pipeline or AWS Glue for large-scale data migrations.
Monitoring Multiple Environments
CloudWatch Dashboards
Create environment-specific dashboards:- Go to CloudWatch Dashboards
- Create dashboard:
bedrock-chat-dev,bedrock-chat-prod - Add widgets for Lambda errors, API Gateway latency, etc.
CloudWatch Alarms
Set different alarm thresholds per environment:Cleanup
Destroy Specific Environment
Destroy All Environments
Best Practices
Use Consistent Naming
Use Consistent Naming
Use consistent environment names across all systems:
parameter.ts:dev,staging,prod- CI/CD pipelines: Same names
- AWS resource tags: Same names
Document Environment Differences
Document Environment Differences
Maintain a document describing:
- Purpose of each environment
- Access controls and permissions
- Cost expectations
- Deployment schedule
Test in Lower Environments First
Test in Lower Environments First
Always test changes in dev/staging before deploying to production:
Use Different AWS Accounts for Prod
Use Different AWS Accounts for Prod
Deploy production to a separate AWS account for:
- Better security isolation
- Separate billing
- Compliance requirements
Monitor Costs Per Environment
Monitor Costs Per Environment
Use AWS Cost Explorer with filters:
- Filter by tag:
CDKEnvironment = dev - Set up budget alarms per environment
- Review monthly cost reports
Regular Cleanup of Dev/Staging
Regular Cleanup of Dev/Staging
Periodically destroy and recreate dev/staging environments to:
- Reduce costs
- Test deployment process
- Remove old test data
Troubleshooting
Environment Not Found Error
parameter.ts:
Stack Name Conflicts
If you see stack name conflicts when deploying to the same account: Solution: Each environment uses a different stack name prefix. Ensure you’re using-c envName= correctly.
Cross-Environment Resource Access
Environments cannot access each other’s resources (DynamoDB, S3, etc.) by default. Solution: If needed, configure cross-stack references or use IAM roles to allow specific access.Next Steps
Parameters Reference
Learn about all available parameters
CDK Deployment
Understand CDK deployment process
CI/CD Integration
Set up automated deployments
Monitoring
Monitor your environments