What’s Covered
- Serverless Template
- Create the ASP.NET Core Web API project
- Integrating Swagger UI
- Connecting to AWS DynamoDB
- Getting all Comments from AWS DynamoDB By Post
- Inserting Comment into AWS DynamoDB
- Deleting Comment From DynamoDB
Prerequisites:
- AWS account
- Knowledge of C#, Javascript, HTML and CSS
- .NET Core SDK and Runtime installed (if you haven’t got them, install them from the given links)
- .NET CLI
Architecture
Serverless Template
You can get the template from here. It is created from the command dotnet new -i Amazon.Lambda.Templates
and what it creates is a serverless function, API Gateway and DynamoDB table.
Create the ASP.NET Core Web API Project
- Open up terminal
- Check the dotnet verison installed (I had 3.1) by runninng
- Install Lambda and serverless template
- Create a ASP.NET Core Web API
cd
into thesrc
and then project’s folder
- Restore all the dependencies
- Run the project to make sure it’s working without errors
- Use VS Code or your favourite text editor and open the project folder
- Notice four new files in addition to the normal files new .NET project area created.
aws-lambda-tools-defaults.json
– This file is read by lambda tooling by default. Your function handler is specified in this file. So, if you update the function’s name, you need to update it in this file too.
LambdaEntryPoint
– Pretty much self explanatory (file from which lambda executes your application code) when you deploy to AWS
LocalEntryProint
– Local testing entry point when you test the application locally
serverless.template
– SAM template file in JSON which defines infrastructure as code.
Integrating Swagger UI
- Add the NuGet(like npm but for .NET development) package to use Swagger in your application
- To use it, you need to register it by adding the below code in the
ConfigureServices
method inStartup.cs
file
- Add import of
Microsoft.OpenApi.Models
on the top of the page which allows you to describes your API’s endpoints using Microsoft.OpenApi.Models;
- Then, enable it by inserting the below code in the
Configure
method ofStartup.cs
; file. Add the below code above theapp.UseHttpsRedirection();
Otherwise, Swagger UI won’t load.
- Run the application and then click url shown.
https://localhost:5001/
- Append
swagger
to the URL and press enter. - If everything went well, you should see Swagger UI now.
Connection to AWS DynamoDB
- Navigate to you project folder. We will add two NuGet packages. These are essential to make a connection to DynamoDB
AWSSDK.DynamoDBv2
– Adds the ability to interact with DynamoDB using AWS .NET SDK
AWSSDK.Extensions.NETCore.Setup
– has methods for configuration and registrations of AWS Services with dependency injection
- Now we need to register Dynamo Service. In the
Startup.cs
file, add the below code inConfigureServices
Code Explanation
AllowAnyOrigin
– We add CORS policy as the origin we will be accessing the DynamoDb will be a different one. Hence, specifying
services.AddAWSService<IAmazonDynamoDB>();
– registering the Interface IAmazonDynamoDB
- Delete the below line from the method:
services.AddAWSService<Amazon.S3.IAmazonS3>();
GET
functionality.Insert Record in DynamoDB
- Open AWS CLI and run the below command
Getting all Comments from AWS DynamoDB (By Post)
- Delete the two controllers, S3ProxyController and ValuesController in the controllers folde
- Right click on Controllers folder > New C# Class
- Name the class CommentsController
- Make sure the class is derived from ControllerBase
- Add the below line above the
class
keywords
- If you get compilation error (red squiggly lines) on the above three lines, this means that you need to add (click the squiggly line, a light bulb will appear, click bulb and choose the imports below respectively) or simply add below “using” statements at the top of the page:
- Add below code available here inside the class
Inserting Comment into AWS DynamoDB
- In your frontend, create a form (copy paste the below code from here)
- Add the Javascript / Ajax code to communicate to ASP.NET Core Web API from here
- That is all we needed to do in the frontend.
- Now to back to your ASP.NET Core Web API project
- Create a folder called Model and in it, create new C# class called Comments.cs
- Add the below code from here in the file
- Navigate to the CommentsController and add the below code from here.
[HttpPost]
– Annotation that the this method handles POST requests only[FromBody]
– Annotate thats take all the content from the bodyDeleting Comment From DynamoDB
-
- Manually by logging into AWS and deleting the record from the DynamoDB Table
- Click a button from the frontend.
CommentsController.cs
and do an AJAX request from the frontend on the click of a button provided that the comment that needs to be deleted is selected.