Loading Configurations Based on Environment in AWS CodeDeploy
Managing environment-specific configuration across multiple clients and deployment environments needs a secure way to store configuration and fetch it in real time while the application is being deployed.
Architecture
Three pieces work together:
- AWS Secrets Manager — stores each environment’s configuration as a secret
- AWS CodeDeploy — handles deployment, with lifecycle hooks
DEPLOYMENT_GROUP_NAME— an environment variable available during deployment
The naming convention ties it together: a deployment group called Client-Dashboard-DG has a matching secret of the same name in Secrets Manager.
Implementation
appspec.yml
A bash script runs during the BeforeInstall lifecycle phase:
hooks:
BeforeInstall:
- location: scripts/getconfig.sh
timeout: 600
runas: root
getconfig.sh
Navigates to the deployment directory and runs a Node.js script that retrieves the secret.
The Node.js retrieval script
Uses the AWS SDK to query Secrets Manager by deployment group name, parses the returned JSON, and appends the configuration values to a .env file the application reads at runtime.
Error handling covers the common Secrets Manager failure modes — decryption failures, invalid parameters, missing resources — so a misconfigured deployment group fails loudly instead of deploying with a blank config.