In this tutorial, we're gonna learn how to connect an AWS lambda function to
DynamoDB so the first thing we're going to do is create a table in DynamoDB. So,
let's first go to our DynamoDB service and create a new table. So, this is the
DynamoDB AWS console you can view your tables here. I have a few tables already.
This is the dashboard and there's a button on the dashboard to create a new
table. So create a table and name your table. In this example we're going to
have our table called message and we need to choose the primary key for our
message table, in this case, I'm going to have our primary key be a GUID that
comes from a request ID. So, we're going to call this messageId and a GUID will
be a string. Keep everything else as default and you create.
So here is your new message table the table name is message we have our
primary key as message ID everything here is our table setup with all its
details and this is actually the Amazon resource name if we were going to
restrict our lambda functions to write to a specific table only we would need
to have this resource name for our example we're going to have it be able
to write to all tables so now let's set up our lambda function going back to our
AWS management console I'm going to go to our lambda
so I have some functions already but we're going to make a new function and
this is going to write a message to our message table in the creative function
and it's going to be from scratch and the function name is going to be right
message and what this function will do is create a new message in our message
table on DynamoDB our runtime info will be no js' that we're going to click
create function
so now we have successfully created our new function it's called bright message
decisions the sample nodejs code that Amazon already fills in for us we're
going to change this so that it can write to the dynamodb and we're going to
just change it within this editor so the first thing we're going to do is import
in the code that we need to so that we can use the Amazon SDK so we're gonna
make a Const call this AWS and we're going to tell it to require the AWS SDK
then we need to connect to our dynamo DB so you're gonna declare Const this will
be called DD be our dynamo database equals new and then we're going to use
our ABA AWS that we create it so new AWS dot 0 to be document client and then
we specify the region our DynamoDB existing so in this case our region is a
USB stash one we created it in North Virginia that is Amazon's
us - East that's - one we're gonna write in curly braket region us - East - one
semicolon and a line break but now we need to modify our handler to write to
dynamo dB so we have our handler it's going to
have an async event and we're going to also add in a context and a callback
context is the data that's being sent in to us and callback is what's actually
going to be returned we do not need this code so we're gonna write as our message
ID a gooood and what I'm going to use right now is the AWS request ID that is
coming in from context in a production environment you wouldn't use this do
this you would normally generate a brand new good but for the sake of this
example we're going to just stick with an easier example so we're gonna just
use that request ID so I'm going to declare a constant request ID equals
context dot AWS request ID I'm now going to make a new function within JavaScript
they work with promises and observables this function is going to return a
promise so I'm gonna make a new function and I'm gonna call this create message
and it will take in the request ID what we're gonna do is write to the DynamoDB
for our table name message so we're going to set up some parameters that
we're going to be passing over so I like to make Const params equals and we set
up our parameters of what we're writing so the first thing that's expected is
the table name so they say table name and our table name was message comma
then the item this is what we are writing to the table so the one thing
that is required is the message ID so message ID and that will be the request
ID we also want to store a message so let's
say a lowercase message right hello from lambda now this would be where you would
use your contacts to get a message that a user sent over but for this purposes
of this tutorial we're gonna stick to it being simple and just hard-coded in this
sets up the parameters now we need to write it to the database so in order to
write to the database we will go ddb dot put we pass in the parameters and we
need to return something so in this case I want to return a promise so I'm going
to write return a DB put brands dot promise so this is the function that
we'll set up to write to our database put is going to place those items into
our database message doesn't exist but DynamoDB is a no sequel database this
will be added when we write it to the database so we need to call create
message so we write a wait create message we pass in our request ID and
then we use then
we're going to return something so if it's not an error we are going to use
our callback to return a respond or going to say callback it's not an air so
we're going to put a null and the body will be our status code so we inserted
something into the database so we're going to return a status code of 201
so status code 201 we're not going to have a body so it's just going to be an
empty body so body empty and we want to enable our cross origin is course so
we're going to do headers and then our access control Wow it'll be so this is
our callback this will be the response that comes back what if there's an error
well let's handle that as well so if there's an error we want to catch the
air so we'll catch specifically a parameter called air and we are going to
write to the console the air so we're going to write console dot air and we'll
write the error message so this is all the code we need to write to the
database but we are not done yet if we try to test this code right now we're
going to get rejected when writing to DynamoDB if I make a test we can just
use this sample one test we don't need any ending in our body so I'm going to
create this so make sure we save and then let's test
and we get an air back saying our access was do not denied for our role right
message so let's edit our right message role so that we can write to the
dynamodb we're gonna scroll up go to permissions we're going to click our
right message role and this will open up our right message role and with that we
have a policy this is the AWS basic execution rules and what we want to do
is add on to this so we're going to edit this policy or click Edit policy add
additional permissions we're going to choose a service we're going to choose
dynamo DB and in this example we're going to give permission to write to all
tables on our dynamo DB so all dynamo DB accesses we want it to read write scan
and with that we need to give it permission to all resources now if we
want it to restrict it we can restrict it down to a specific table and that's
when if we go back to our dynamo DB you would need your information of your a RN
or amazon resource name and that's where you can restrict it to only being able
to query one table going back to our permissions here I'm going to say all
resources though this is set up completely and
we're gonna say review policy there's our DynamoDB with full access to
all resources we're gonna click Save Changes it has been updated now with
dynamo DB so let's go back to our right message lambda function and let's test
it again and see if it succeeds so we're gonna click test and we get back the
status code is 201 and it's all successful let's go over to our dynamo
DB go to items tab we can refresh this view and there is our message hello from
lamda so now we've learned how to write to a dynamo dB let's learn how to read
from the dynamo DB I'm going back to lamda and I'm going to make a new lambda
function and this is going to be called read message to function read message we
create our function
now the setup for reading from our data from our database is going to be very
similar to writing to our database - how we query the information so let's copy
from the other area where we declared our AWS and ddb I'm going to add that to
the top of our index j s and now we need to modify our exports so exports top
handler we're going to take the exact same async signature that we used
earlier so that's events context and callback now what we need to do is
create a method that scans our database in this case we are going to call our
dynamo DB and just get back ten of the messages in the database if there is ten
we're gonna make a method I'm gonna remove this code we're gonna write a
wait and this will be called read message it will take no parameters and
we're gonna have our it's gonna be a promise so we'll have our thing that
will return back some form of data which will be what we're querying otherwise it
will have an air so then whenever we're going to do otherwise we catch our air
and we'll do the same thing we did in the last example so we'll walk the air
console dot and so let's define the function read message function read
message no parameters and our parameters are going to be similar to our create
message so I'm actually going to use the exact same parameters but there's Kant's
pram I'm having my table name message I'm not inserting any items we're going
to be using a limit so limit
10 now I'm going to call dynamodb and scan the database so this is going to
return d DB dot scan passing in our params dot promise so this is going to
return back data this time so this will have a list of items that we are going
to out print but we're going to say data dot items these are the items being
returned for each we make a function
item it's the code block will do console dot
blog and we'll say item dot message this would be for the one item that we have
in the table it would say hello from lambda so let's save this if we run this
code right now the lambda function does not have the correct permissions the
role that the lambda functions executing on so let's change our role so let's
scroll up let's go to our permissions and we can either update this read
message to have permissions to read from the DynamoDB or we can edit our
execution role and choose our write message will so let's choose right
message and we'll hit save and now our read message and right message are
executing under the same role so if we test our function to get our results
back we're not returning anything with our call back but we log hello from
lambda so now you've learned how to read from dynamodb so you might be saying
wait a minute I don't want to just log it to the console
I want the response to come back through our API
well let's modify our callbacks so you do call back
this is when it is successful so how long no and then defining our body so
this case our status code should be 200 it's successful but we didn't insert
anything into the database tool same status code 200 the body is going to be
the items in our database that we are returning back so we'll say body data
items because that's what we're doing here we're doing our data items for each
and reloading them now our data items is what's going to be our body and we add
our header so headers I'm just gonna copy it from our other lambda function
headers there's our access control origins for all the click safe and let's
test this you get our test and now we have our response with each message in
there if there's multiple message this will be curly brackets comma and then
the next message so in congratulations you have learned
how to read and write to dynamo DB using AWS lambda functions