Creating C# Serverless Project with .NET Core

In the previous section, we used Visual Studio and the AWS Toolkit to create our first AWS Lambda function with C#. Now we will create a more complex serverless project that contains Lambda functions and the API Gateway. We are going to use the AWS Toolkit and Visual Studio to create a serverless project. We will also explore the generated source code and deploy and test the project.

  1. Go to Visual Studio 2015 and create a new serverless project. Select File | New | Project.
  2. Select AWS Serverless Application and click OK.
  3. We are creating a little bit more complex application, so select the Blog API using DynamoDB.
  1. Our serverless project has been created. There are two files that have been generated for us, Blog.cs file and Functions.cs file, as shown here:

In the Functions.cs file, we find the function handlers that are used to implement the business logic for our Blog API. First, there is a constructor that sets up the context for our DynamoDB:

namespace AWSServerless1
{
public class Functions
{
const string TABLENAME_ENVIRONMENT_VARIABLE_LOOKUP = "BlogTable";

public const string ID_QUERY_STRING_NAME = " Id";
IDynamoDBContext DDBContext {get; set; }

public Functions()
{
var tablename =
System.Environment.GetEnvironmentVariable(TABLENAME_ENVIRONMENT_VARIABLE_LOOKUP);
if(!string.IsNullOrEmpty(tableName))
{
AWSConfigDynamoDB.COntext.TypeMappings[typeof(Blog)] = new
Amazon.Util.TypeMapping(typeof(BlogTable))
}
var config = new DynamoDBContextConfig {COnversion = DynamoDBEntryConversion.V2};

For example, in the previous screenshot, we retrieve the table name from an environment variable and set up the context for our DynamoDB client. Following this, you will see the main business functions, such as get blogs. You can also retrieve a blog identified by its blog ID. We read the blog ID from the path parameters:

public async Task<APIGatewayProxyResponse> GetBlogAsync(APIGatewayProxyRequest request, ILambdaContext)
{
string blogId = null;
if(request.PathParameters != null && request.PathParameters.ContainsKey(ID_QUERY_STRING_NAME))
blogId = request.PathParameters[ID_QUERY-STRING_NAME];
else if(request.QueryStringParameters[ID-QUERY-STRING_NAME]);

Further to this, we use the DynamoDB client to retrieve the blog from our DynamoDB table:

context.Logger.LogLine($"Getting blog (blogId}");
var blog = await DDBContext.LoadAsync<Blog>(blogId);
context.Logger.LogLine($"Found blog: {blog!= null}");

if(blog == null)
{
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.NotFound
};
}

In the following screenshot, we are preparing an API Gateway proxy response, so we set the HTTP status code, body, and headers in our code instead of setting these in our AWS Management Console. This is pretty similar to the approach in the serverless framework and Lambda proxy integration.

var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonConvert.SerializeObject(blog),
Headers = new Dictionary<string, string>{{"Content-Type", "application/json"}}
};
return response;

In addition, we have a function that adds the blog post and a function for removing a blog post.

In our Solution Explorer, we can see the serverless.template, which contains the serverless application model, as shown here:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "AWS Serverless API that exposes the add, remove and get operations for a blogging
platform"
"Parameters": {
"ShouldCreateTable"

This is basically an extension of the CloudFormation syntax that we use it to create AWS resources. For example, we specify the Lambda functions to get blogs, get a single blog identified by its ID, add blogs, and remove blogs. We also specify the blog table that is being created in DynamoDB.

Let's try it out. Right-click on the AWS Serverless1 in the Solution Explorer, and Publish to AWS Lambda; we can use the same account settings as before, as shown in the following:

Select the stack name and create a new bucket to which our CloudFormation code will be uploaded. Click Next.

We then need to enter the environment variables that will be used in our function code, as you have seen before. We also need to enter a table name for the blog table that is being created in DynamoDB. We call it CsBlogTable, as shown here:

The minimum settings for reading capacity and write capacity for DynamoDB are 1 and 1. Once that is done, click on Publish.

You will be able to see the current Status of the CloudFormation stack that will create our resources, which should end with Create_Completed.

If there was an error in your setup, you should get some info back on your console. Or you can go to the AWS Management Console dashboard, then head over to the CloudFormation dashboard, and see what has gone wrong. You can also delete your CloudFormations stack and all the resources that have been created here by going to your AWS Management Console and simply deleting the stack.

You can also go to the AWS Management dashboard and take a look what has been created.

Just copy the URL to our API and open Postman.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.54.149