Writing data into DynamoDB using Lambda

Practical Serverless Development for Beginners AWS Lambda - Serverless Functions
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€46.60
List Price:  €65.24
You save:  €18.64
£39.85
List Price:  £55.79
You save:  £15.94
CA$68.41
List Price:  CA$95.78
You save:  CA$27.37
A$76.53
List Price:  A$107.15
You save:  A$30.61
S$68.03
List Price:  S$95.25
You save:  S$27.21
HK$390.80
List Price:  HK$547.16
You save:  HK$156.35
CHF 45.55
List Price:  CHF 63.78
You save:  CHF 18.22
NOK kr550.70
List Price:  NOK kr771.03
You save:  NOK kr220.32
DKK kr347.59
List Price:  DKK kr486.66
You save:  DKK kr139.06
NZ$84.03
List Price:  NZ$117.65
You save:  NZ$33.61
د.إ183.60
List Price:  د.إ257.05
You save:  د.إ73.45
৳5,487.36
List Price:  ৳7,682.75
You save:  ৳2,195.38
₹4,170.67
List Price:  ₹5,839.27
You save:  ₹1,668.60
RM238.57
List Price:  RM334.02
You save:  RM95.45
₦66,243.24
List Price:  ₦92,745.84
You save:  ₦26,502.60
₨13,919.64
List Price:  ₨19,488.62
You save:  ₨5,568.97
฿1,852.73
List Price:  ฿2,593.98
You save:  ฿741.24
₺1,618.89
List Price:  ₺2,266.58
You save:  ₺647.68
B$255.86
List Price:  B$358.23
You save:  B$102.36
R934.26
List Price:  R1,308.04
You save:  R373.78
Лв91.20
List Price:  Лв127.69
You save:  Лв36.48
₩68,991.47
List Price:  ₩96,593.58
You save:  ₩27,602.11
₪187.21
List Price:  ₪262.11
You save:  ₪74.89
₱2,883.99
List Price:  ₱4,037.82
You save:  ₱1,153.82
¥7,844.28
List Price:  ¥10,982.62
You save:  ¥3,138.34
MX$849.16
List Price:  MX$1,188.90
You save:  MX$339.73
QR182.39
List Price:  QR255.36
You save:  QR72.97
P685.83
List Price:  P960.21
You save:  P274.38
KSh6,625.24
List Price:  KSh9,275.87
You save:  KSh2,650.62
E£2,395.76
List Price:  E£3,354.25
You save:  E£958.49
ብር2,870.01
List Price:  ብር4,018.25
You save:  ብር1,148.23
Kz41,742.64
List Price:  Kz58,443.04
You save:  Kz16,700.40
CLP$47,160.37
List Price:  CLP$66,028.30
You save:  CLP$18,867.92
CN¥361.90
List Price:  CN¥506.69
You save:  CN¥144.79
RD$2,925.21
List Price:  RD$4,095.54
You save:  RD$1,170.32
DA6,709.48
List Price:  DA9,393.81
You save:  DA2,684.33
FJ$113.15
List Price:  FJ$158.42
You save:  FJ$45.27
Q388.57
List Price:  Q544.03
You save:  Q155.46
GY$10,460.61
List Price:  GY$14,645.70
You save:  GY$4,185.08
ISK kr7,004.59
List Price:  ISK kr9,806.99
You save:  ISK kr2,802.40
DH506.05
List Price:  DH708.51
You save:  DH202.46
L882.28
List Price:  L1,235.26
You save:  L352.98
ден2,874.42
List Price:  ден4,024.43
You save:  ден1,150
MOP$403.10
List Price:  MOP$564.37
You save:  MOP$161.27
N$937.41
List Price:  N$1,312.44
You save:  N$375.03
C$1,849.89
List Price:  C$2,589.99
You save:  C$740.10
रु6,678.31
List Price:  रु9,350.18
You save:  रु2,671.86
S/187.13
List Price:  S/262
You save:  S/74.86
K190.39
List Price:  K266.57
You save:  K76.17
SAR187.48
List Price:  SAR262.49
You save:  SAR75.01
ZK1,332.51
List Price:  ZK1,865.62
You save:  ZK533.11
L231.86
List Price:  L324.62
You save:  L92.76
Kč1,172.97
List Price:  Kč1,642.25
You save:  Kč469.28
Ft18,205.74
List Price:  Ft25,489.50
You save:  Ft7,283.75
SEK kr547.93
List Price:  SEK kr767.15
You save:  SEK kr219.21
ARS$43,829.95
List Price:  ARS$61,365.44
You save:  ARS$17,535.49
Bs346.26
List Price:  Bs484.79
You save:  Bs138.53
COP$194,628.58
List Price:  COP$272,495.59
You save:  COP$77,867
₡25,090.62
List Price:  ₡35,128.88
You save:  ₡10,038.25
L1,234.87
List Price:  L1,728.92
You save:  L494.05
₲372,339.59
List Price:  ₲521,305.22
You save:  ₲148,965.63
$U1,915.65
List Price:  $U2,682.06
You save:  $U766.41
zł201.49
List Price:  zł282.11
You save:  zł80.61
Already have an account? Log In

Transcript

Have a couple of lectures we have been to lambda and we know how lambda works. We saw event and context and how they work. In this lecture, we are going to add real data into this dynamodb table what we created some lectures back. And of course, to get access of this dynamodb there is an AWS SDK for JavaScript. Of course, if you are using dotnet, core Python that is different SDK, but what we are going to use is this AWS SDK. SDK gives you facility to access dynamodb or all other functionalities, like if you want to access different features of AWS, you can get access of those features using AWS SDK.

So let's let's let's include AWS SDK in our node j s function, lambda. So of course, this is a common j s. So we are including AWS SDK, you don't have to worry about any kind of NPM modules here, because that's a local lambda environment, and they take care of everything. Of course, this is some functionality about Dynamo DB document glide functionality and everything so you can explore. And I'm going to give all those links at the end of the lecture. So you can go through them, you can study them, and you can use those functionalities. So to write data into a dynamodb table, we need to use both functionalities or put is creating a new item.

So first of all, we need to create an instance of dynamodb document client so What we are going to do is create an instance of AWS dynamodb and document client. And since this lambda and this your dynamodb they are in the same region, so we don't have to specify any region at all. Of course you can also specify region and everything when you are defining or initializing your Dynamo DB client. So by using this syntax, you are able to access your Dynamo dB. just just just little bit of test during all the development so everything is working fine and you don't have any problem. So what we are going to do is we are going to just copy this functionality from this documentation support is responsible to write an item in Dynamo DB and what we are going to do is we are going to create couple of parents liters that needs passing.

And of course, we need to define all those parameters here. And parameters like this, like we have already created and seen in the previous lecture. So let, let's add those parameters so we can define what sort of information we need to store in our dynamodb table. And this document helps you a lot. So just refer to this document when you are in need, and you will get all the desired information you want. Let's probably so this is the table name.

So table name is that inventory table name. So here is that inventory table name we have defined. We don't need any sort of other other other details we are, we are interested to pass all this information here. So we'll just replace with this test the JSON data, what we created To JSON request body what we created a couple of lectures back. So we are setting up correctly that JSON and everything. So that's a you know syntactical requirement of a dynamo DB client document.

So we are setting up all the objects and these are just hard coded in the subsequent lectures we'll see how to get them dynamically. But for the sake of testing purpose, what we are going to do is we are going to just test it. So if we are going to store all this informations in database, of course, we are going to return await and new promise because of course this document client and Dynamo dB, they work. The work light is a sink and await functionality. So what we are going to create we are going to create a new promise and we'll move all those right functionality inside Promise. So once if, once this call is successful, we'll resolve with the correct status code.

And if and if it has failed for any reason. And then we can define what the reasons are. And let's let's just copy that request response what we have defined earlier. So if if the request is successful, what we are going to define a status code 200. And if something goes wrong, we can set status code as 400. And that is pretty generic in API development.

So we are kind of writing all microservices here using AWS lambda. And what we are also saying is that if if there is some error while writing into Dynamo dB, we are defining study scored as 400 It seems we haven't defined what what should come out so that I should be you know, blank because some some some area and also, let's probably pass the exact error what errors we are having on this occasion. Similarly, if the request is successful and the function is able to write successfully into dynamodb the status code will be defining as to not one did I call some data and since we don't have any editor, we can get rid of any editor here. Of course, we I think there is some error so what we need to do if there is a comma a little bit missing here let's let's prepare that.

And now I think we are ready to use this function. So, everything works fine we have defined promise we are using document client of Dynamo TV to write into To dynamodb table, let's save all this information here and try to test it. And this is the whole you can you can zoom in a bit you can get rid of unwanted code here. Let's go back so we can see what the responses are coming out from this lambda functions. Let's save it now. and test it a bit.

Now, the moment we test it, oh we see some error coming here. And the message it says is it is not authorized to perform dynamodb put item support item is a is a is a fun put item is on functionality what we want to write data into Dynamo DB and so what we are doing here, there Some missing functionality.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.