简体   繁体   中英

Accessing Environment variables in Serverless C#

I am working on Serverless project that runs with C#. I create the Boilerplate code from this repo. So my question is how I access the environment variables that are mentioned in the serverless.yml file for use inside the methods. My serverless.yml file looks like this.

service: doc-header

provider:
  name: aws
  runtime: dotnetcore3.1
  environment: 
    MONGODB_URL: ${ssm:/mongoUrl/${self:provider.stage}~true}

package:
  individually: true

functions:
  docHeaderUpdator:
    handler: AwsDotnetCsharp::AwsDotnetCsharp.Handler::TestFunction
    package:
      artifact: bin/release/netcoreapp3.1/hello.zip
    events:
      - sqs:
          arn: 
            Fn::GetAtt:
              - TestUserQueue
              - Arn
          batchSize: 10
          maximumBatchingWindow: 10

resources:
  Resources:
    TestUserQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "TestUserQueue-${opt:stage,self:provider.stage}"
        FifoQueue: true
        VisibilityTimeout: 200
        MessageRetentionPeriod: 102800
        ReceiveMessageWaitTimeSeconds: 5

I have tried using,

var mongoUrl = Environment.GetEnvironmentVariable("MONGODB_URL");

But seems that not working.

As Martin Costello explained in comments, Lambda Test Tool 3.1 cannot identify Environment variables set by the serverless.yml, even it cannot recognize the local environment variables set in your local device. If you deploy it to AWS and then serverless.yml can apply Environment Variables to the Lambda Runtime as same as normal NodeJS runtime.

If you need to test with Environment variables to the local environment as well without writing any other code by utilizing the same,

var mongoUrl = Environment.GetEnvironmentVariable("MONGODB_URL");

implementation. So in my case, I am using VSCode as debugging environment, So I can set Environment variables by adding this part to your launch.json file.

"env": {         
   "MongoDBUrl": "url"       
},

Full launch.json will look like this.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (console)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "/Users/sandunn/.dotnet/tools/dotnet-lambda-test-tool-3.1",
      // More information:
      // https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#configure-for-visual-studio-code,
      // https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#configure-for-visual-studio-for-mac
      "args": [],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "stopAtEntry": false,
      "internalConsoleOptions": "openOnSessionStart",
      // Add your Environment variables here.
      "env": {
        "MongoDBUrl": "url" 
      }
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM