How do I run C#-based AWS Lambda functions locally for development?

Viewed 1584

I'd like to write an AWS Lambda function in C#. My eventual goal will be to make an Alexa skill, but it looks like the ASK CLI doesn't support C# yet.

My development environment consists of:

  • Ubuntu 18.10 x64
  • JetBrains Rider with the AWS Toolkit plugin installed
  • A US-based AWS account with everything running in us-east-1
  • AWS CLI installed and configured
  • dotnet-sdk-2.1 and dotnet-runtime-2.1 installed

I can't seem to find any useful documentation on how to write a Lambda function in C# and run it on my computer directly for testing. I've done this before with Node.js using lambda-local which just inherits my AWS credentials from the AWS CLI, but the code runs on my own computer.

I've tried using the AWS Toolkit plugin in Rider, but when I try to create a run configuration for AWS Lambda Local, the runtimes list is empty:

enter image description here

So, how can I get a development environment set up that lets me run C# Lambda functions locally for testing with breakpoints and whatnot, using the same credentials presumably as the AWS CLI, and also eventually build and deploy to AWS?

I'm open to using another IDE like VSCode if this makes it easier, but I really want to avoid running Windows to develop my code which means avoiding Visual Studio proper.

1 Answers

You can create a console application, reference lambda project and run lambda in it.

If you have any environment variables you have to use Environment class. To better manage configs in your application it'd good to write a wrapper (smth like ApplicationConfig) for environment-dependent variables.

To setup env variables in Console runner use

Environment.SetEnvironmentVariable("Environment", "local");

And to retrieve:

Environment.GetEnvironmentVariable("Environment");

ApplicationConfig.cs

public class ApplicationConfig
{
     public string Environment => GetValue("Environment");
     public string GetValue(string key) => Environment.GetEnvironmentVariable(key);
} 

Also, you can set a default value for ApplicationConfig properties if there is nothing in the config

Related