To continue our series on the Serverless AWS Lambda Web API, we'll create the AWS Lambda Project in this tutorial. Read part 1 if you have not already.
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 Node.js 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”.
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 the Lambda
Delete the unneeded files and code to write a 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.
public class HelloController : Controller
public string Get(string name)