Leveraging CDK Context Variables in C# Lambda Initialization Code

I have a .NET Lambda function written in C# that is implemented as a .NET Minimal API according to the guidance provided here. To define AWS resources, I am utilizing CDK (TypeScript).

Within my build pipeline, there is shell scripting involved to supply a parameter for stage definition (e.g. dev vs. qa vs. beta vs. prod) with the command:

cdk synth -c "stage=${StageName}"

When accessing this variable in my CDK code, I use the following syntax:

const stage = this.node.tryGetContext('stage');

This variable determines which environment-specific settings from cdk.json are used when setting up the stack (such as endpoints).

My query pertains to passing this variable into the startup logic of the Lambda function. While I can pass it within the body of the function itself as a parameter, I require it to be available in the application's startup logic executed prior to entering the function body (for configuring the appropriate appsettings.{env}.json file).

Can this be achieved through the CDK?

Answer №1

After a bit of troubleshooting, I believe I have found the solution.

By adjusting the .NET environment variable within the lambda.FunctionProps's environment field, I was able to successfully make it work, like so:

environment: {
    'stage': stage
}

This environment variable can then be accessed within a .NET application just like any other, for example:

var stage = Environment.GetEnvironmentVariable("stage");

I was pleasantly surprised to discover that the CDK could handle .NET environment variables in this manner!

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

Exploring Feature Management in Blazor WASM

Are there any resources available to assist with configuring features in a Blazor WASM project? I am aware of the Microsoft packages: Microsoft.FeatureManagement.AspNetCore and Microsoft.FeatureManagement. Implementing feature management on the server si ...

C# - Dealing with a system.stackoverflow exception

My code is causing a stackoverflow exception when using an if/else statement. Here is an example of the code: if (driver.FindElements(By.XPath("//*[@id='modal']/div/div/div/p[contains(text(), 'Hello World')]")).Count != 0) { Conso ...

How to Pass Values in handleChange Event in Typescript

I have noticed that most online examples of handling change events only pass in the event parameter, making the value accessible automatically. However, I encountered an error stating that the value is not found when trying to use it in my handleChange fun ...

verify this condition prior to executing the for loop in javascript

When working with a queue in Typescript (ES6) set to run on an interval of 1 ms, it's important to consider the most efficient approach for performance. 1. setInterval(() => { //if (this._queue.filter(a => !a.running && a.cbs.length) ...

What is the best way to transform HeadersInit into an Object<string,string> data type?

In short, I am faced with the task of converting the headers of a RequestInit into a format that another library can comprehend. This particular library requires the headers to be in the format of Object<string, string>. Initially, I attempted someth ...

Creating a generic class for third-party API requests in NestJS

I'm working on a NestJS application that involves making third-party API requests. Every function requires the same code to retrieve data, causing repetition. How can I streamline this process by creating a common class for handling GET or POST reque ...

Angular: Stop additional input from being processed in Child Component and disable Change Detection

Is there a way to limit the number of inputs a child input in Angular receives before stopping further changes? For example, I want the child input to accept only 3 updates from ngOnChanges and then ignore any subsequent ones. I am currently using an inpu ...

Getting a precise item in JSON with varied key signatures

Given the following JSON data: var responses = {customer : {results: 2938; id: 9283}, bredesh : {results: 2938; id: 248} }; I am trying to use an if statement in my HTML template : <div ng-if="response.(customer and bredesh and any new element (Is ...

Discovering SQL Server availability through C# and ASP.NET 4.0

My current setup involves a client-server architecture with one server and four client machines, each running SQL Server connected via LAN. I am looking to verify whether all four client databases are successfully connected or not. Within my web.config f ...

AJax and MVC: Elevating Your File Uploading Experience

I have come across numerous examples of file uploading, but most of them involve using forms or stand alone file uploads where only the file itself is passed. Currently, I have a page that sends data to a controller via Ajax. Now, I want to incorporate a ...

Press the key to navigate to a different page

I have an input field for a search box. I want it so that when I enter my search query and press enter, the page navigates to another page with the value of the input included in the URL as a query string. How can I achieve this functionality? Thank you ...

Error TS2322: Type 'Partial<T>' is not assignable to type 'T'

I'm struggling to articulate my problem, so I think the best way to convey it is through a minimal example. Take a look below: type Result = { prop1: { val1: number, val2: string }, prop2: { val1: number } }; f ...

Ways to verify if a list of items includes an object with particular attributes

My task involves dealing with a list of duplicates objects: let duplicates = workspace.Maps.GroupBy(m => m.sFolder).SelectMany(grp => grp.Skip(1)).ToList(); To achieve this, I need to implement an if statement that can verify if the list contains a ...

Guide on assigning JSON response values to TypeScript variables in Angular 4

I'm just starting with Angular 4 and I'm attempting to retrieve a JSON value using http.post. The response I'm receiving is: {"status":"SUCCESS"} component onSubmit(value: any) { console.log("POST"); let url = `${this.posts_Url}`; t ...

Vue HeadlessUI Error: The function vue.defineComponent is not recognized

Trying to incorporate @headlessui/vue into my nuxt project has been a challenge. My attempt at using it looks like this: <template> <Menu> <MenuItems> <MenuItem>Item</MenuItem> </MenuItems> </Menu&g ...

Steps for referencing a custom JavaScript file instead of the default one:

Currently, I am utilizing webpack and typescript in my single page application in combination with the oidc-client npm package. The structure of the oidc-client package that I am working with is as follows: oidc-client.d.ts oidc-client.js oidc-client.rs ...

Creating a personalized theme for Material UI 5.0 using Typescript with React

Having some trouble customizing a theme in Material UI 5.0 with typescript. theme.ts import { createTheme } from '@mui/material'; declare module '@mui/material/styles' { interface Theme { custom: { buttonWi ...

Ways to utilize array reduce for organizing information

Can someone please guide me on how to use the reduce array method to sort entries by date in the following data: const entries = [ {date: 'lu'}, {date: 'lu'}, {date: 'ma'}, {date: 'ma'} ] I would like the ou ...

Exploring assembly in-code revelations within library scripts

I implemented a code snippet in a shared library to handle internationalization. The concept is to use a markup extension to retrieve values by providing the fully qualified RESX file name: resx:ResxProperty.Name="SampleApp.Common.Resources.MainWindow ...

Struggling to access the "this.array" variable within a TypeScript-powered Angular 4 application

I cannot access the this.array variable in my TypeScript-Angular 4 application. The error is being thrown at this.services.push because this.services is undefined. My code looks like this: export class ServersComponent implements OnInit { //Initializi ...