I am facing a problem where I need to include the deployment_env
tag with values of either dev
, test
, or prod
on all resources deployed to AWS within a CDK stack. All resources should have identical properties, except for this one tag. I attempted to utilize an environment variable called DEPLOYMENT_ENV
which seemed to work well at first. However, any value other than the specified ones still managed to pass through during synthesis and deployment in CDK. Additionally, when the environment variable is not defined, the TypeScript compiler fails to validate the undefined
or null
value, resulting in an error only upon assigning a value to the Tag must have a value
tag. This error should ideally occur earlier in the process. Here's the relevant code snippet:
#!/usr/bin/env node
import { App, Tags } from 'aws-cdk-lib';
import { EnvInitStack } from '../lib/foo-stack';
const deploymentEnv: 'dev' | 'test' | 'prod' = process.env.DEPLOYMENT_ENV as 'dev' | 'test' | 'prod';
const app = new App();
const fooStack = new FooStack(app, 'FooStack', {});
Tags.of(envInitStack).add('deployment_env', deploymentEnv as string);
- By running
DEPLOYMENT_ENV=foo cdk synth
, the CDK synthesizes and deploys successfully. - When running
cdk synth
, an error occurs statingError: Tag must have a value
.