X

This site uses cookies and by using the site you are consenting to this. We utilize cookies to optimize our brand’s web presence and website experience. To learn more about cookies, click here to read our privacy statement.

Creating a Serverless AWS lambda webapi with AWS Toolkit – Part 2

To continue our series on the serverless AWS Lambda webapi, we’ll create the AWS Lambda Project in this tutorial.

Creating the AWS Serverless application from template

To create a new project, go to “File -> New -> Project”. You should see all the available AWS projects types in C#. There is a template for Nodejs under root “AWS” as well.

To create an AWS Lambda in .net core, go to “AWS Lambda” under “Visual C#” then select “AWS Serverless Application (.Net Core)” as shown below. Call the project HelloWorld.

 

 

After clicking create, select a blueprint — basically a template with a base code. Select the “ASP.NET Core Web API” option and click “Finish”.

 

Template structure

The structure of the template is very simple. To reach lambda function, go through “API Gateway”. This is a mandatory component to trigger a lambda function from outside AWS. Here, we are not using any auth service, but it is provided in AWS by a service called Cognito.

Lastly, as seen below, this template allows the application to save and get objects/files from S3 storage, which isn’t needed in this tutorial.

 

 

At this point you should be able to see the template files in the “Solution Explorer” as below. Also, notice  some new files:

  • cs : One of the most important files as this class contains the Init() method which will be invoked first once we trigger the lambda function
  • cs: This class allow us to run the project locally for debugging and it uses Kestrel webserver as a host
  • template: This file will have all the configuration for the project, it will have a pointer to the LambdaEntryPoint class and in this template it will have the configuration for S3 as well since it has been used in the controller for this template

 

 

Writing lambda

Delete the unneeded files and code to write lambda. Start by deleting the two controllers, then navigate to serverless.template and remove all S3 configuration.

 

 

After removing all the S3 bucket configuration and references, the resources object should look like this:

 

 

Since you deleted the two controllers that came with the template/blueprint, go ahead and create a new API empty controller and call it HelloController. Also, create two Get methods, with and without a parameter.

 

namespace HelloWorld.Controllers
{
       [Route("api/[controller]")]
       public class HelloController : Controller
       {
            [HttpGet]
            public string Get()
            {
                return "Hello! there";
            }

            [HttpGet("{name}")]
            public string Get(string name)
            {
                return $"Hello! {name}";
            }
      }
}

Click F5 to run the webapi locally as any regular .net core app and test it right in the browser. Because this is a Get action, you can navigate to http://localhost:57002/api/hello to get the first greeting, or pass a parameter http://localhost:57002/api/hello/world to get the greeting with a name.

 

Next in our blog series: Operationalize Solution in AWS.