C# and AWS Lambdas, Part 2 – Web API and an API Gateway

Full source code available here.

In the previous post I created a lambda that executed a C# console application. In this post I walkthrough creating a .NET 3.1 Web API application inside a lambda and making it reachable from the web, just like a normal Web API application.

First, update the lambda templates, they have fixed a bug in the csproj file of the template, that I described in the in first post, and added some new templates.

dotnet new -i Amazon.Lambda.Templates::5.1.0

Another useful thing to install are the Amazon.Lambda.Tools package, but I don’t use them in this post.

dotnet tool install -g Amazon.Lambda.Tools 

Or update it if you have it installed already.

dotnet tool update -g Amazon.Lambda.Tools

Create a new application.

dotnet new serverless.AspNetCoreWebAPI --name WebAPILambda

In LambdaEntryPoint.cs change the class it inherits from to Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction.

For the sake of a demo I’m going to change very little, and show a GET and a POST call.

Add a Person class.

namespace WebAPILambda.Models
    public class Person
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
            return $"{FirstName} {LastName}";

Replace the ValuesController class with this –

public class ValuesController : ControllerBase

    public string Get()
        return $"Hello World from inside a lambda {DateTime.Now}";

    public string Get(int id)
        return $"You asked for {id}";

    public IActionResult Post([FromBody] Person person)
        return Ok($"You sent {person.ToString()}");

Build it. You can try it by starting the application locally and use like a normal Web API application.

Zip the contents of the WebAPILambda/bin/Debug/netcoreapp3.1 directory in a file named WebAPILambda.zip.

We’re done with the code.

Create the Lambda

I’m not going to show the detailed steps because they are described in part 1 of this series of posts.

But there is one difference – make this the handler WebAPILambda::WebAPILambda.LambdaEntryPoint::FunctionHandlerAsync.

Upload the zip as shown in part 1.

You should be able to paste in the content of the test/WebAPILambda.Tests/SampleRequests/ValuesController-Get.json from the source code into the lambda test feature, but the Json generated by the template does NOT work with a LambdaEntryPoint that inherits from APIGatewayHttpApiV2ProxyFunction.

That’s the lambda setup.

Now we need to send some web requests to it.

API Gateway
There are a few choices here, I am going to show how to use the Http API because it is simpler.

Click Create API.

Top of the list should be HTTP API, click Build.

Hit Add integration, choose Lambda, and pick the lambda function defined in the previous step.
Select Version 2.0.
Give the API a name.
Hit next.

For route configuration leave the METHOD as ANY.
Change resource path to /api/values – this matches the route in Web API controller in the application.

In Configure Stages don’t change anything for this simple example.
Hit next.

In Review and Create confirm that it looks correct and hit Create in the bottom right.

You should now have an API Gateway setup that looks something like –

Click on the Invoke URL and you should get a response like –

message: “Not Found”

Change the url to include the route of the controller and by adding /api/values and you will get –

That is hitting the GET method. In the attached source code there is file named test.http, it has an example of a POST method.

There you go, an API inside a lambda reachable from the web! Not so hard after you’ve done it once.

Full source code available here.

One thought on “C# and AWS Lambdas, Part 2 – Web API and an API Gateway

  1. Pingback: C# and AWS Lambdas, Part 7 – .NET 5 Web API inside a Container inside a Lambda, with API Gateway in front | no dogma blog

Leave a Reply

Your email address will not be published. Required fields are marked *