Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Lambda environment variable?

When running a .net core 2.1 AWS Lambda function, it is simple to fetch an environment variable from the AWS Lambda Console in c# using:

var envVariable = Environment.GetEnvironmentVariable("myVariableName");

However, when running ASP.NET core 2.1 as a Serverless Application on AWS Lambda, this does not work (the above returns null).

I can set a local env variable in the launchSettings.json file, but I want to use the Env variable from the AWS Lambda console.

How can I access the AWS Lambda Env variable in ASP.NET Core 2.1?

like image 714
Pete Lunenfeld Avatar asked Aug 04 '18 19:08

Pete Lunenfeld


People also ask

How do I access my AWS environment variables?

Sign in to the AWS Management Console and open the Amplify console . In the Amplify console, choose App Settings, and then choose Environment variables. In the Environment variables section, choose Manage variables. In the Manage variables section, under Variable, enter your key.

How do I access environment variables?

Do so by pressing the Windows and R key on your keyboard at the same time. Type sysdm. cpl into the input field and hit Enter or press Ok. In the new window that opens, click on the Advanced tab and afterwards on the Environment Variables button in the bottom right of the window.

Can Lambda layer access environment variables?

You are right that there is no concept of environment variables for Lambda Layers.


2 Answers

How can I access the AWS Lambda Env variable in ASP.NET Core 2.1

You access it the same way you would as before.

var envVariable = Environment.GetEnvironmentVariable("myVariableName");

Ensure that the environment variable is set for the respective resource so that it is available when called.

Each resource would have an entry in the serverless.template file, which is the AWS CloudFormation template used to deploy functions.

Environment variable entries are found under the Resources:{ResourceName}:Properties:Environment:Variables JSON path in the file.

Example declaration

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Transform" : "AWS::Serverless-2016-10-31",
  "Description" : "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
  "Parameters" : {
  },
  "Conditions" : {
  },
  "Resources" : {
    "Get" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "TimeZoneService::TimeZoneService.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore1.0",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 60,
        "Role": null,
        "Policies": [ "AWSLambdaFullAccess" ],
        "Environment" : {
          "Variables" : {
            "myVariableName" : "my environment variable value"
          }
        },
        "Events": {
          "PutResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          }
        }
      }
    }
  },
  "Outputs" : {
  }
}

Reference Build and Test a Serverless Application with AWS Lambda

Reference Creating a Serverless Application with ASP.NET Core, AWS Lambda and AWS API Gateway

like image 193
Nkosi Avatar answered Oct 12 '22 05:10

Nkosi


Try this:

public interface IEnviromentVariables {
    string getEnVariable(string variable);
}

public class EnviromentClass : IEnviromentVariables {
    public string getEnVariable(string variable) {
        return System.Environment.GetEnvironmentVariable(variable);
    }
}
like image 39
Eduardo Díaz Avatar answered Oct 12 '22 05:10

Eduardo Díaz