1 00:00:00,120 --> 00:00:04,256 ‫So what is AWS Lambda and why is it helpful for us? 2 00:00:04,256 --> 00:00:06,080 ‫So let's take an example. 3 00:00:06,080 --> 00:00:08,340 ‫We are going to start with Amazon EC2. 4 00:00:08,340 --> 00:00:09,580 ‫And Amazon EC2, as we know, 5 00:00:09,580 --> 00:00:11,590 ‫they are virtual servers in the cloud 6 00:00:11,590 --> 00:00:12,810 ‫and we have to provision them, 7 00:00:12,810 --> 00:00:14,500 ‫so we are limited by the amount 8 00:00:14,500 --> 00:00:17,270 ‫of memory and CPU we're going to provision. 9 00:00:17,270 --> 00:00:18,620 ‫They have to be continuously running, 10 00:00:18,620 --> 00:00:20,005 ‫I mean, we can optimize them 11 00:00:20,005 --> 00:00:22,340 ‫by starting them and stopping them efficiently, 12 00:00:22,340 --> 00:00:23,770 ‫but otherwise they run continuously, 13 00:00:23,770 --> 00:00:25,970 ‫regardless of if something's happening 14 00:00:25,970 --> 00:00:27,690 ‫or not on your instance. 15 00:00:27,690 --> 00:00:30,220 ‫And if you want to scale, you can use auto scaling groups, 16 00:00:30,220 --> 00:00:32,860 ‫but that means that you need to do something to 17 00:00:32,860 --> 00:00:35,840 ‫automatically add and remove servers. 18 00:00:35,840 --> 00:00:37,410 ‫Now, this is one way of doing things 19 00:00:37,410 --> 00:00:38,840 ‫and it has worked great. 20 00:00:38,840 --> 00:00:41,700 ‫But then there is AWS Lambda. 21 00:00:41,700 --> 00:00:43,880 ‫So with Lambda, these are visual functions. 22 00:00:43,880 --> 00:00:45,380 ‫No servers to manage. 23 00:00:45,380 --> 00:00:47,280 ‫So that means that we just provision the code 24 00:00:47,280 --> 00:00:48,860 ‫and the functions run. 25 00:00:48,860 --> 00:00:49,850 ‫It is limited by time, 26 00:00:49,850 --> 00:00:51,450 ‫so we're talking about short executions 27 00:00:51,450 --> 00:00:54,270 ‫of up to 15 minutes, which is in my opinion, 28 00:00:54,270 --> 00:00:55,350 ‫not that short. 29 00:00:55,350 --> 00:00:57,810 ‫And then finally they run on-demand. 30 00:00:57,810 --> 00:00:59,560 ‫So that means that when you don't use Lambda, 31 00:00:59,560 --> 00:01:01,080 ‫your Lambda function is not running, 32 00:01:01,080 --> 00:01:02,670 ‫and you only are going to be billed 33 00:01:02,670 --> 00:01:03,780 ‫when your function is running. 34 00:01:03,780 --> 00:01:06,130 ‫And it will run on demand when it gets invoked, 35 00:01:06,130 --> 00:01:09,160 ‫which is a huge shift from Amazon EC2. 36 00:01:09,160 --> 00:01:11,560 ‫Finally, the scaling is automated. 37 00:01:11,560 --> 00:01:14,900 ‫If you need more than the functions, occurrences, 38 00:01:14,900 --> 00:01:18,710 ‫and the concurrency, then automatically AWS will provision 39 00:01:18,710 --> 00:01:20,870 ‫for you more Lambda functions. Magical. 40 00:01:20,870 --> 00:01:22,310 ‫We'll see this in the hands-on. 41 00:01:22,310 --> 00:01:23,710 ‫So benefits of Lambda. 42 00:01:23,710 --> 00:01:24,840 ‫I'm sure you see many. 43 00:01:24,840 --> 00:01:27,220 ‫First of all the pricing is super easy. 44 00:01:27,220 --> 00:01:28,053 ‫You're going to pay 45 00:01:28,053 --> 00:01:29,950 ‫for the number of requests Lambda receives, 46 00:01:29,950 --> 00:01:31,530 ‫so the number of invocations, 47 00:01:31,530 --> 00:01:32,820 ‫and your computer time, 48 00:01:32,820 --> 00:01:34,830 ‫so how long Lambda was running for. 49 00:01:34,830 --> 00:01:37,810 ‫There is a very generous free tier on Lambda, 50 00:01:37,810 --> 00:01:40,260 ‫which is 1 million Lambda requests 51 00:01:40,260 --> 00:01:44,150 ‫and 400,000 gigabytes seconds of compute time. 52 00:01:44,150 --> 00:01:45,421 ‫It is also integrated 53 00:01:45,421 --> 00:01:48,290 ‫with so many A-list services as we'll see. 54 00:01:48,290 --> 00:01:50,620 ‫And we can use a lot of different programming 55 00:01:50,620 --> 00:01:53,310 ‫languages on Lambda, so we're quite free. 56 00:01:53,310 --> 00:01:55,060 ‫And there's very easy monitoring 57 00:01:55,060 --> 00:01:56,790 ‫integrations with CloudWatch. 58 00:01:56,790 --> 00:01:58,340 ‫Finally, if you wanted to provision 59 00:01:58,340 --> 00:01:59,900 ‫more resources per function, 60 00:01:59,900 --> 00:02:02,296 ‫you can provision up to 10 GB 61 00:02:02,296 --> 00:02:06,220 ‫of RAM per function, which is a lot. 62 00:02:06,220 --> 00:02:08,100 ‫And by the way, you need to know this: 63 00:02:08,100 --> 00:02:10,490 ‫if you increase the RAM of your function, 64 00:02:10,490 --> 00:02:13,230 ‫then it will also improve the quality 65 00:02:13,230 --> 00:02:17,820 ‫and performance of your CPU and network, so. 66 00:02:17,820 --> 00:02:20,930 ‫Now let's talk about the language supports for Lambda. 67 00:02:20,930 --> 00:02:23,130 ‫We have node.js, for JavaScript, 68 00:02:23,130 --> 00:02:27,260 ‫Python, Java which is Java 8 compatible or Java 11, 69 00:02:27,260 --> 00:02:31,500 ‫C# for .NET Core, Golang, C# for Powershell, 70 00:02:31,500 --> 00:02:35,170 ‫Ruby, and you can write pretty much any language for Lambda 71 00:02:35,170 --> 00:02:37,150 ‫thanks to the custom runtime API 72 00:02:37,150 --> 00:02:38,750 ‫which is community supported. 73 00:02:38,750 --> 00:02:40,020 ‫And, for example, if you wanted to run 74 00:02:40,020 --> 00:02:42,430 ‫Rust language functions on Lambda, 75 00:02:42,430 --> 00:02:44,810 ‫it is possible thanks to an open source project. 76 00:02:44,810 --> 00:02:47,180 ‫So Lambda has a lot of language support. 77 00:02:47,180 --> 00:02:51,390 ‫And it also has support for the Lambda container image. 78 00:02:51,390 --> 00:02:53,790 ‫So this Lambda container image is quite special. 79 00:02:53,790 --> 00:02:55,980 ‫The container image itself must implement 80 00:02:55,980 --> 00:02:57,860 ‫the Lambda runtime API, 81 00:02:57,860 --> 00:03:01,000 ‫so it's not any container image that can run on Lambda. 82 00:03:01,000 --> 00:03:02,740 ‫There needs to be some prerequisites 83 00:03:02,740 --> 00:03:05,350 ‫about how that container image is built. 84 00:03:05,350 --> 00:03:08,510 ‫And then ECS and Fargate is going still to be preferred 85 00:03:08,510 --> 00:03:11,530 ‫for running arbitrary Docker images. 86 00:03:11,530 --> 00:03:15,130 ‫So the exam, if they ask you to run a container on Lambda, 87 00:03:15,130 --> 00:03:18,720 ‫unless that container does implement the Lambda runtime API, 88 00:03:18,720 --> 00:03:23,720 ‫you will run to run that container on ECS or Fargate. 89 00:03:25,440 --> 00:03:27,490 ‫Now, I said that Lambda was integrated 90 00:03:27,490 --> 00:03:29,410 ‫with so many A-list services. 91 00:03:29,410 --> 00:03:30,670 ‫So I'll give you a few 92 00:03:30,670 --> 00:03:33,090 ‫and some idea of how, how they're integrated. 93 00:03:33,090 --> 00:03:36,220 ‫And in this course, we will see some of these integrations. 94 00:03:36,220 --> 00:03:40,070 ‫So API Gateway is to create a REST API, 95 00:03:40,070 --> 00:03:42,070 ‫and they will invoke our Lambda functions. 96 00:03:42,070 --> 00:03:43,410 ‫Kinesis will be using Lambda 97 00:03:43,410 --> 00:03:45,710 ‫to do some data transformations on the fly. 98 00:03:45,710 --> 00:03:48,540 ‫DynamoDB will be used to create some triggers, 99 00:03:48,540 --> 00:03:50,880 ‫so whenever something happens in our database 100 00:03:50,880 --> 00:03:52,530 ‫a Lambda function will be triggered. 101 00:03:52,530 --> 00:03:54,350 ‫Amazon S3, we've seen this already. 102 00:03:54,350 --> 00:03:56,210 ‫A Lambda function would be triggered anytime, 103 00:03:56,210 --> 00:03:59,200 ‫for example, a file is created in S3. 104 00:03:59,200 --> 00:04:01,170 ‫CloudFront, this will be Lambda@edge, 105 00:04:01,170 --> 00:04:03,550 ‫and I have a lecture dedicated to this in this section. 106 00:04:03,550 --> 00:04:05,330 ‫CloudWatch Events or EventBridge. 107 00:04:05,330 --> 00:04:09,690 ‫This is whenever things happen in our infrastructure on AWS, 108 00:04:09,690 --> 00:04:11,490 ‫and we want to be able to react to things. 109 00:04:11,490 --> 00:04:13,520 ‫For example, say we have a cut pipeline, 110 00:04:13,520 --> 00:04:15,080 ‫state changes and we want to 111 00:04:15,080 --> 00:04:16,630 ‫do some automations based on it, 112 00:04:16,630 --> 00:04:18,420 ‫we can use a Lambda function. 113 00:04:18,420 --> 00:04:20,290 ‫CloudWatch Logs, to stream these logs, 114 00:04:20,290 --> 00:04:21,350 ‫wherever you want. 115 00:04:21,350 --> 00:04:24,780 ‫SNS to react to notifications and your SNS topics. 116 00:04:24,780 --> 00:04:27,970 ‫SQS to process messages from your SQS queues. 117 00:04:27,970 --> 00:04:31,890 ‫And finally Cognito to react to whenever, 118 00:04:31,890 --> 00:04:34,530 ‫for example, a user login to your database. 119 00:04:34,530 --> 00:04:36,300 ‫So, these are just the main ones. 120 00:04:36,300 --> 00:04:38,710 ‫There are tons of Lambda integrations. 121 00:04:38,710 --> 00:04:41,600 ‫And so I want to show you a very good example here, 122 00:04:41,600 --> 00:04:44,310 ‫which is a serverless thumbnail creation. 123 00:04:44,310 --> 00:04:46,520 ‫So let's say we have an S3 bucket, 124 00:04:46,520 --> 00:04:49,430 ‫and we want to create thumbnails on the fly. 125 00:04:49,430 --> 00:04:50,580 ‫So there will be an event 126 00:04:50,580 --> 00:04:54,020 ‫which is that the new image will be uploaded in Amazon S3. 127 00:04:54,020 --> 00:04:57,850 ‫This will trigger, through an S3 event notification, 128 00:04:57,850 --> 00:04:58,777 ‫a Lambda function. 129 00:04:58,777 --> 00:05:01,450 ‫And that Lambda function will have code 130 00:05:01,450 --> 00:05:03,120 ‫to generate a thumbnail. 131 00:05:03,120 --> 00:05:05,870 ‫That thumbnail maybe pushed and uploaded 132 00:05:05,870 --> 00:05:08,770 ‫into another S3 bucket or the same S3 bucket, 133 00:05:08,770 --> 00:05:11,790 ‫which would be a smaller version of that image. 134 00:05:11,790 --> 00:05:14,110 ‫And also, our Lambda function may want to 135 00:05:14,110 --> 00:05:16,652 ‫insert some data into DynamoDB, 136 00:05:16,652 --> 00:05:18,650 ‫around some metadata for the image, 137 00:05:18,650 --> 00:05:20,950 ‫for example the image, name, size, creation date, 138 00:05:20,950 --> 00:05:22,070 ‫et cetera, et cetera. 139 00:05:22,070 --> 00:05:24,690 ‫And so thanks to Lambda, we've automated 140 00:05:24,690 --> 00:05:26,740 ‫and had a reactive architecture 141 00:05:26,740 --> 00:05:31,740 ‫to the events of new app, new images being created in S3. 142 00:05:32,280 --> 00:05:34,340 ‫Another example that's extremely popular 143 00:05:34,340 --> 00:05:36,350 ‫is a serverless CRON drop. 144 00:05:36,350 --> 00:05:39,785 ‫So CRON is a way on your EC2 instances, for example, 145 00:05:39,785 --> 00:05:42,760 ‫to generate jobs every five minutes, 146 00:05:42,760 --> 00:05:46,410 ‫or every Monday at 10:00 AM, et cetera, et cetera. 147 00:05:46,410 --> 00:05:49,920 ‫But you need to run CRON on a virtual server. 148 00:05:49,920 --> 00:05:51,397 ‫So an ECE two instance and so on. 149 00:05:51,397 --> 00:05:53,540 ‫And so while your instance is not running, 150 00:05:53,540 --> 00:05:55,180 ‫or at least your CRONs are not doing anything, 151 00:05:55,180 --> 00:05:56,903 ‫then your instance time is wasted. 152 00:05:58,209 --> 00:06:00,950 ‫And so, as such, you can create a CloudWatch Event rule 153 00:06:00,950 --> 00:06:02,690 ‫or an EventBridge rule 154 00:06:02,690 --> 00:06:04,802 ‫that will be triggered every 1 hour. 155 00:06:04,802 --> 00:06:07,680 ‫And every 1 hour it will be integrated 156 00:06:07,680 --> 00:06:10,480 ‫with a Lambda function that will perform your task. 157 00:06:10,480 --> 00:06:12,420 ‫So this is a way to create a serverless CRON, 158 00:06:12,420 --> 00:06:15,170 ‫because in this example, CloudWatch Events is serverless 159 00:06:15,170 --> 00:06:17,890 ‫and Lambda functions are serverless too. 160 00:06:17,890 --> 00:06:20,610 ‫Now let's take an example at the Lambda pricing. 161 00:06:20,610 --> 00:06:22,560 ‫So you can find all the information on this website 162 00:06:22,560 --> 00:06:24,360 ‫in case the information here is outdated 163 00:06:24,360 --> 00:06:26,310 ‫but it will give you an example of how that works. 164 00:06:26,310 --> 00:06:27,570 ‫So you pay per calls. 165 00:06:27,570 --> 00:06:29,620 ‫The first 1 million requests are free, 166 00:06:29,620 --> 00:06:31,233 ‫and then you're going to pay 20 cents 167 00:06:31,233 --> 00:06:33,470 ‫per extra 1 million requests. 168 00:06:33,470 --> 00:06:36,010 ‫So that makes a very, very cheap request. 169 00:06:36,010 --> 00:06:37,680 ‫Then you're going to pay per duration 170 00:06:37,680 --> 00:06:39,870 ‫in increments of 1 (audio cuts out) seconds. 171 00:06:39,870 --> 00:06:42,400 ‫So you get the first 400,000 gigabytes-seconds 172 00:06:42,400 --> 00:06:45,270 ‫of compute time per month for free. 173 00:06:45,270 --> 00:06:48,450 ‫And then, what that means, this gigabyte-seconds is 174 00:06:48,450 --> 00:06:52,260 ‫it means you get to 400, 000 seconds of execution 175 00:06:52,260 --> 00:06:54,910 ‫if the function has 1 gigabyte of RAM. 176 00:06:54,910 --> 00:06:57,450 ‫That means you get 8 times as more seconds 177 00:06:57,450 --> 00:06:59,510 ‫if the function has 8 times as less RAM, 178 00:06:59,510 --> 00:07:02,810 ‫so 128 gigabytes, megabytes of RAM. 179 00:07:02,810 --> 00:07:04,040 ‫And, after that, you're going to pay $1 180 00:07:04,040 --> 00:07:06,629 ‫for 600,000 gigabytes-seconds. 181 00:07:06,629 --> 00:07:08,750 ‫So to be honest, you can make the math, 182 00:07:08,750 --> 00:07:10,700 ‫it's usually very, very cheap 183 00:07:10,700 --> 00:07:12,500 ‫to run your code on Lambda. 184 00:07:12,500 --> 00:07:15,550 ‫So it's a very popular option to create your applications. 185 00:07:15,550 --> 00:07:16,383 ‫So that's it. 186 00:07:16,383 --> 00:07:17,216 ‫Now let's go into the hands-on 187 00:07:17,216 --> 00:07:19,373 ‫to get a feeling of how Lambda works.