‫So now we want to add a DynamoDB table
‫to our whole serverless docs to really make
‫our app fully serverless.
‫So, let's go back to the code we had from before.
‫And so as we can see, we have DynamoDB already in it.
‫So, this sounds about right.
‫So, let's go ahead and basically copy this code right here
‫and we're going to load the function.
‫And, here we create our boto3 clients.
‫So, something you do note is that we create the client
‫outside of the handler.
‫So, this is again, if you remember,
‫one of the best practice is to put the clients
‫outside of your handler.
‫It's very, very important.
‫Here, we load the table name
‫and the other thing we want to do is
‫set this boto3 client to be in the region name we are in.
‫So, here I'll say region name
‫equals os.environ
‫so we get another environ variable.
‫And, this time it is going to be called region name.
‫And, here I will say region name equals region name.
‫So, basically we set our DynamoDB client
‫to be equal to the region in which we deploy our function.
‫As we can see now, we are
‫referencing two environment variables,
‫region name and table name.
‫And, so we have to set them in the template.yaml.
‫So, how do we do this.
‫Let's have a look at how it was done in the examples file.
‫So, this is our lambda function.
‫We don't need this anymore.
‫And, here is out template.yaml.
‫And, so now we have to reference DynamoDB
‫and so, as you can see in the bottom,
‫there is a table that gets created for us
‫and this table of type simple table.
‫So, let's go ahead and copy this.
‫So, we'll copy this simple table right here, okay table.
‫And, the type is a simple table
‫but I want to set a few properties
‫because I don't want this table to cost you a lot of money.
‫And, so if we type "simpletable serverless options,"
‫that should be it.
‫Here, we go to simple table and we get the resource type,
‫and so we get the different patterns we can use.
‫One of them being, if you scroll down,
‫the provisioned throughput.
‫I want the provisioned throughput to be equal to one and one
‫just so we have limited version throughput
‫and we don't overwrite.
‫We can also set a primary key,
‫and so we can just copy this as well
‫so we get a full simple table that is going to be working.
‫So, let's copy these things.
‫We have the primary key in with the provisioned throughput.
‫The primary key is going to be called greeting
‫and it's going to be a string.
‫So, we just have a partition key.
‫And, for the provisioned throughput,
‫I said, well let's use two.
‫Two and two.
‫So, here is our simple table and so now we
‫need to set environment variables for our lambda handler.
‫For this, pretty easy.
‫Again, let's go back and see how it was done.
‫We use something called environment
‫and in there we can reference the variables we want.
‫So, let's just go to environment
‫and we're going to set it right here, environment.
‫And, I'll have to indent it, okay.
‫The variables, the first one is going to be table name
‫and I'm going to do a reference function,
‫so ref, like an Intrinsic CloudFormation function,
‫to the table that was created from before,
‫this simple table.
‫So, here we have table that's typed correctly
‫and the other environment variable that I said
‫is the region name.
‫And, for this, I'll set reference to AWS region,
‫which is a pseudo parameter, if you remember it,
‫this is a pseudo parameter that is provided
‫directly by the AWS CloudFormation templates.
‫Okay, so now we have our table name and our region name.
‫And, we have created a table, but if you remember,
‫when we do this, we need to give ourselves
‫the lambda function the access to this simple table.
‫For this, pretty easy.
‫Again, we go ahead and we need to add policies.
‫And these policies are going to be
‫IAM policies we can add on to our lambda function.
‫So, let's go and write our policies right here.
‫And the policy, once you add it, is DynamoDB crud policy.
‫And, the table name we reference has to be
‫for the table name that was created, so ref table.
‫And, this will basically give us, okay,
‫a DynamoDB policy that allows us to do crude operation
‫on the table that was created.
‫And, so we get the full IAM policy.
‫Now, in terms of what our application does, well,
‫maybe we don't want to return "hello world."
‫Maybe we want to do something
‫a little bit more interesting here.
‫So, we can use a scan operation.
‫So, DyanmoDB,
‫so dynamo
‫dot
‫scan
‫and then we have to reference a table name parameter,
‫so table name equals,
‫and we have to pass in the table name
‫that we have retrieved from before.
‫So, table name equals table name.
‫This is our scan result that is equals to this.
‫And, this is what we are going to pass.
‫We are going to respond with the scan result.
‫So, a pretty simple function.
‫We are just going to scan the table that was created.
‫So, that's it for our function.
‫It just scans, you know, our table
‫that is created from this simple table thing.
‫So, now let's go ahead and deploy our function,
‫so I'll just run these two things right here in my terminal.
‫And, so now we are in CloudFormation.
‫The cloud update is complete and if you look at
‫the resources being created,
‫we now have a DynamoDB table that was being created.
‫So, we have a table in DynamoDB.
‫Let's go into DynamoDB to see how things are.
‫So, DynamoDB,
‫here we go.
‫We go to tables, and here is my hello world sam table
‫who has a partition key named greeting
‫and it has two read capacity units
‫and two write capacity units.
‫Let's add a few items, so let's just create an item.
‫I'll say, greeting hello and then click on save.
‫This is our first greeting.
‫And, maybe I'll create another greeting called bonjour,
‫this is in my language, French.
‫So, we have greeting in bonjour,
‫but you can have other greetings if you wanted to.
‫So, we have two elements in our DynamoDB table,
‫which is pretty cool.
‫And, now what we want to do
‫is make sure that our function works properly.
‫So, let's go to our lambda function
‫and we'll refresh this page.
‫And, now if we scroll down
‫and look at the environment variables,
‫we can see that the region name was
‫automatically set to eu-west-3
‫and our table name was automatically
‫set to the table name that was created by DynamoDB.
‫So, our environment variables are correctly set.
‫And so, our code should be able to reference them.
‫And, it turns out that our code
‫has not been updated for some reason.
‫Let's have a look.
‫Nope, the code wasn't updated
‫because I probably forgot to save the code.
‫So, let's run this again.
‫Okay, so my code is now updated, which is great.
‫By the way, if you scroll down and look at the IAM role,
‫it has created a hello world sam, hello world IAM role,
‫so we could go to IAM to see what was done in there.
‫So, lets go to IAM,
‫click on that role,
‫and here is the role that was created by hello world sam
‫and it turns out it has a lambda basic execution role,
‫which is great, and also an inline policy
‫allowing it to do DynamoDB get item,
‫delete item, put item, scan, etc.
‫All on my hello world sam table.
‫So, this is pretty awesome.
‫So, sam framework just generated for us
‫the DynamoDB, the API gateway,
‫the lambda function, the IAM roles.
‫Everything is perfect,
‫so now hopefully if I go to my resources,
‫to the GET route and test it out,
‫I'll click on test and click on test again,
‫I get a 200, so it works,
‫and now it returns a response value,
‫greeting hello, greeting bonjour.
‫So, everything I add into my DynamoDB
‫will just be returned by the API gateway.
‫And so, overall that seems like a lot of things we did,
‫but we just put together the API gateway,
‫the lambda functions, the DynamoDB tables, the IAM policy.
‫All together, such as now I have an API
‫that I can call straight from Prod, for example.
‫And, if I go to this url, prod slash hello,
‫it will return to me all the greetings
‫that are in my DynamoDB table and you could play
‫and add more greetings here or remove them if you want.
‫So, that's it for this lecture on DynamoDB table.
‫I hope you liked it, and I will see you in the next one.