1 00:00:00,000 --> 00:00:02,573 ‫So let's get our users post table and 2 00:00:02,573 --> 00:00:05,160 ‫on it we're going to enable, DynamoDB stream. 3 00:00:05,160 --> 00:00:07,533 ‫So to do so let's go to exports and streams, 4 00:00:07,533 --> 00:00:11,171 ‫and here we're going to have DynamoDB stream details, 5 00:00:11,171 --> 00:00:12,567 ‫and we're going to enable it. 6 00:00:12,567 --> 00:00:14,560 ‫Now we have the option to choose 7 00:00:14,560 --> 00:00:16,520 ‫which type of view we want to have 8 00:00:16,520 --> 00:00:18,370 ‫within our DynamoDB stream. 9 00:00:18,370 --> 00:00:19,853 ‫And it can be key attributes, new image, 10 00:00:19,853 --> 00:00:22,051 ‫all image or new and old images. 11 00:00:22,051 --> 00:00:24,760 ‫So I'm going to keep the last option 12 00:00:24,760 --> 00:00:26,519 ‫to get as much information as possible, 13 00:00:26,519 --> 00:00:28,291 ‫and I will enable this stream. 14 00:00:28,291 --> 00:00:29,320 ‫Okay, great. 15 00:00:29,320 --> 00:00:31,213 ‫So now my stream is enabled and within it, 16 00:00:31,213 --> 00:00:32,888 ‫as you can see, if I scroll down, 17 00:00:32,888 --> 00:00:34,040 ‫there is the trigger. 18 00:00:34,040 --> 00:00:36,568 ‫And the trigger, is what is this stream 19 00:00:36,568 --> 00:00:37,500 ‫going to trigger? 20 00:00:37,500 --> 00:00:38,755 ‫So we can create a trigger. 21 00:00:38,755 --> 00:00:42,200 ‫And here we have a Lambda function that can be invoked 22 00:00:42,200 --> 00:00:45,133 ‫every time your DynamoDB stream is updated. 23 00:00:45,133 --> 00:00:46,830 ‫So to do so, well, let's go ahead 24 00:00:46,830 --> 00:00:48,461 ‫and create a new function. 25 00:00:48,461 --> 00:00:49,294 ‫Okay. 26 00:00:49,294 --> 00:00:51,360 ‫And we can use a blueprint. 27 00:00:51,360 --> 00:00:54,250 ‫And in here I can type DynamoDB, 28 00:00:54,250 --> 00:00:57,900 ‫and we have DynamoDB process stream Python. 29 00:00:57,900 --> 00:00:59,670 ‫Which is going to log all the updates 30 00:00:59,670 --> 00:01:00,503 ‫made to a table. 31 00:01:00,503 --> 00:01:01,336 ‫So this is good. 32 00:01:01,336 --> 00:01:02,810 ‫Let's configure it 33 00:01:02,810 --> 00:01:06,878 ‫and I'll call it Lambda demo, DynamoDB stream. 34 00:01:06,878 --> 00:01:09,770 ‫Next, we're going to create a new role 35 00:01:09,770 --> 00:01:11,110 ‫with basic Lambda permissions, 36 00:01:11,110 --> 00:01:12,610 ‫and we'll make sure to edit this role, 37 00:01:12,610 --> 00:01:14,912 ‫to add the permissions, to read from DynamoDB. 38 00:01:14,912 --> 00:01:16,900 ‫Now for the DynamoDB triggers, 39 00:01:16,900 --> 00:01:18,700 ‫we need to create the trigger. 40 00:01:18,700 --> 00:01:21,111 ‫So is going to be the users post, 41 00:01:21,111 --> 00:01:22,145 ‫which table was it? 42 00:01:22,145 --> 00:01:23,590 ‫The users post table? 43 00:01:23,590 --> 00:01:24,880 ‫Yes, indeed. 44 00:01:24,880 --> 00:01:26,950 ‫And the batch size is 100. 45 00:01:26,950 --> 00:01:27,783 ‫Okay. 46 00:01:27,783 --> 00:01:29,040 ‫So this is how many records will read at a time. 47 00:01:29,040 --> 00:01:30,260 ‫The batch window, if you wanted to 48 00:01:30,260 --> 00:01:32,280 ‫gather records before invoking the functions, 49 00:01:32,280 --> 00:01:33,534 ‫so to be more efficient. 50 00:01:33,534 --> 00:01:35,040 ‫And the starting position. 51 00:01:35,040 --> 00:01:36,410 ‫So do you want to read from the start or 52 00:01:36,410 --> 00:01:38,001 ‫from the end of the stream, just in case 53 00:01:38,001 --> 00:01:40,640 ‫the stream had been already created. 54 00:01:40,640 --> 00:01:41,640 ‫So we're good. 55 00:01:41,640 --> 00:01:42,473 ‫We enable the trigger. 56 00:01:42,473 --> 00:01:43,306 ‫And as you can see, what it does is 57 00:01:43,306 --> 00:01:45,100 ‫that it just prints the records 58 00:01:45,100 --> 00:01:46,247 ‫that we get out of the stream, 59 00:01:46,247 --> 00:01:48,090 ‫into the logs we'll be able to 60 00:01:48,090 --> 00:01:50,143 ‫view this in CloudWatch logs. 61 00:01:52,596 --> 00:01:54,540 ‫So now we have created the function, 62 00:01:54,540 --> 00:01:55,730 ‫but we get an error, which is that 63 00:01:55,730 --> 00:01:57,705 ‫the record or the function can not access 64 00:01:57,705 --> 00:02:01,053 ‫our stream because we have missing IAM information. 65 00:02:01,053 --> 00:02:02,430 ‫So we're going to fix this. 66 00:02:02,430 --> 00:02:03,795 ‫So we click on our function 67 00:02:03,795 --> 00:02:05,630 ‫and then under configuration, 68 00:02:05,630 --> 00:02:06,909 ‫we're going to go under permissions, 69 00:02:06,909 --> 00:02:08,905 ‫and this is the execution role. 70 00:02:08,905 --> 00:02:11,700 ‫Now we have to click on this execution role 71 00:02:11,700 --> 00:02:13,980 ‫and we're going to add the necessary permissions 72 00:02:13,980 --> 00:02:15,113 ‫to read from DynamoDB. 73 00:02:15,113 --> 00:02:16,978 ‫So we'll attach a policy 74 00:02:16,978 --> 00:02:19,305 ‫and I will look for DynamoDB, 75 00:02:19,305 --> 00:02:22,387 ‫and we'll have DynamoDB read only access 76 00:02:22,387 --> 00:02:24,090 ‫because what we're doing is that we're 77 00:02:24,090 --> 00:02:26,189 ‫actually reading from DynamoDB. 78 00:02:26,189 --> 00:02:28,310 ‫So this is good. 79 00:02:28,310 --> 00:02:29,830 ‫And this one 80 00:02:31,870 --> 00:02:34,070 ‫is also good to read from DynamoDB. 81 00:02:34,070 --> 00:02:35,324 ‫So we'll attach these two just in case, 82 00:02:35,324 --> 00:02:37,370 ‫but this has meant for Lambda. 83 00:02:37,370 --> 00:02:38,332 ‫So probably a little bit better. 84 00:02:38,332 --> 00:02:39,847 ‫So let's attach this policy 85 00:02:39,847 --> 00:02:42,630 ‫and to be honest, one of these is going to work. 86 00:02:42,630 --> 00:02:44,070 ‫I'm trying to make this as simple as possible, 87 00:02:44,070 --> 00:02:45,778 ‫which is why I'm not spending a lot of time on this. 88 00:02:45,778 --> 00:02:47,751 ‫So let's refresh now this page, 89 00:02:47,751 --> 00:02:50,570 ‫and it seems like we're good to go. 90 00:02:50,570 --> 00:02:53,174 ‫So we have more actions on DynamoDB. 91 00:02:53,174 --> 00:02:54,300 ‫So this is good. 92 00:02:54,300 --> 00:02:55,460 ‫Okay. 93 00:02:55,460 --> 00:02:57,322 ‫And now if we refresh this, 94 00:02:57,322 --> 00:02:59,071 ‫we can choose this function 95 00:02:59,071 --> 00:03:01,117 ‫and set the batch size in 100 96 00:03:01,117 --> 00:03:02,517 ‫and we create this trigger. 97 00:03:02,517 --> 00:03:04,959 ‫So now that means that my DynamoDB stream 98 00:03:04,959 --> 00:03:07,625 ‫is going to trigger my Lambda function. 99 00:03:07,625 --> 00:03:10,047 ‫And if I go back into my Lambda function 100 00:03:10,047 --> 00:03:11,643 ‫and refresh this page, 101 00:03:12,590 --> 00:03:14,260 ‫we can see the DynamoDB 102 00:03:14,260 --> 00:03:16,590 ‫is indeed triggering my Lambda function. 103 00:03:16,590 --> 00:03:18,293 ‫So this is an enabled type of integration 104 00:03:18,293 --> 00:03:20,063 ‫with these settings. 105 00:03:20,063 --> 00:03:21,500 ‫Okay, good. 106 00:03:21,500 --> 00:03:23,390 ‫So next, what I'm going to do is just test it out. 107 00:03:23,390 --> 00:03:25,717 ‫So we're going to go to our table 108 00:03:25,717 --> 00:03:28,298 ‫and what I'm going to do is click on view items. 109 00:03:28,298 --> 00:03:30,165 ‫And we're going to do a few things. 110 00:03:30,165 --> 00:03:32,309 ‫For example, let's take this item, 111 00:03:32,309 --> 00:03:34,826 ‫the second post of John, we're going to action, 112 00:03:34,826 --> 00:03:37,279 ‫edit it, and then I'm going to modify. 113 00:03:37,279 --> 00:03:39,933 ‫So I'm going to say a second post 'yay' edit. 114 00:03:40,990 --> 00:03:42,730 ‫And click on save change. 115 00:03:42,730 --> 00:03:43,870 ‫So this is good. 116 00:03:43,870 --> 00:03:45,732 ‫And then we'll take Alice 117 00:03:45,732 --> 00:03:47,670 ‫and we're going to do action 118 00:03:47,670 --> 00:03:48,800 ‫and then duplicate. 119 00:03:48,800 --> 00:03:50,280 ‫So it's going to create a new thing 120 00:03:50,280 --> 00:03:53,360 ‫and I'm going to just add a bit more data 121 00:03:53,360 --> 00:03:56,149 ‫as a new Alice blog 122 00:03:56,149 --> 00:03:57,983 ‫and then create item. 123 00:03:59,890 --> 00:04:01,520 ‫And then finally, we actually don't like this blog, 124 00:04:01,520 --> 00:04:03,480 ‫so I'm going to delete it. 125 00:04:03,480 --> 00:04:07,460 ‫So I'm going to do action and then delete items. 126 00:04:07,460 --> 00:04:08,830 ‫And yes. 127 00:04:08,830 --> 00:04:09,663 ‫Okay. 128 00:04:09,663 --> 00:04:10,840 ‫So we've done three kinds of operations. 129 00:04:10,840 --> 00:04:12,974 ‫We've had an update, a create and a delete. 130 00:04:12,974 --> 00:04:14,887 ‫And so we went to do my Lambda function. 131 00:04:14,887 --> 00:04:17,550 ‫My Lambda function executed this code, 132 00:04:17,550 --> 00:04:19,756 ‫which was printing, so logging these events. 133 00:04:19,756 --> 00:04:22,180 ‫And so what I need you to do is just go into 134 00:04:22,180 --> 00:04:25,540 ‫CloudWatch logs and have a look at whether 135 00:04:25,540 --> 00:04:27,278 ‫or not we saw these informational logs 136 00:04:27,278 --> 00:04:30,173 ‫So let's click on view the logs in CloudWatch logs. 137 00:04:31,418 --> 00:04:35,027 ‫And here we have some information around this log stream. 138 00:04:35,027 --> 00:04:36,970 ‫And as you can see in this log stream 139 00:04:36,970 --> 00:04:38,169 ‫you get a lot of information. 140 00:04:38,169 --> 00:04:39,710 ‫Okay, so we get one line. 141 00:04:39,710 --> 00:04:41,005 ‫So this was a modify 142 00:04:41,005 --> 00:04:43,160 ‫and this was the DynamoDB records. 143 00:04:43,160 --> 00:04:45,400 ‫So it gives you the key, the user ID, 144 00:04:45,400 --> 00:04:46,786 ‫and as well as the new image. 145 00:04:46,786 --> 00:04:47,750 ‫Okay. 146 00:04:47,750 --> 00:04:48,583 ‫And the content, 147 00:04:48,583 --> 00:04:51,178 ‫so we can see yes, a second post 'yay' edit. 148 00:04:51,178 --> 00:04:53,120 ‫And we can also see the old image 149 00:04:53,120 --> 00:04:54,620 ‫of what it was before. 150 00:04:54,620 --> 00:04:56,311 ‫So that was like for one request, 151 00:04:56,311 --> 00:04:57,975 ‫then we have an insert. 152 00:04:57,975 --> 00:05:00,386 ‫And so again, we can see, the DynamoDB record in it. 153 00:05:00,386 --> 00:05:03,450 ‫And then the new image and the, there is 154 00:05:03,450 --> 00:05:05,390 ‫no old image because obviously we wasn't inserts. 155 00:05:05,390 --> 00:05:06,434 ‫So there was nothing before. 156 00:05:06,434 --> 00:05:07,723 ‫And then we get a remove. 157 00:05:07,723 --> 00:05:10,274 ‫And again, this remove operation was logged. 158 00:05:10,274 --> 00:05:12,210 ‫We have the old image, and obviously there 159 00:05:12,210 --> 00:05:14,270 ‫is no new image because the thing was removed. 160 00:05:14,270 --> 00:05:15,103 ‫Okay. 161 00:05:15,103 --> 00:05:16,119 ‫So that's fairly easy. 162 00:05:16,119 --> 00:05:17,516 ‫We just enable the stream 163 00:05:17,516 --> 00:05:19,130 ‫and you went to Lambda function 164 00:05:19,130 --> 00:05:20,380 ‫and the Lambda function was logging it, 165 00:05:20,380 --> 00:05:22,882 ‫but this is the basis to have these 166 00:05:22,882 --> 00:05:25,260 ‫kinds of integrations with DynamoDB streams. 167 00:05:25,260 --> 00:05:26,093 ‫Okay. 168 00:05:26,093 --> 00:05:28,090 ‫Lastly, please make sure to disable the trigger. 169 00:05:28,090 --> 00:05:30,340 ‫So on the DynamoDB, you take this trigger 170 00:05:30,340 --> 00:05:32,205 ‫and you disabled it or delete it, 171 00:05:32,205 --> 00:05:34,285 ‫whatever you want, and you'll be good to go. 172 00:05:34,285 --> 00:05:36,260 ‫So that's it for this lecture. 173 00:05:36,260 --> 00:05:37,093 ‫I hope you liked it. 174 00:05:37,093 --> 00:05:39,010 ‫And I will see you in the next lecture.