To make prebuilt SPA installers with CDK I like to keep environment configuration outside of the minified code via a config.json
file. This allows me to build the app once and move it between environments and stages.
The file contains settings like Cognito Pool Id, region, and/or branding information.
I used to write my own Custom Resource for building this file but recently discovered a simpler CDKV2 way! Credit: aws-cdk-v2-three-tier-serverless-application/
I am copy-pasting the relevant code for future reference.
1 | import { |
Pitfall
I have a config.json
in the SPA deploy assets also, so with each rebuild and redeploy, CloudFormation was replacing and overwriting the config.json
from SPA assets. I added this to the myConfig
object above to force the custom resource to execute with each cdk deploy
1 | buildTime: new Date().toISOString(); |