Validating environment variable values in an AWS CDK TypeScript project

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);
  1. By running DEPLOYMENT_ENV=foo cdk synth, the CDK synthesizes and deploys successfully.
  2. When running cdk synth, an error occurs stating Error: Tag must have a value.

Answer №1

To ensure the accuracy of your 'deploymentEnv' value, it is recommended to validate it using JavaScript code instead of TypeScript definition. You can achieve this by incorporating the following snippet:

if (!['dev','test','prod'].includes(deploymentEnv))
    throw ('invalid deploymentEnv tag');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to apply JavaScript object destructuring but make changes to certain values before assigning them to a new object?

After receiving movie data from an api, I am currently manually creating a new object with a subset of properties and modified values. Is there a more efficient way to achieve this using javascript/typescript object destructuring syntax? I specifically wa ...

Retrieve the final variable in an Observable sequence

In my code, I have a variable called 'messages' which stores messages from a conversation: messages: Observable<Message[]>; To populate the 'messages' variable, I do the following: const newMessage = new Message(objMessage); ne ...

Using createContext in React.tsx to pass the state through useState

There is a context called Transaction that accepts an object and a function as parameters. In the AppProvider component, the Transaction.Provider is returned. The following code snippet is from the GlobalState.tsx file: import { createContext, useState } f ...

What is the method for filtering out specific fields in a template string?

I am currently working on defining constraints for the method field type event = { [k: `on${string}`]:(e:string)=>void } However, I need the event argument to be a number for fields that do not begin with 'on' type event = { [k: ` ...

Tips for creating a cookie for an alternate website upon launching a new browser tab

Hey there, I'm facing an issue that I could really use some help with. To give you some context, I'm working on a project using Angular and TypeScript. My goal is to implement single sign-on functionality for multiple websites within one applica ...

Ensuring Consistency of Values Between Child and Parent Components

Is there a way to ensure that the value of submitted in the child component always matches the value of submitted in the parent component? Appreciate any help! @Component({ selector: 'child-cmp', template: ` child:{{submitted}} ...

What is the best way to design a circular icon using OpenLayers?

I am currently incorporating openlayers into my ionic app and working on placing users on the map. I have encountered a problem as I am unsure how to apply custom CSS styling to the user element, which is not directly present in the HTML. In the screenshot ...

What could be the reason for my inability to reach function attributes?

Struggling with accessing properties in a function that involves callback functions. const example = ( callback: (...args: unknown[]) => unknown ): void => ({ name: callback.name // <- errors // ... }) Encountering issues in typescript d ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

Determine the sum of the values in a column using Angular

I have a collection of objects with a 'quantity' field that I want to aggregate and display on a review screen within a table. My array consists of administrations with various object fields, and my focus is on calculating the total of the &apos ...

Ensure that multiple URLs within an object are properly sanitized instead of just focusing on a single

I am working with an object that contains success, summary, and detail elements, which are used to display messages in PrimeNG message component (p-messages) after a record is created. Once the record is created, I invoke the displayMessage method to set t ...

How can I verify the "Type" of user input in a custom React TypeScript hook?

Creating a customized hook to fetch JSON data from an API is a task I am currently working on. The challenge is that I intend to use this hook for various types of data, so I need a mechanism to specify the type each time I utilize the hook. Depending on t ...

Exploring Typescript: Uncovering the Secrets of the navigator.connection Property

I am trying to access the NetworkInformation interface by using a simple TypeScript function like the one shown below: private checkNetworkConnection(): void { const connection = Navigator.connection || navigator.mozConnection || navigator.webkitConn ...

What is the best way to dynamically generate a component and provide props to it programmatically?

I am interested in creating a function that can return a component with specific props assigned to it. Something like a reusable component for Text/View/Pressable, where styles can be extracted and passed as props. Personally, I find it more efficient to s ...

Tips for waiting for an Http response in TypeScript with Angular 5

Is it possible to create a function that can retrieve a token from a server, considering that the http.post() method generates a response after the function has already returned the token? How can I ensure that my function waits for the http.post() call t ...

Is there a way to convert my messages into different languages without relying on the 'translate' directive or pipe?

Currently, my Angular application is set up with ngx-translate for translation purposes. While it is currently monolingual, I am already looking ahead to the possibility of needing to translate it in the future. Everything is functioning perfectly, but I w ...

Comparing the functions of useMemo and the combination of useEffect with useState

Is there a benefit in utilizing the useMemo hook instead of using a combination of useEffect and useState for a complex function call? Here are two custom hooks that seem to function similarly, with the only difference being that useMemo initially returns ...

Export an array of objects using the Angular XLSX library

Here is my example data: exampleData: any[] = [ { "id": "123", "requestType": "Demo", "requestDate": "12/05/21", "status": "Success", "product": [ { "productName": "example product A", "productQty": "8" ...

Using Mocks in a NestJS Program to Enhance Contract Testing

Challenge I am currently exploring ways to launch a NestJS application with mocked providers. This is essential for conducting provider contract tests, as a service needs to be initiated in isolation. When using the Pact library for testing the provider, ...

Utilizing a class structure to organize express.Router?

I've been playing around with using Express router and classes in Typescript to organize my routes. This is the approach I've taken so far. In the index.ts file, I'm trying to reference the Notes class from the notes.ts file, which has an en ...