1 00:00:00,310 --> 00:00:02,700 ‫Now let's practice the destinations feature. 2 00:00:02,700 --> 00:00:05,920 ‫And for this, we're going to go into the lambda S3 function 3 00:00:05,920 --> 00:00:07,990 ‫and we're going to add destinations directly 4 00:00:07,990 --> 00:00:09,830 ‫into my lambda S3 function. 5 00:00:09,830 --> 00:00:12,460 ‫We're going to add a destination for successes 6 00:00:12,460 --> 00:00:15,320 ‫and a destination for failures. 7 00:00:15,320 --> 00:00:18,940 ‫So to do so, first we need to go into SQS 8 00:00:18,940 --> 00:00:20,840 ‫and we're going to create two queues 9 00:00:20,840 --> 00:00:22,610 ‫that will be these destinations. 10 00:00:22,610 --> 00:00:24,010 ‫So lets create a queue. 11 00:00:24,010 --> 00:00:27,300 ‫I'll call this one S3-success, 12 00:00:27,300 --> 00:00:28,730 ‫which is going to be a destination 13 00:00:28,730 --> 00:00:33,083 ‫for the successes of processing from our S3 function. 14 00:00:34,210 --> 00:00:36,600 ‫And just click on create queue. 15 00:00:36,600 --> 00:00:40,143 ‫And I'll create second queue called S3-failure. 16 00:00:42,820 --> 00:00:44,180 ‫And create this queue. 17 00:00:44,180 --> 00:00:45,560 ‫Okay, great. 18 00:00:45,560 --> 00:00:47,530 ‫So we have success and failure queues. 19 00:00:47,530 --> 00:00:49,350 ‫Now let's go into our lambda function 20 00:00:49,350 --> 00:00:52,370 ‫and go over the configuration. 21 00:00:52,370 --> 00:00:54,460 ‫So let's add a destination. 22 00:00:54,460 --> 00:00:55,920 ‫And so the source type 23 00:00:55,920 --> 00:00:57,760 ‫is going to be for asynchronous invocation, 24 00:00:57,760 --> 00:00:59,440 ‫but you can also have a destination 25 00:00:59,440 --> 00:01:01,280 ‫for a stream type of invocation. 26 00:01:01,280 --> 00:01:02,113 ‫For example, 27 00:01:02,113 --> 00:01:04,860 ‫if it's Kinesis or DynamoDB stream mapped to this function. 28 00:01:04,860 --> 00:01:07,560 ‫But let's go over asynchronous invocation, 29 00:01:07,560 --> 00:01:10,000 ‫the condition is going to be on failure. 30 00:01:10,000 --> 00:01:12,400 ‫The destination type is going to be an SQS queue, 31 00:01:12,400 --> 00:01:15,750 ‫and the destination is going to be S3-failure. 32 00:01:15,750 --> 00:01:18,000 ‫Now it says that the function execution role does not 33 00:01:18,000 --> 00:01:20,820 ‫have the permission to send the results to the destination. 34 00:01:20,820 --> 00:01:22,290 ‫And yes that's expected, 35 00:01:22,290 --> 00:01:25,260 ‫but this lender console is nice and clicking saved, 36 00:01:25,260 --> 00:01:27,660 ‫the permissions will be added automatically. 37 00:01:27,660 --> 00:01:28,960 ‫So let's click on save, 38 00:01:28,960 --> 00:01:31,450 ‫and the permissions should be added as well. 39 00:01:31,450 --> 00:01:32,710 ‫And we can make sure of that 40 00:01:32,710 --> 00:01:36,280 ‫by going into the IAM role of our lambda function. 41 00:01:36,280 --> 00:01:38,993 ‫So to do so let's open our function in a new tab, 42 00:01:40,090 --> 00:01:41,720 ‫go to configuration, 43 00:01:41,720 --> 00:01:43,070 ‫permissions, 44 00:01:43,070 --> 00:01:44,083 ‫find the role name. 45 00:01:46,110 --> 00:01:48,440 ‫And as we can see here we do have indeed, 46 00:01:48,440 --> 00:01:50,524 ‫um, a destination, 47 00:01:50,524 --> 00:01:54,780 ‫Amazon lambda, SQS queue, destination execution role. 48 00:01:54,780 --> 00:01:57,720 ‫which allows us to do a write to an SQS queue. 49 00:01:57,720 --> 00:02:00,380 ‫If the queue name is S3-failure. 50 00:02:00,380 --> 00:02:02,310 ‫So this was added correctly. 51 00:02:02,310 --> 00:02:04,850 ‫And I can add a second destination 52 00:02:04,850 --> 00:02:07,180 ‫for asynchronous invocations on success. 53 00:02:07,180 --> 00:02:11,470 ‫This time for my SQS queue and the destination being again, 54 00:02:11,470 --> 00:02:14,050 ‫my S3-success queue. 55 00:02:14,050 --> 00:02:15,190 ‫And same as before, 56 00:02:15,190 --> 00:02:17,820 ‫the permissions are going to be added for it. 57 00:02:17,820 --> 00:02:19,650 ‫So we're good to go. 58 00:02:19,650 --> 00:02:20,790 ‫Okay, great. 59 00:02:20,790 --> 00:02:22,750 ‫So now we need to test the fact 60 00:02:22,750 --> 00:02:25,940 ‫that these two destinations are working properly. 61 00:02:25,940 --> 00:02:28,600 ‫And so to do so we will test one success event, 62 00:02:28,600 --> 00:02:31,870 ‫and one failure type of event. 63 00:02:31,870 --> 00:02:34,400 ‫So first let's test the success. 64 00:02:34,400 --> 00:02:36,460 ‫So we're going to go into, and by the way, 65 00:02:36,460 --> 00:02:37,650 ‫you can see the two destinations 66 00:02:37,650 --> 00:02:40,290 ‫here are configured under the destinations tab, 67 00:02:40,290 --> 00:02:44,270 ‫but also you can see them right here, in the lambda console. 68 00:02:44,270 --> 00:02:45,103 ‫Okay. 69 00:02:45,103 --> 00:02:46,720 ‫So let's test the success use case. 70 00:02:46,720 --> 00:02:48,933 ‫So let's go into Amazon S3, 71 00:02:49,890 --> 00:02:52,370 ‫Our buckets, so let's open our buckets. 72 00:02:52,370 --> 00:02:53,500 ‫Let's see if I can click on this. 73 00:02:53,500 --> 00:02:54,620 ‫Yes, we can. 74 00:02:54,620 --> 00:02:56,690 ‫To go into our S3 buckets. 75 00:02:56,690 --> 00:02:59,150 ‫And I'm going to just upload a new file. 76 00:02:59,150 --> 00:03:00,690 ‫I'll add a file. 77 00:03:00,690 --> 00:03:03,353 ‫And this one's going to be beach.JPEG, 78 00:03:04,260 --> 00:03:05,203 ‫and upload it. 79 00:03:06,470 --> 00:03:08,220 ‫Now the upload has succeeded. 80 00:03:08,220 --> 00:03:10,370 ‫So the lambda function should be run 81 00:03:10,370 --> 00:03:13,890 ‫and it should return just this which is a success. 82 00:03:13,890 --> 00:03:14,723 ‫It's not a failure. 83 00:03:14,723 --> 00:03:15,920 ‫So it's a success. 84 00:03:15,920 --> 00:03:18,790 ‫And so therefore the log should be in CloudWatch logs, 85 00:03:18,790 --> 00:03:21,460 ‫and then the destination should send it 86 00:03:21,460 --> 00:03:25,160 ‫into my SQS queue named S3-success. 87 00:03:25,160 --> 00:03:29,350 ‫So if I go into SQS and refresh this page, 88 00:03:29,350 --> 00:03:32,340 ‫I see my S3-success queue is right here, 89 00:03:32,340 --> 00:03:35,020 ‫and we see that one message is available in it, 90 00:03:35,020 --> 00:03:37,430 ‫which means that yes indeed the message was sent 91 00:03:37,430 --> 00:03:40,360 ‫into my SQS queue named S3-success. 92 00:03:40,360 --> 00:03:43,370 ‫So we go to send and receive messages, 93 00:03:43,370 --> 00:03:44,560 ‫and we can receive messages. 94 00:03:44,560 --> 00:03:46,730 ‫One is available, let's poll for messages. 95 00:03:46,730 --> 00:03:48,140 ‫Here is the message. 96 00:03:48,140 --> 00:03:49,600 ‫And in terms of the body of the message, 97 00:03:49,600 --> 00:03:52,900 ‫we get a lot of information around the request context. 98 00:03:52,900 --> 00:03:56,870 ‫Which is that there was a success and it was invoked once. 99 00:03:56,870 --> 00:03:58,520 ‫As well as the record itself. 100 00:03:58,520 --> 00:04:01,080 ‫So it was an S3 event source. 101 00:04:01,080 --> 00:04:03,530 ‫Object Created:put and so on. 102 00:04:03,530 --> 00:04:06,830 ‫We get a lot of information around the event source itself. 103 00:04:06,830 --> 00:04:10,300 ‫And we also get information around the response payload, 104 00:04:10,300 --> 00:04:11,930 ‫which contains the status code and the body. 105 00:04:11,930 --> 00:04:14,520 ‫So we get both the event source, 106 00:04:14,520 --> 00:04:17,890 ‫as well as the event response and some extra information 107 00:04:17,890 --> 00:04:20,310 ‫into the body of my SQS queue message, 108 00:04:20,310 --> 00:04:21,160 ‫which is amazing. 109 00:04:21,160 --> 00:04:25,200 ‫So destinations have a lot of information packed into them. 110 00:04:25,200 --> 00:04:27,140 ‫And so if you want to test the failure case 111 00:04:27,140 --> 00:04:28,880 ‫then we need to go into the code. 112 00:04:28,880 --> 00:04:31,570 ‫And instead of this return, we need to raise an exception. 113 00:04:31,570 --> 00:04:34,440 ‫So raise exception, 114 00:04:34,440 --> 00:04:38,950 ‫and we'll just have "boom!" as the exception message, 115 00:04:38,950 --> 00:04:40,230 ‫let's deploy this. 116 00:04:40,230 --> 00:04:42,930 ‫So now my function should be raising exceptions. 117 00:04:42,930 --> 00:04:46,850 ‫So if we go yet again into our S3 console, 118 00:04:46,850 --> 00:04:51,010 ‫and we're going to go back into here and upload a new file. 119 00:04:51,010 --> 00:04:55,580 ‫So this time we'll upload a maybe an index.HTML file, 120 00:04:55,580 --> 00:04:56,463 ‫and upload it. 121 00:04:57,320 --> 00:04:59,340 ‫So the upload has succeeded. 122 00:04:59,340 --> 00:05:03,120 ‫And my lambda function is going to be invoked asynchronously 123 00:05:03,120 --> 00:05:05,020 ‫and is going to raise an exception. 124 00:05:05,020 --> 00:05:07,220 ‫So if you remember if I go into a SQS right now 125 00:05:07,220 --> 00:05:09,310 ‫I should not see any messages right away. 126 00:05:09,310 --> 00:05:11,993 ‫And let's see let's refresh this. 127 00:05:13,390 --> 00:05:16,420 ‫So yes, my S3-failure does not have any messages yet. 128 00:05:16,420 --> 00:05:18,010 ‫And do you know why? 129 00:05:18,010 --> 00:05:19,300 ‫You should know by now. 130 00:05:19,300 --> 00:05:22,330 ‫But because S3 invoking my lambda function 131 00:05:22,330 --> 00:05:25,200 ‫is an asynchronous type of invocation. 132 00:05:25,200 --> 00:05:27,470 ‫Then if we go into the configuration remember, 133 00:05:27,470 --> 00:05:29,820 ‫and go to the asynchronous invocation, 134 00:05:29,820 --> 00:05:31,740 ‫well we have two retry attempts 135 00:05:31,740 --> 00:05:32,960 ‫that are going to be running. 136 00:05:32,960 --> 00:05:35,180 ‫So these retry attempts are going to be run. 137 00:05:35,180 --> 00:05:37,730 ‫And then once the retry attempts are run, 138 00:05:37,730 --> 00:05:40,680 ‫then the destination will be invoked as a failure. 139 00:05:40,680 --> 00:05:44,870 ‫And the message should end up into my SQS queue. 140 00:05:44,870 --> 00:05:47,800 ‫So let's just wait a little bit I'm going to pause the video 141 00:05:47,800 --> 00:05:49,900 ‫and get back to you in two, three minutes. 142 00:05:50,760 --> 00:05:52,330 ‫Okay so I just refreshed and indeed 143 00:05:52,330 --> 00:05:55,630 ‫we see one message in my S3-failure SQS queue. 144 00:05:55,630 --> 00:05:58,090 ‫So let's go in send and receive messages, 145 00:05:58,090 --> 00:05:59,880 ‫scroll down, poll for message. 146 00:05:59,880 --> 00:06:01,520 ‫Here is my message. 147 00:06:01,520 --> 00:06:03,660 ‫And if you have a look at the body itself, 148 00:06:03,660 --> 00:06:06,620 ‫well it has decided that the condition for this message 149 00:06:06,620 --> 00:06:09,870 ‫to be here is that the retries have been exhausted. 150 00:06:09,870 --> 00:06:12,280 ‫Then the approximate invoke count was three. 151 00:06:12,280 --> 00:06:15,550 ‫And so then it was sent to my destination failure. 152 00:06:15,550 --> 00:06:18,070 ‫We can have a look at the record itself 153 00:06:18,070 --> 00:06:20,060 ‫that made our lambda function fail. 154 00:06:20,060 --> 00:06:22,440 ‫So we could debug what happened in here, 155 00:06:22,440 --> 00:06:23,680 ‫in our lambda function to make sure 156 00:06:23,680 --> 00:06:25,310 ‫that it doesn't happen next time. 157 00:06:25,310 --> 00:06:28,890 ‫And we can handle this error case maybe more gracefully. 158 00:06:28,890 --> 00:06:30,280 ‫And then we can also get information 159 00:06:30,280 --> 00:06:31,770 ‫about the response payload. 160 00:06:31,770 --> 00:06:33,810 ‫Which says the error message was "boom!". 161 00:06:33,810 --> 00:06:35,030 ‫The type was an exception, 162 00:06:35,030 --> 00:06:36,750 ‫and the stack trace was right here, 163 00:06:36,750 --> 00:06:39,130 ‫which also helps us debug our function. 164 00:06:39,130 --> 00:06:39,963 ‫So this is great. 165 00:06:39,963 --> 00:06:42,700 ‫We've seen how destinations work and they're amazing, 166 00:06:42,700 --> 00:06:45,020 ‫because we can have both the successes and the failures 167 00:06:45,020 --> 00:06:46,600 ‫in two different destinations. 168 00:06:46,600 --> 00:06:47,530 ‫I hope you liked this, 169 00:06:47,530 --> 00:06:49,480 ‫and I will see you in the next lecture.