Jack Musick
Jack Musick

Reputation: 345

Debug C# AWS Lambda Function Locally

I've been reviewing Amazon's documentation for C# and Lambda. I've tried using their yeoman generator and Toolkit for Visual Studio. My goal is to simply have a project that holds multiple functions that I can debug on my local machine. The problem is that running the debugger gives me a message about not having an entry point into the project, which makes sense given it's created as a class library.

From what I'm seeing, it looks like the development process is to write your code, deploy and run the function on AWS. Debugging would be reviewing the output from that and going from there. Is there a way to actually use the built-in debugger for Visual Studio, though? Better yet, I'd like a workflow for local debugging on my Mac. For example, in NodeJS, I can use the Serverless framework and set my launch.json file in VS Code to the following:

"configurations": [{
    "type": "node",
    "request": "launch",
    "protocol": "legacy",
    "name": "run hello",
    "program": "${workspaceRoot}/node_modules/.bin/sls",
    "args": [
        "invoke",
        "local",
        "--function",
        "hello",
        "--data",
        "{}"
    ]
}

This allows me to mock data and use breakpoints for debugging.

Upvotes: 7

Views: 19911

Answers (3)

SMartDev
SMartDev

Reputation: 112

With Visual Studio, you can install AWS's AWS .NET Mock Lambda Test Tool

Use the following command in the same directory as your Solution file:

dotnet tool install -g Amazon.Lambda.TestTool-2.1

Visual Studio automatically adds a Debug configuration so you can debug the project just like you would any other .NET app.

VS Code, Rider, and other instructions available in the The AWS .NET Mock Lambda Test Tool GitHub page

Upvotes: 7

jackofallcode
jackofallcode

Reputation: 1996

I haven't followed this guide, but it seems to talk about how to debug locally. It does go further into actually setting up on AWS if needed:

https://cloudncode.blog/2017/01/24/getting-started-with-writing-and-debugging-aws-lambda-function-with-visual-studio-code/

From the gist of it, it seems that you setup a Main entry point for your Lambda class that loads a test file from JSON. This is similar to how you would setup tests of your Lambda function on AWS.

Upvotes: 2

Jenn
Jenn

Reputation: 902

This AWS lambda documentation begins the topic of testing by invoking the lambda via the AWS CLI. This article has links for using the AWS CLI in different OS and IDEs.

Example Invoke call:

aws lambda invoke --invocation-type RequestResponse --function-name helloworld --region us-west-2 --profile adminuser

Tutorial with details of using Invoke

AWS Invoke API Reference documentation

Upvotes: 1

Related Questions