1 00:00:00,360 --> 00:00:02,880 ‫So I told you that Lambda was supporting 2 00:00:02,880 --> 00:00:05,670 ‫container images and this is a new feature. 3 00:00:05,670 --> 00:00:08,730 ‫And this allows to deploy Lambda Function as a container 4 00:00:08,730 --> 00:00:12,090 ‫of images up to 10 gigabytes from ECR 5 00:00:12,090 --> 00:00:14,430 ‫which allows you to pack complex dependencies 6 00:00:14,430 --> 00:00:16,410 ‫and large dependencies in a container. 7 00:00:16,410 --> 00:00:19,740 ‫So Docker is very famous to allow you to help 8 00:00:19,740 --> 00:00:23,190 ‫to put your application code, the dependencies, 9 00:00:23,190 --> 00:00:25,890 ‫and the data sets it needs all together 10 00:00:25,890 --> 00:00:27,630 ‫on top of a base image. 11 00:00:27,630 --> 00:00:32,220 ‫And that base image must implement the Lambda Runtime API. 12 00:00:32,220 --> 00:00:35,670 ‫So the idea is that to make it seemingly simple, 13 00:00:35,670 --> 00:00:38,910 ‫Lambda runs a virtual machine, the container. 14 00:00:38,910 --> 00:00:40,950 ‫And so you use the base image of that container, 15 00:00:40,950 --> 00:00:44,070 ‫you add on your application code and your dependencies 16 00:00:44,070 --> 00:00:46,710 ‫and you pack this as an image that Lambda can run 17 00:00:46,710 --> 00:00:51,390 ‫because this base image implements the Lambda Runtime API. 18 00:00:51,390 --> 00:00:53,820 ‫And this allows you to run your containers onto Lambda, 19 00:00:53,820 --> 00:00:55,560 ‫but it's not any Docker container. 20 00:00:55,560 --> 00:00:58,350 ‫The base image must implement the Lambda Runtime API. 21 00:00:58,350 --> 00:01:01,560 ‫So these base images exist for multiple languages. 22 00:01:01,560 --> 00:01:06,240 ‫For example, Python, Node.js, Java, .NET, Go and Ruby. 23 00:01:06,240 --> 00:01:09,390 ‫But you can create your own Lambda base image 24 00:01:09,390 --> 00:01:12,600 ‫as long as you implement the Lambda Runtime API. 25 00:01:12,600 --> 00:01:14,250 ‫And for the specification of this, 26 00:01:14,250 --> 00:01:15,570 ‫you can look in the documentation. 27 00:01:15,570 --> 00:01:16,980 ‫It's quite complicated. 28 00:01:16,980 --> 00:01:20,520 ‫Then this also allows you to test your containers locally 29 00:01:20,520 --> 00:01:23,640 ‫using the Lambda Runtime Interface Emulator. 30 00:01:23,640 --> 00:01:26,640 ‫And if you have your Lambda Functions as containers, 31 00:01:26,640 --> 00:01:28,410 ‫this allows you to get a unified workflow 32 00:01:28,410 --> 00:01:29,490 ‫to publish your apps. 33 00:01:29,490 --> 00:01:32,880 ‫That means that no matter if it's a container for 34 00:01:32,880 --> 00:01:34,740 ‫ECS or a container for Lambda, 35 00:01:34,740 --> 00:01:36,960 ‫you can build and publish your containers 36 00:01:36,960 --> 00:01:40,140 ‫the same way and send them into Amazon ECR, 37 00:01:40,140 --> 00:01:41,760 ‫and then from Amazon ECR, 38 00:01:41,760 --> 00:01:45,030 ‫deploy your containers onto Lambda. 39 00:01:45,030 --> 00:01:47,700 ‫So what does a Lambda container image look like? 40 00:01:47,700 --> 00:01:49,020 ‫Well, here's an example 41 00:01:49,020 --> 00:01:51,960 ‫where we can build from the base images provided by AWS. 42 00:01:51,960 --> 00:01:54,240 ‫Number one, we choose an image that implements 43 00:01:54,240 --> 00:01:55,800 ‫the Lambda Runtime API, 44 00:01:55,800 --> 00:02:00,780 ‫and this is from amazon/aws-lambda-nodejs version 12. 45 00:02:00,780 --> 00:02:03,090 ‫Then we copy our application code and file. 46 00:02:03,090 --> 00:02:05,820 ‫So we copy the app.js package.json file 47 00:02:05,820 --> 00:02:08,640 ‫and all the data onto our container. 48 00:02:08,640 --> 00:02:11,040 ‫Then we install the dependencies we need in the container. 49 00:02:11,040 --> 00:02:13,410 ‫So we run npm install. 50 00:02:13,410 --> 00:02:15,870 ‫And finally, we say which function to run 51 00:02:15,870 --> 00:02:17,610 ‫when the Lambda Function is invoked. 52 00:02:17,610 --> 00:02:19,623 ‫So we say command app.lambdaHandler. 53 00:02:21,180 --> 00:02:23,190 ‫And that's all you need to do. 54 00:02:23,190 --> 00:02:27,690 ‫This Docker image, because it is made from the base image 55 00:02:27,690 --> 00:02:29,400 ‫of Lambda for Node.js 12 56 00:02:29,400 --> 00:02:31,860 ‫will be able to be run on Lambda Functions. 57 00:02:31,860 --> 00:02:34,530 ‫And so that's quite magical, but that's amazing 58 00:02:34,530 --> 00:02:36,390 ‫because you don't need to worry too much 59 00:02:36,390 --> 00:02:38,700 ‫about your dependencies, 60 00:02:38,700 --> 00:02:40,320 ‫compiling it correctly and so on. 61 00:02:40,320 --> 00:02:43,080 ‫As long as the Docker container is made correctly 62 00:02:43,080 --> 00:02:44,370 ‫from the correct base image, 63 00:02:44,370 --> 00:02:46,170 ‫it will be able to run on Lambda, 64 00:02:46,170 --> 00:02:48,570 ‫which is a great alternative to, for example, 65 00:02:48,570 --> 00:02:51,180 ‫compiling your own Lambda layers if you needed to. 66 00:02:51,180 --> 00:02:53,010 ‫So let's look at best practices 67 00:02:53,010 --> 00:02:55,770 ‫when using Lambda container images. 68 00:02:55,770 --> 00:02:58,800 ‫So first, you can optimize your container images. 69 00:02:58,800 --> 00:03:01,410 ‫The number one thing to do is to use the AWS 70 00:03:01,410 --> 00:03:04,680 ‫provided base images, which is built on Amazon Linux 2. 71 00:03:04,680 --> 00:03:06,330 ‫And the reason we want to use this is that 72 00:03:06,330 --> 00:03:09,540 ‫these base images are already cached by the Lamba service, 73 00:03:09,540 --> 00:03:10,680 ‫and so if you use them, 74 00:03:10,680 --> 00:03:13,410 ‫then the Lambda service has to pull less information 75 00:03:13,410 --> 00:03:14,850 ‫out of your containers. 76 00:03:14,850 --> 00:03:17,160 ‫Also, you should use multi-stage builds. 77 00:03:17,160 --> 00:03:18,690 ‫So the idea is that you can 78 00:03:18,690 --> 00:03:20,850 ‫do whatever is the most complicated things 79 00:03:20,850 --> 00:03:24,600 ‫to build your code in preliminary large images 80 00:03:24,600 --> 00:03:27,120 ‫and then the output of your build 81 00:03:27,120 --> 00:03:29,640 ‫is that you only copy the artifacts you need 82 00:03:29,640 --> 00:03:32,370 ‫and you create a new final container image 83 00:03:32,370 --> 00:03:35,010 ‫where you discard all the preliminary steps. 84 00:03:35,010 --> 00:03:37,980 ‫Therefore, the final image is going to be much smaller 85 00:03:37,980 --> 00:03:39,960 ‫and much more simple. 86 00:03:39,960 --> 00:03:42,960 ‫You can also build your image in different layers 87 00:03:42,960 --> 00:03:46,200 ‫and the layers are from stable to frequently changing. 88 00:03:46,200 --> 00:03:48,660 ‫The idea is that the things that changes the most 89 00:03:48,660 --> 00:03:51,330 ‫should be at the end of your build phase, 90 00:03:51,330 --> 00:03:52,920 ‫and the things that changes less, 91 00:03:52,920 --> 00:03:54,180 ‫such as for example 92 00:03:54,180 --> 00:03:57,120 ‫installing base packages on your image 93 00:03:57,120 --> 00:03:59,160 ‫should be as early as possible. 94 00:03:59,160 --> 00:04:02,070 ‫And finally, you should use a single repository 95 00:04:02,070 --> 00:04:03,870 ‫for functions with large layers. 96 00:04:03,870 --> 00:04:05,640 ‫The idea is that if you have large layers 97 00:04:05,640 --> 00:04:07,320 ‫and they're all in the same repo, 98 00:04:07,320 --> 00:04:09,810 ‫then ECR will have more facility 99 00:04:09,810 --> 00:04:11,790 ‫to compare these layers together 100 00:04:11,790 --> 00:04:14,220 ‫and this will avoid you from uploading and storing 101 00:04:14,220 --> 00:04:16,470 ‫duplicates of these layers. 102 00:04:16,470 --> 00:04:18,630 ‫Finally, one of the best use cases 103 00:04:18,630 --> 00:04:20,310 ‫for Lambda container images 104 00:04:20,310 --> 00:04:23,460 ‫is if you want to upload a very large Lambda Function 105 00:04:23,460 --> 00:04:25,170 ‫of up to 10 gigabytes. 106 00:04:25,170 --> 00:04:28,800 ‫Then instead of having some code pushed to Lambda as is, 107 00:04:28,800 --> 00:04:31,500 ‫you can create a very large container image 108 00:04:31,500 --> 00:04:35,220 ‫and use that as the basis for your Lambda Function. 109 00:04:35,220 --> 00:04:37,080 ‫Okay, that's it for this lecture. 110 00:04:37,080 --> 00:04:40,293 ‫I hope you liked it, and I will see you in the next lecture.