How to access Lambda environment variable?

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


Try this:

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

public class EnviromentClass : IEnviromentVariables {
    public string getEnVariable(string variable) {
        return System.Environment.GetEnvironmentVariable(variable);
    }
}