4

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 Answer 1

5

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

1
  • 2
    Thanks, that worked. For future reference: I made a second project in the same solution (console application with .NET Core), added a reference to the first (the Lambda), and invoked the function by instantiating its class and calling it directly. I did a test connection using the v3 AWS S3 SDK from Nuget, and it magically inherited my credentials from ~/.aws and worked fine. Dec 27, 2018 at 1:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.