.NET on AWS – Connecting with DynamoDB

Introduction

This is the third post on .NET on AWS series where we are taking a step by step approach to utilize and integrate AWS platform and services with our .NET Core applications.

In previous two posts, we covered the basics of .NET apps on AWS including connecting with RDS database. You can check those on following links:

Beside Relational Database systems, NoSQL databases are very popular and they have their place and valid use cases for different kind of applications. AWS DynamoDB managed service can be used for such requirements.

In this post, we will integrate DynamoDB with our API.

DynamoDB – Basics

DynamoDB is a Key-Value and document database that delivery single-digit milliseconds performance on any scale.

DynamoDB can handle more than 10 trillion requests per day and can support peaks of more than 20 million requests per seconds.

AWS Free Tier: 25 GB of storage and up to 200 million read/write requests per month.

Here is the link to official website for more details.

Integrating DynamoDB to .NET Core Application

I’ve previously written a post where we went through the basics of using DynamoDB with .NET Core application. I will be reusing same setup here. You can check the post for the details. In general it requires following steps:

  • Install NuGet Packages
  • Setup dependency injection (Startup.cs)
  • Controller code for reading/writing data

All these steps are explained in above mentioned post.

Here are the changes in the code:

With these changes in-place, we can run the application locally and can see that application can successfully read the data from DynamoDB table:

Publish API to AWS Lambda

After testing application locally, I published the API to AWS. But once, I tried to access the data through vehicles endpoint, it returns a 500 internal server Error:

and if we see the CloudWatch logs, it shows that our application is not authorized to access the DynamoDB:

But earlier we were able to access it when testing locally. So why the error now?

You may already know the reason, our application was able to access DynamoDB when running locally because I have AWS CLI/AWS Credentials setup on my local machine and AWS SDK was able to read and inject those to our application, however when deployed to Lambda, Lambda run-time does not have that credentials and hence can not access the DynamoDB table.

Lets see next, how we can solve this issue.

Updating the Policy

One way to solve this issue is to give our application necessary permissions using AWS Policies.

Following picture shows that I’ve updated the serverless.template file with a Policy “AmazonDynamoDBFullAccess” (though this policy is giving too much access to the application, but its ok for now for our demo)

With this change, we can again publish the application to AWS Lambda and this time our application is able able to read the data from DynamoDB successfully:

Here is the result array showing the data:

You can download the source-code from this git repository.

Summary

In this post, we took one more step and added DynamoDB connectivity to our .NET Core application. As you can see that AWS provided SDKs/tools make it very easy to integrate with AWS services.

We will cover more examples or DynamoDB uses in upcoming posts.

Let me know if you have some questions or comments. Till next time, Happy Coding.