1 00:00:00,120 --> 00:00:00,953 ‫So now that 2 00:00:00,953 --> 00:00:02,410 ‫we've seen synchronous invocations, 3 00:00:02,410 --> 00:00:04,980 ‫let's go into asynchronous invocation. 4 00:00:04,980 --> 00:00:06,340 ‫So they're for service 5 00:00:06,340 --> 00:00:08,610 ‫that will invoke another functions behind the scenes, 6 00:00:08,610 --> 00:00:13,130 ‫so Amazon S3, SNS topics, CloudWatch Events, and so on. 7 00:00:13,130 --> 00:00:15,040 ‫So let's go through a concrete example. 8 00:00:15,040 --> 00:00:16,360 ‫Say we have an S3 bucket 9 00:00:16,360 --> 00:00:19,710 ‫and an S3 event notification for new files. 10 00:00:19,710 --> 00:00:22,100 ‫This will go into the Lambda Service, 11 00:00:22,100 --> 00:00:24,780 ‫and because it's asynchronous something will happen. 12 00:00:24,780 --> 00:00:26,340 ‫The events are going to be placed 13 00:00:26,340 --> 00:00:28,860 ‫in an internal Event Queue. 14 00:00:28,860 --> 00:00:30,500 ‫So we have an Event Queue here 15 00:00:30,500 --> 00:00:31,830 ‫and your Lambda function is going to 16 00:00:31,830 --> 00:00:34,250 ‫be reading off that Event Queue. 17 00:00:34,250 --> 00:00:37,810 ‫The Lambda function then will try to process these events, 18 00:00:37,810 --> 00:00:39,980 ‫but if somehow things go wrong, 19 00:00:39,980 --> 00:00:42,950 ‫the Lambda function will automatically attempt to retry. 20 00:00:42,950 --> 00:00:45,800 ‫So that means that it will be three tries in total. 21 00:00:45,800 --> 00:00:47,450 ‫The first one will happen right away, 22 00:00:47,450 --> 00:00:49,920 ‫then the second one will happen a minute after, 23 00:00:49,920 --> 00:00:52,293 ‫and the third one will happen two minutes after 24 00:00:52,293 --> 00:00:53,670 ‫the second one. 25 00:00:53,670 --> 00:00:54,980 ‫So the idea is that our Lambda function 26 00:00:54,980 --> 00:00:57,250 ‫is going to retry three times in total. 27 00:00:57,250 --> 00:01:00,130 ‫And then, once the retries happen, 28 00:01:00,130 --> 00:01:02,580 ‫that means that our Lambda function is possibly 29 00:01:02,580 --> 00:01:05,310 ‫going to process those same events multiple times 30 00:01:05,310 --> 00:01:06,860 ‫and so this could be a problem. 31 00:01:06,860 --> 00:01:09,830 ‫And so if you lambda function is not idempotent 32 00:01:09,830 --> 00:01:12,480 ‫this could be big problem, so meaning your Lambda function 33 00:01:12,480 --> 00:01:14,005 ‫should be idempotent. 34 00:01:14,005 --> 00:01:16,530 ‫Idempotentcy means that, in case of retries, 35 00:01:16,530 --> 00:01:18,260 ‫the result is the same. 36 00:01:18,260 --> 00:01:21,100 ‫So then, if you have a retry happening, 37 00:01:21,100 --> 00:01:22,440 ‫what will happen is that you will see 38 00:01:22,440 --> 00:01:25,060 ‫duplicate log entries in CloudWatch Logs 39 00:01:25,060 --> 00:01:26,230 ‫because your Lambda function 40 00:01:26,230 --> 00:01:29,070 ‫will try over and over and over again. 41 00:01:29,070 --> 00:01:32,410 ‫So we can define a DLQ, or dead-letter queue, 42 00:01:32,410 --> 00:01:34,700 ‫for after the retries are done. 43 00:01:34,700 --> 00:01:37,610 ‫So that means that in case there's a failed processing 44 00:01:37,610 --> 00:01:39,910 ‫and we never were able to succeed 45 00:01:39,910 --> 00:01:42,270 ‫because of the retries, then the Lambda function 46 00:01:42,270 --> 00:01:45,930 ‫can send some events to SQS or SNS 47 00:01:45,930 --> 00:01:48,300 ‫for further processing later one. 48 00:01:48,300 --> 00:01:49,720 ‫And so, this is the whole idea 49 00:01:49,720 --> 00:01:51,560 ‫behind asynchronous invocations. 50 00:01:51,560 --> 00:01:52,393 ‫And so you may ask me, 51 00:01:52,393 --> 00:01:54,600 ‫"Why we will use asynchronous versus synchronous?" 52 00:01:54,600 --> 00:01:57,010 ‫Well, first of all, some services must use asynchronous, 53 00:01:57,010 --> 00:01:58,130 ‫so you have no choice, 54 00:01:58,130 --> 00:01:59,790 ‫and the second one is, for example, 55 00:01:59,790 --> 00:02:01,340 ‫say you need to speed up processing 56 00:02:01,340 --> 00:02:03,220 ‫and you don't need to wait for the results, 57 00:02:03,220 --> 00:02:05,770 ‫then you can start 1000 files being processed 58 00:02:05,770 --> 00:02:08,100 ‫at the same time, and you just wait at the end 59 00:02:08,100 --> 00:02:10,010 ‫for all five to be processed in parallel, 60 00:02:10,010 --> 00:02:12,150 ‫but you don't wait for each individual results, 61 00:02:12,150 --> 00:02:14,690 ‫and so that speeds up your processing time. 62 00:02:14,690 --> 00:02:18,080 ‫So, what services are done asynchronously? 63 00:02:18,080 --> 00:02:19,490 ‫The first one is Amazon S3 64 00:02:19,490 --> 00:02:21,630 ‫when we have S3 Event Notifications 65 00:02:21,630 --> 00:02:23,230 ‫invoking Lambda functions. 66 00:02:23,230 --> 00:02:25,290 ‫We have SNS, so when we receive notifications 67 00:02:25,290 --> 00:02:27,290 ‫and we trigger a Lambda function. 68 00:02:27,290 --> 00:02:30,660 ‫CloudWatch Events or CloudWatch EventBridge, 69 00:02:30,660 --> 00:02:32,830 ‫which will basically have our Lambda functions 70 00:02:32,830 --> 00:02:36,740 ‫react to events happening in our AWS infrastructure. 71 00:02:36,740 --> 00:02:38,820 ‫Other services that we will not necessarily see 72 00:02:38,820 --> 00:02:40,730 ‫in the hands-on would be CodeCommit 73 00:02:40,730 --> 00:02:42,520 ‫to trigger a Lambda function whenever 74 00:02:42,520 --> 00:02:44,990 ‫there's a new branch or new tag or new push, 75 00:02:44,990 --> 00:02:46,030 ‫CodePipeline to invoke a Lambda function during the pipeline 76 00:02:47,802 --> 00:02:50,340 ‫and the Lambda must callback CodePipeline, 77 00:02:50,340 --> 00:02:53,560 ‫or some other services, CloudWatch Logs for log processing, 78 00:02:53,560 --> 00:02:56,370 ‫SES, so Simple Email Service for sending out emails, 79 00:02:56,370 --> 00:02:59,750 ‫CloudFormation, Config, Io T, and IoT Events. 80 00:02:59,750 --> 00:03:02,750 ‫So from our standpoint for this certification 81 00:03:02,750 --> 00:03:05,080 ‫we need to know how Lambda works with Amazon S3, 82 00:03:05,080 --> 00:03:07,970 ‫SNS, and CloudWatch Events or EventBridge. 83 00:03:07,970 --> 00:03:09,290 ‫So let's go ahead and learn 84 00:03:09,290 --> 00:03:11,720 ‫about asynchronous invocation in the hands-on. 85 00:03:11,720 --> 00:03:13,470 ‫I will see you in the next lecture.