1 00:00:00,120 --> 00:00:03,100 ‫So now we want to add a DynamoDB table 2 00:00:03,100 --> 00:00:05,380 ‫to our whole serverless docs to really make 3 00:00:05,380 --> 00:00:07,823 ‫our app fully serverless. 4 00:00:07,823 --> 00:00:11,060 ‫So, let's go back to the code we had from before. 5 00:00:11,060 --> 00:00:14,820 ‫And so as we can see, we have DynamoDB already in it. 6 00:00:14,820 --> 00:00:16,250 ‫So, this sounds about right. 7 00:00:16,250 --> 00:00:21,250 ‫So, let's go ahead and basically copy this code right here 8 00:00:21,370 --> 00:00:23,520 ‫and we're going to load the function. 9 00:00:23,520 --> 00:00:26,370 ‫And, here we create our boto3 clients. 10 00:00:26,370 --> 00:00:29,910 ‫So, something you do note is that we create the client 11 00:00:31,470 --> 00:00:32,883 ‫outside of the handler. 12 00:00:33,740 --> 00:00:35,350 ‫So, this is again, if you remember, 13 00:00:35,350 --> 00:00:38,660 ‫one of the best practice is to put the clients 14 00:00:38,660 --> 00:00:39,860 ‫outside of your handler. 15 00:00:39,860 --> 00:00:41,320 ‫It's very, very important. 16 00:00:41,320 --> 00:00:43,750 ‫Here, we load the table name 17 00:00:43,750 --> 00:00:45,210 ‫and the other thing we want to do is 18 00:00:45,210 --> 00:00:49,050 ‫set this boto3 client to be in the region name we are in. 19 00:00:49,050 --> 00:00:52,380 ‫So, here I'll say region name 20 00:00:52,380 --> 00:00:54,260 ‫equals os.environ 21 00:00:54,260 --> 00:00:56,440 ‫so we get another environ variable. 22 00:00:56,440 --> 00:01:00,180 ‫And, this time it is going to be called region name. 23 00:01:00,180 --> 00:01:03,500 ‫And, here I will say region name equals region name. 24 00:01:03,500 --> 00:01:05,670 ‫So, basically we set our DynamoDB client 25 00:01:05,670 --> 00:01:09,070 ‫to be equal to the region in which we deploy our function. 26 00:01:09,070 --> 00:01:10,070 ‫As we can see now, we are 27 00:01:10,070 --> 00:01:12,580 ‫referencing two environment variables, 28 00:01:12,580 --> 00:01:14,100 ‫region name and table name. 29 00:01:14,100 --> 00:01:17,960 ‫And, so we have to set them in the template.yaml. 30 00:01:17,960 --> 00:01:19,030 ‫So, how do we do this. 31 00:01:19,030 --> 00:01:23,210 ‫Let's have a look at how it was done in the examples file. 32 00:01:23,210 --> 00:01:24,730 ‫So, this is our lambda function. 33 00:01:24,730 --> 00:01:26,070 ‫We don't need this anymore. 34 00:01:26,070 --> 00:01:28,630 ‫And, here is out template.yaml. 35 00:01:28,630 --> 00:01:31,380 ‫And, so now we have to reference DynamoDB 36 00:01:31,380 --> 00:01:33,030 ‫and so, as you can see in the bottom, 37 00:01:33,030 --> 00:01:36,510 ‫there is a table that gets created for us 38 00:01:36,510 --> 00:01:39,840 ‫and this table of type simple table. 39 00:01:39,840 --> 00:01:41,880 ‫So, let's go ahead and copy this. 40 00:01:41,880 --> 00:01:46,680 ‫So, we'll copy this simple table right here, okay table. 41 00:01:46,680 --> 00:01:48,540 ‫And, the type is a simple table 42 00:01:48,540 --> 00:01:50,230 ‫but I want to set a few properties 43 00:01:50,230 --> 00:01:52,820 ‫because I don't want this table to cost you a lot of money. 44 00:01:52,820 --> 00:01:57,773 ‫And, so if we type "simpletable serverless options," 45 00:01:59,380 --> 00:02:00,460 ‫that should be it. 46 00:02:00,460 --> 00:02:05,460 ‫Here, we go to simple table and we get the resource type, 47 00:02:06,290 --> 00:02:08,600 ‫and so we get the different patterns we can use. 48 00:02:08,600 --> 00:02:10,210 ‫One of them being, if you scroll down, 49 00:02:10,210 --> 00:02:12,150 ‫the provisioned throughput. 50 00:02:12,150 --> 00:02:16,130 ‫I want the provisioned throughput to be equal to one and one 51 00:02:16,130 --> 00:02:18,247 ‫just so we have limited version throughput 52 00:02:18,247 --> 00:02:20,170 ‫and we don't overwrite. 53 00:02:20,170 --> 00:02:22,490 ‫We can also set a primary key, 54 00:02:22,490 --> 00:02:25,120 ‫and so we can just copy this as well 55 00:02:25,120 --> 00:02:28,020 ‫so we get a full simple table that is going to be working. 56 00:02:28,020 --> 00:02:29,450 ‫So, let's copy these things. 57 00:02:29,450 --> 00:02:32,420 ‫We have the primary key in with the provisioned throughput. 58 00:02:32,420 --> 00:02:35,780 ‫The primary key is going to be called greeting 59 00:02:35,780 --> 00:02:37,200 ‫and it's going to be a string. 60 00:02:37,200 --> 00:02:39,500 ‫So, we just have a partition key. 61 00:02:39,500 --> 00:02:40,980 ‫And, for the provisioned throughput, 62 00:02:40,980 --> 00:02:42,710 ‫I said, well let's use two. 63 00:02:42,710 --> 00:02:43,790 ‫Two and two. 64 00:02:43,790 --> 00:02:46,600 ‫So, here is our simple table and so now we 65 00:02:46,600 --> 00:02:49,480 ‫need to set environment variables for our lambda handler. 66 00:02:49,480 --> 00:02:51,010 ‫For this, pretty easy. 67 00:02:51,010 --> 00:02:53,620 ‫Again, let's go back and see how it was done. 68 00:02:53,620 --> 00:02:56,100 ‫We use something called environment 69 00:02:56,100 --> 00:02:58,700 ‫and in there we can reference the variables we want. 70 00:02:58,700 --> 00:03:00,660 ‫So, let's just go to environment 71 00:03:01,770 --> 00:03:04,970 ‫and we're going to set it right here, environment. 72 00:03:04,970 --> 00:03:06,520 ‫And, I'll have to indent it, okay. 73 00:03:06,520 --> 00:03:09,830 ‫The variables, the first one is going to be table name 74 00:03:09,830 --> 00:03:11,850 ‫and I'm going to do a reference function, 75 00:03:11,850 --> 00:03:15,360 ‫so ref, like an Intrinsic CloudFormation function, 76 00:03:15,360 --> 00:03:17,110 ‫to the table that was created from before, 77 00:03:17,110 --> 00:03:18,870 ‫this simple table. 78 00:03:18,870 --> 00:03:20,920 ‫So, here we have table that's typed correctly 79 00:03:20,920 --> 00:03:24,520 ‫and the other environment variable that I said 80 00:03:24,520 --> 00:03:25,760 ‫is the region name. 81 00:03:25,760 --> 00:03:30,540 ‫And, for this, I'll set reference to AWS region, 82 00:03:30,540 --> 00:03:33,500 ‫which is a pseudo parameter, if you remember it, 83 00:03:33,500 --> 00:03:35,610 ‫this is a pseudo parameter that is provided 84 00:03:35,610 --> 00:03:40,260 ‫directly by the AWS CloudFormation templates. 85 00:03:40,260 --> 00:03:43,230 ‫Okay, so now we have our table name and our region name. 86 00:03:43,230 --> 00:03:46,430 ‫And, we have created a table, but if you remember, 87 00:03:46,430 --> 00:03:49,040 ‫when we do this, we need to give ourselves 88 00:03:49,040 --> 00:03:52,800 ‫the lambda function the access to this simple table. 89 00:03:52,800 --> 00:03:54,330 ‫For this, pretty easy. 90 00:03:54,330 --> 00:03:57,140 ‫Again, we go ahead and we need to add policies. 91 00:03:57,140 --> 00:03:58,650 ‫And these policies are going to be 92 00:03:58,650 --> 00:04:02,030 ‫IAM policies we can add on to our lambda function. 93 00:04:02,030 --> 00:04:05,160 ‫So, let's go and write our policies right here. 94 00:04:05,160 --> 00:04:09,410 ‫And the policy, once you add it, is DynamoDB crud policy. 95 00:04:09,410 --> 00:04:12,980 ‫And, the table name we reference has to be 96 00:04:12,980 --> 00:04:16,980 ‫for the table name that was created, so ref table. 97 00:04:16,980 --> 00:04:19,530 ‫And, this will basically give us, okay, 98 00:04:19,530 --> 00:04:23,860 ‫a DynamoDB policy that allows us to do crude operation 99 00:04:23,860 --> 00:04:25,460 ‫on the table that was created. 100 00:04:25,460 --> 00:04:28,490 ‫And, so we get the full IAM policy. 101 00:04:28,490 --> 00:04:32,150 ‫Now, in terms of what our application does, well, 102 00:04:32,150 --> 00:04:34,000 ‫maybe we don't want to return "hello world." 103 00:04:34,000 --> 00:04:34,930 ‫Maybe we want to do something 104 00:04:34,930 --> 00:04:36,510 ‫a little bit more interesting here. 105 00:04:36,510 --> 00:04:38,750 ‫So, we can use a scan operation. 106 00:04:38,750 --> 00:04:40,150 ‫So, DyanmoDB, 107 00:04:40,150 --> 00:04:41,360 ‫so dynamo 108 00:04:41,360 --> 00:04:42,920 ‫dot 109 00:04:42,920 --> 00:04:43,890 ‫scan 110 00:04:43,890 --> 00:04:45,880 ‫and then we have to reference a table name parameter, 111 00:04:45,880 --> 00:04:47,500 ‫so table name equals, 112 00:04:47,500 --> 00:04:49,010 ‫and we have to pass in the table name 113 00:04:49,010 --> 00:04:50,340 ‫that we have retrieved from before. 114 00:04:50,340 --> 00:04:52,680 ‫So, table name equals table name. 115 00:04:52,680 --> 00:04:56,300 ‫This is our scan result that is equals to this. 116 00:04:56,300 --> 00:04:58,130 ‫And, this is what we are going to pass. 117 00:04:58,130 --> 00:05:01,950 ‫We are going to respond with the scan result. 118 00:05:01,950 --> 00:05:03,100 ‫So, a pretty simple function. 119 00:05:03,100 --> 00:05:06,410 ‫We are just going to scan the table that was created. 120 00:05:06,410 --> 00:05:08,300 ‫So, that's it for our function. 121 00:05:08,300 --> 00:05:11,080 ‫It just scans, you know, our table 122 00:05:11,080 --> 00:05:14,700 ‫that is created from this simple table thing. 123 00:05:14,700 --> 00:05:16,930 ‫So, now let's go ahead and deploy our function, 124 00:05:16,930 --> 00:05:20,923 ‫so I'll just run these two things right here in my terminal. 125 00:05:24,380 --> 00:05:25,751 ‫And, so now we are in CloudFormation. 126 00:05:25,751 --> 00:05:27,300 ‫The cloud update is complete and if you look at 127 00:05:27,300 --> 00:05:29,220 ‫the resources being created, 128 00:05:29,220 --> 00:05:33,160 ‫we now have a DynamoDB table that was being created. 129 00:05:33,160 --> 00:05:35,380 ‫So, we have a table in DynamoDB. 130 00:05:35,380 --> 00:05:37,950 ‫Let's go into DynamoDB to see how things are. 131 00:05:37,950 --> 00:05:39,147 ‫So, DynamoDB, 132 00:05:40,420 --> 00:05:41,500 ‫here we go. 133 00:05:41,500 --> 00:05:45,410 ‫We go to tables, and here is my hello world sam table 134 00:05:45,410 --> 00:05:47,740 ‫who has a partition key named greeting 135 00:05:47,740 --> 00:05:50,070 ‫and it has two read capacity units 136 00:05:50,070 --> 00:05:51,910 ‫and two write capacity units. 137 00:05:51,910 --> 00:05:54,880 ‫Let's add a few items, so let's just create an item. 138 00:05:54,880 --> 00:05:58,400 ‫I'll say, greeting hello and then click on save. 139 00:05:58,400 --> 00:05:59,660 ‫This is our first greeting. 140 00:05:59,660 --> 00:06:01,940 ‫And, maybe I'll create another greeting called bonjour, 141 00:06:01,940 --> 00:06:03,860 ‫this is in my language, French. 142 00:06:03,860 --> 00:06:04,960 ‫So, we have greeting in bonjour, 143 00:06:04,960 --> 00:06:07,990 ‫but you can have other greetings if you wanted to. 144 00:06:07,990 --> 00:06:10,210 ‫So, we have two elements in our DynamoDB table, 145 00:06:10,210 --> 00:06:11,810 ‫which is pretty cool. 146 00:06:11,810 --> 00:06:13,200 ‫And, now what we want to do 147 00:06:13,200 --> 00:06:16,110 ‫is make sure that our function works properly. 148 00:06:16,110 --> 00:06:17,517 ‫So, let's go to our lambda function 149 00:06:17,517 --> 00:06:19,870 ‫and we'll refresh this page. 150 00:06:19,870 --> 00:06:21,310 ‫And, now if we scroll down 151 00:06:21,310 --> 00:06:23,680 ‫and look at the environment variables, 152 00:06:23,680 --> 00:06:25,350 ‫we can see that the region name was 153 00:06:25,350 --> 00:06:27,870 ‫automatically set to eu-west-3 154 00:06:27,870 --> 00:06:29,840 ‫and our table name was automatically 155 00:06:29,840 --> 00:06:32,290 ‫set to the table name that was created by DynamoDB. 156 00:06:32,290 --> 00:06:34,880 ‫So, our environment variables are correctly set. 157 00:06:34,880 --> 00:06:37,750 ‫And so, our code should be able to reference them. 158 00:06:37,750 --> 00:06:39,430 ‫And, it turns out that our code 159 00:06:40,440 --> 00:06:42,860 ‫has not been updated for some reason. 160 00:06:42,860 --> 00:06:43,693 ‫Let's have a look. 161 00:06:43,693 --> 00:06:45,180 ‫Nope, the code wasn't updated 162 00:06:45,180 --> 00:06:48,560 ‫because I probably forgot to save the code. 163 00:06:48,560 --> 00:06:50,833 ‫So, let's run this again. 164 00:06:53,310 --> 00:06:56,120 ‫Okay, so my code is now updated, which is great. 165 00:06:56,120 --> 00:07:01,000 ‫By the way, if you scroll down and look at the IAM role, 166 00:07:01,000 --> 00:07:04,240 ‫it has created a hello world sam, hello world IAM role, 167 00:07:04,240 --> 00:07:07,980 ‫so we could go to IAM to see what was done in there. 168 00:07:07,980 --> 00:07:08,930 ‫So, lets go to IAM, 169 00:07:11,090 --> 00:07:12,560 ‫click on that role, 170 00:07:12,560 --> 00:07:16,060 ‫and here is the role that was created by hello world sam 171 00:07:16,060 --> 00:07:19,130 ‫and it turns out it has a lambda basic execution role, 172 00:07:19,130 --> 00:07:22,290 ‫which is great, and also an inline policy 173 00:07:22,290 --> 00:07:25,080 ‫allowing it to do DynamoDB get item, 174 00:07:25,080 --> 00:07:27,590 ‫delete item, put item, scan, etc. 175 00:07:27,590 --> 00:07:30,800 ‫All on my hello world sam table. 176 00:07:30,800 --> 00:07:31,830 ‫So, this is pretty awesome. 177 00:07:31,830 --> 00:07:34,220 ‫So, sam framework just generated for us 178 00:07:34,220 --> 00:07:35,950 ‫the DynamoDB, the API gateway, 179 00:07:35,950 --> 00:07:37,430 ‫the lambda function, the IAM roles. 180 00:07:37,430 --> 00:07:38,970 ‫Everything is perfect, 181 00:07:38,970 --> 00:07:43,240 ‫so now hopefully if I go to my resources, 182 00:07:43,240 --> 00:07:45,960 ‫to the GET route and test it out, 183 00:07:45,960 --> 00:07:49,300 ‫I'll click on test and click on test again, 184 00:07:49,300 --> 00:07:51,980 ‫I get a 200, so it works, 185 00:07:51,980 --> 00:07:54,290 ‫and now it returns a response value, 186 00:07:54,290 --> 00:07:56,330 ‫greeting hello, greeting bonjour. 187 00:07:56,330 --> 00:07:59,070 ‫So, everything I add into my DynamoDB 188 00:07:59,070 --> 00:08:01,800 ‫will just be returned by the API gateway. 189 00:08:01,800 --> 00:08:05,350 ‫And so, overall that seems like a lot of things we did, 190 00:08:05,350 --> 00:08:07,890 ‫but we just put together the API gateway, 191 00:08:07,890 --> 00:08:11,150 ‫the lambda functions, the DynamoDB tables, the IAM policy. 192 00:08:11,150 --> 00:08:14,040 ‫All together, such as now I have an API 193 00:08:14,040 --> 00:08:16,430 ‫that I can call straight from Prod, for example. 194 00:08:16,430 --> 00:08:20,530 ‫And, if I go to this url, prod slash hello, 195 00:08:20,530 --> 00:08:22,320 ‫it will return to me all the greetings 196 00:08:22,320 --> 00:08:25,240 ‫that are in my DynamoDB table and you could play 197 00:08:25,240 --> 00:08:28,400 ‫and add more greetings here or remove them if you want. 198 00:08:28,400 --> 00:08:31,180 ‫So, that's it for this lecture on DynamoDB table. 199 00:08:31,180 --> 00:08:33,893 ‫I hope you liked it, and I will see you in the next one.