An Introduction to AWS DynamoDB (Accessing DynamoDB data from .NET Core AWS Lambda Function)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dotnet code central in today's video i am going to cover another serverless feature of aws in my last couple of videos i covered how to create aws lambda and then how to use aws lambda and api gateway to expose apis in today's video i am going to cover aws dynamodb and as a part of this video first i am going to explain what is dynamodb and its key features then i'm going to create a dynamodb table using aws console and then finally i'm going to access the table using dotnet core c sharp code first let's explain what is dynamodb dynamodb is an extremely fast server-less nosql database it is fully managed and can be set up to support multi-region active active mode it supports in-memory cache for highly scalable application though in-memory cache is a add-on feature and it doesn't come by default the aws dynamodb acceleration feature should be enabled to get the in-memory cache feature and it can support more than 10 trillion requests per day and up to 20 million request per second for a particular table and it can support acid transactions though i have not used it for sa transaction because most of the time in a micro services environment will have one table for one service transaction across multiple table is usually i managed using distributor transaction like saga pattern instead of using transaction at a table layer the other important factor of dynamodb is that dynamodb uses key value and document data model and because of that it supports extremely flexible schema in a lot of cases i have used a key value pair where key is an identifier and the value is an xml object whereas other cases i have used it like relational database table where i have created typed columns and used it in that manner as well now let's talk about few important concepts of dynamodb using read drives capacity we can manage the cost of dynamodb because dynamodb charges based on read write capacity write capacities are more expensive compared to read you can have more read and less right to keep the cost minimal and that's usually how most of the applications behave the read access is usually 70 on an average compared to right capacity along with that it provides an auto scale feature to unpredictable workload as well so we can either use a read write capacity mode we can use a auto scale feature a workload which can go up and come down using on demand capacity aws will auto scale instantly and of course it is more expensive compared to when we set auto scaling mode where we scale based on a percentage of load and then scale down and keep a consistent read write capacity for a normal workload dynamodb supports triggers capacity using lambda function and i have not personally used it but essentially you can create triggers like traditional rdbms which will trigger a aws lambda function and it will have the data pre-change as well as post change and then you can use that to have certain features in terms of keys there are two main concepts or two main aspect which is very important to understand about dynamodb is that dynamodb supports two types of key a partition key and sort key as primary keys partition key is what is used to create a partition on the table and sort key is what used to sort the data inside of the partition and when we go and create a table it will be more clear what partition key versus what is sort key and apart from that you can also create a secondary index on the table which can also be used apart from the partition key and sort key to support faster search of data inside of the table and apart from this there are a couple of other feature one is there is a support for point in time recovery for the dynamodb table apart from that it has support for on-demand backup and restore of the table so having said that let's now go and create a dynamodb table using aws console so once you search you can search from here for dynamodb and once you select this page is going to show up and then this is the dashboard and this is the tables right now i don't have tables any tables let's create a new table and as you can see the table starts with a primary key by default it's the partition key and then we can also add a sort key along with it as i mentioned if you consider the table a partition key is used to partition the data in the table and sort key is used to sort the data inside of partition so let's create a table called user table another thing to keep in mind is if we don't use sort key then the partition key has to be unique across the table so you have to give an identifier so here in this case the partition key can be email because it is unique across table but if you add a sort key then the partition key along with sort key has to be unique so given that this is an user table user information is going to be unique but the user city for example will be repeated across multiple users and if we want to have city as key search criteria what we can do is in the column we can have city as the partition key and for the sort key we can use email and we're going to click on create table and the table is getting created we can see the table is now created we can go into the item and here we can create new item so let's create a new item for cd we can give nyc and for email we can give abc8 email.com and let's add couple of other columns now in dynamodb apart from the primary key which is partition key and sort key or just the partition key the other columns are not needed to be added during table creation they can be done later on either through if you are adding the item through the console or if you are reading the item through code you can just add extra attribute and it will automatically add to the table so let's add another column and let's call it address and then we can also have another column which is going to be phone and now i'm going to save this so you can see the record is added now i'm going to go and create a new record i'm going to give the same city but i'm going to use another email and i'll have an address and i'm going to have the phone number save so now we have the record ready and then let's try to first use our existing lambda function and using the existing lambda function let's access this data and then we can also access it through the api gateway so let's go back to the lambda and this is our lambda function so here we can create a new class and we can say user provider so this is going to create a user provider class and then here let's make it let's create a model called user and the user model is going to have four property city email address phone these four properties is what it's going to have and now let's create function and and let's call it public task of user okay get users let's get all the users and so here now what we can do is for accessing these we need to use the dynamodb classes and namespaces for that i am going to install the dynamodb nuget package and this is the aws dynamodb v2 sdk is what i'm going to install once it is installed i'm going to go back here going to create a constructor and i'm going to expect i i amazon dynamodb it's part of this and then let's create a variable called dynamodb and then here what i can do is our result is equal to dynamodb dot and if i do a dot you can see there is a bunch of method it has things like create table delete item create item insert item updated a bunch of stuff we have to use scan item if we are getting all data from the table and if we are getting data based on a specific user then we will be using get item if we are expecting a single response and patch get item for multiple responses now here because we are trying to get all user i'm going to use can again scan is a expensive operation just like any other databases so we should try to avoid it but i'm showing it just to show you how it could be done and you can use it there are certain situation you might want to use it now it comes with as you can see it comes with an overload which takes scan request object so i'm going to use that that's a new scan request and for scan request i have to add the model's namespace amazon.dynamodbv2 and inside of scan request i'm going to pass the table name and our table name is user table so table and that's all we need to pass for the scan request that is nothing else is needed there are other things you can pass for example if you want to limit the number of record you can see the maximum number of items evaluated and then you can give an index name which we don't have we have not created an index you can have filter expression if you want to filter out item as a part of scan now filter expression is something we can use to filter out only user with a certain phone number if you want to know people in the same household and have the same phone number and things like that you can use filter expression there are of course other scenarios where filter expression will make sense projection expression is something that is used to project the data so this is more like select conditions select first name last name etc is like projection expression and filter expression is more like where condition so these are couple more important attributes that we should be aware of now let's say we got the data after we got the data the next thing we are going to do is we are going to check the items so if we say if the result is not equal to null and result dot items we have to use a weight here and a sink here now we can say result dot items it's not equal to null in that case what we will do is we will for each item in result dot items because it's going to be an error and then here we want to get all the attributes so we can say item dot try get value and for the key we're going to say city it's out var city similarly we'll have address and then we have email and then finally we have phone so this will give us all the value and then after we get all the value what we can do is we can create our users equal to new list user and then here we can do users dot add new user and then for every attributes here for address for example we are going to do address dot s stands for string and one thing i forgot to mention is that in dynamodb apart from saving singular items like string and object you can also save sets or areas so when you save set for example if it is a string array we'll be using ss which is an area of string and it will return a list of string similarly if it is a number array we will use ns as an array of number and n stands for a single number similarly bool is for boolean value and so on and so forth in our case it's going to be s for everything and the out variable i declared here they are nothing but the attribute value object provided by the dynamodb nuget package email dot s and then finally phone equal to 1 s and then after that here we can return users to array so users.2 array and then here finally we'll return array dot mt user that's about it this should return all the user from dynamodb and i'm going to extract an interface and let's rename the get user to get users async because it's an asynchronous function to follow the naming convention as it is returning a task and once it is done we can go back to the demo function here and here we can use the user so here i'm not going to change the existing implementation the parameter name and everything will remain here in the body instead of save sending this i'm going to fast create bar go to new of user provider and the user provider needs an amazon dynamodb object and this is where something important to keep in mind so we can create a new instance of aws dynamodb there are two ways of creating a new instance of amazon dynamodb client one thing is you can pass the xs secret and region and this is something you will use only if you are running the application from your local pc and you want to try to connect to the cloud but if you are running the application in the cloud then you just use the default constructor and internally the client will figure out based on the role what access it has so i'm going to use the default amazon dynamo db client object here and then after that i can say our users is equal to user provider dot get user async and then i'll have to use a weight here in which case i need to change this to async and task and once i do that here i can do json convert dot let me add the name space serialize object and for serialized object i'm going to pass the users here so that's all i have to do now you can use a di container instead of a lambda function so you don't have to create objects like this you can just get the object from di and pass in this case it does not make a lot of sense because we are really not doing much creating an object here is really not a problem it's not going to be a big deal but when we get into testing and all this kind of this implementation needs to change but for the interest of time i'm going to just go ahead and use this as is and then next i'm going to publish this function into aws and for the deployment i'm going to use the dotnet command and this is something i have shown in my last couple of videos so if you're watching this video for the first time i strongly suggest you watch the previous two video on lambda and here let me check what was the name of my lambda function just to make sure that i don't end up accidentally creating a new one the function name is lambda demo and then my profile is personal and region is e-west is too you can see this is the region here and ohio is us is too so i'm going to use that and run this command and this should build my lambda and then finally publish it in aws and as you can see it's updated the existing code and if we go here if we go inside we can see updated a few seconds back yeah it was updated 12 seconds back and now if i go to my api gateway here is the test api which i created and i can go and execute the get function and i already have a prod stage which i deployed which is here so i can execute this and if i execute you can see the response from dynamodb is showing up here ct is nyc email abc street phone and then the next city so as you can see within 15-20 minutes we were able to create aws dynamodb table add some data update the existing aws lambda function and then using api gateway we could access the data from dynamodb table so this is all i wanted to cover for today's video again dynamodb has lot of other interesting feature i've just shown the scan item feature here apart from this it has get item batch get items put item sync and things like that in my next video i'm going to cover few of this feature and i'm going to update my lambda function and my api gateway to post data using the post feature into the dynamodb and then get data using the get feature from my dynamodb table if you like this video please give me a thumbs up and if you are new to my channel and if you think you are getting value out of my challenge please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 3,384
Rating: undefined out of 5
Keywords: aws dynamodb, aws dynambodb lambda, aws dynamodb .net core, aws dynamodb c#, aws dynamodb aws lambda .net core, aws dynamodb aws lambda aws api gateway, aws dynamodb aws lambda aws api gateway .net core
Id: 9rlKByxBLaY
Channel Id: undefined
Length: 21min 23sec (1283 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.