18

I have created AWS Lambda Project in C# (NOT Serverless Application)

enter image description here

I have defined a Environment variable in aws-lambda-tools-defaults.json as below

"environment-variables": {
 "my-api": "http://myapihost.com/api/attendance-backfill"
 }

In Function.cs, fetching the value as below

   var apiUrl = Environment.GetEnvironmentVariable("my-api").ToString();

but it always coming as null.

How do I set & fetch Environment variable?

Thanks!

As per comment.

launch-settings.json

Function.cs

3 Answers 3

32

It's pretty close but after some digging around I found out how to actually set these for local runs using the Mock Lambda Test Tool. It is, in-fact, inside the launchSettings.json file. You want to drop the settings inside the Mock Lambda Test Tool section of the profiles node, not outside it.

{
 "profiles": {
  "Mock Lambda Test Tool": {
   "commandName": "Executable",
   "commandLineArgs": "--port 5050",
   "workingDirectory": ".\\bin\\Debug\\netcoreapp2.1",
   "executablePath": "C:\\Users\\%USERNAME%\\.dotnet\\tools\\dotnet-lambda-test-tool-2.1.exe",
   "environmentVariables": {
     "environment": "test"
   }
  }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

15
  1. Setting Environment variables docs used

There are two places where you’ll need to set environment variables: development-time and deployment-time. To do this, open the launchSettings.json file from under the Properties folder in the Solution Explorer. Then add the following JSON property:

    "environmentVariables": {
      "my-api": "something"
    }

To set environment variables at deployment-time, you can add these to the aws-lambda-tools-defaults.json file. (Just remember to escape the double quote marks.)

    environment-variables, its format is: "<key1>=<value1>;<key2>=<value2>;".

In your case you should have

    "environment-variables" : "\"my-api\"=\"http://myapihost.com/api/attendance-backfill\";"
  1. Consuming/fetching the environment variables

    Consuming the environment variables as part of the Lambda function’s logic is done intuitively in the C# code, by using the System library aws blog:

    System.Environment.GetEnvironmentVariable(<key>);
    

    In your case you can use the following;

    var apiUrl = System.Environment.GetEnvironmentVariable("my-api");
    

In this document it is suggested that your approach for fetching environment variable is correct.

    var variableValue = Environment.GetEnvironmentVariable("nameOfVariable"); 

5 Comments

Did you try again with just Environment.GetEnvironmentVariable("my-api"). Make sure your environment-variables are configured correctly. I found two documents which differ slightly aws thread and twenty6 doc on lambda variables
System.Environment.GetEnvironmentVariable("my-api") is returning null while the value is set in .json file
I updated my answer with some additional docs. Can you confirm that you fixed your aws-lambda-tools-defaults.json so that environment-variables parameter is correctly set?
I added the screenshot of launch-settings.json & Function.cs Still getting the value as null
For anyone who had this issue, it's environmentVariables (not environment-variables) in launchSettings.json
0

These solutions worked. Need to set environment variables in two places 1.) deployment purpose and 2.) test locally using mock tool

serverless.template file enter image description here

launchSettings.json file enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.