Create a C # Lambda project and debug it in Visual Studio for Mac

3 minute read

Introduction

This is a record of creating a C # Lambda project on a Mac and debugging a test project with Visual Studio for Mac.

What I was able to do

  • Create a C # Lambda project
  • Deploy to AWS Lambda
  • Testing with AWS Lambda
  • Solution with Visual Studio for Mac
  • Debug execution of test project

I didn’t attach and debug a program that runs on AWS Lambda.
I debugged the test project on my local PC.

Preparation

Set access key ID and secret access key

Set the access key ID and secret access key so that you can access AWS from your PC.

  • AWS CLI installation
    Please refer to the following URL for the installation procedure.
    Reference: https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-chap-install.html

  • Setting
    Set the access key ID and secret access key referring to the following URL.
    Reference: https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html

.NET Core CLI

  • Add project template
    Run the dotnet new -i Amazon.Lambda.Templates command to add AWS Lambda templates to the .NET Core CLI.

  • Install Amazon.Lambda.Tools .NET Core Global Tool
    Run the dotnet tool install -g Amazon.Lambda.Tools command.

Reference: https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/csharp-package-cli.html

Creating a project

Run the command to create an empty Lambda project.
dotnet new lambda.EmptyFunction --name {method name}
(Hereafter, the explanation is based on the assumption that “Select Function” is specified in {method name}.)

Various files are generated, but when you execute the Lambda function, the Function.FunctionHandler () method of Function.cs will be called.
By default, the character string received by the argument input is converted to uppercase and returned as the return value.

Function.cs


using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace SelectFunction
{
    public class Function
    {
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(string input, ILambdaContext context)
        {
            return input?.ToUpper();
        }
    }
}

Reference: https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/csharp-package-cli.html

Deploy

Let’s deploy.

  • Change to the directory where the .csproj file is located.
  • Execute the dotnet restore command.
  • Run dotnet lambda deploy-function.

  • ʻEnter AWS Region:` is displayed, so enter the region to deploy to.
  • ʻEnter Function Name:` is displayed, so enter the Lambda function name you want to create.
  • Select IAM Role that to provide AWS credentials to your code: and a list of IAM roles will be displayed. Select the IAM role you want to use.

If the deployment is successful, you’ll see the Lambda function you created in the AWS Lambda console.
スクリーンショット 2020-09-21 8.49.12.png

Tested on AWS Lambda

In the AWS Lambda console, select the Lambda function you deployed and click Set Test Event.
スクリーンショット 2020-09-21 8.49.59.png

Enter an arbitrary name in the “Event name” field, enter the information to be passed to the argument in the edit area below it in Json, and click the save button.
スクリーンショット 2020-09-21 8.50.49.png

If you select the name you entered in the “Event name” field and click the test button, the return value of the Function.FunctionHandler () method of Function.cs will be displayed in the execution result field.
スクリーンショット 2020-09-21 8.51.39.png

Solution with Visual Studio for Mac

I want to code and debug in Visual Studio for Mac, so I create a solution.
First, create an empty solution.

スクリーンショット 2020-09-21 8.17.17.png

Specify the location of the solution according to the directory structure created in Create Project (#Create Project). Feel free to choose the directory structure.
スクリーンショット 2020-09-21 8.23.37.png

In the solution window, right-click the solution name → select Add → Existing Project to add the Lambda project and its test project.
スクリーンショット 2020-09-21 9.03.10.png

Run the test

Set the test project as the startup project.

You can debug the test method by displaying the unit test window, right-clicking the test method, and clicking [Debug Test].

スクリーンショット 2020-09-21 9.07.48.png