Running AWS Lambda Container Images on a Serverless Framework

Tutorial Highlights & Transcript

  • 00:00 - Beginning of video

    • Today I'm going to present running Lambda containers images in serverless.

  • 00:15 - What is the Serverless Framework?

    • So first of all, I'm going to talk about serverless frameworks. This is a tool that is used to deploy mainly AWS Lambda functions. It also supports them. It can also provide the infrastructure needed in order to use specific Lambda functions. So it supports all of the major cloud providers. And it extends functionality by plugins, because it's maintained by the community. So I'm going to start with the core concepts of this.

  • 00:55 - Serverless Framework services

    • First of all the services, the service is basically the name of the project that we're going to be deploying into AWS. And it can be described in YAML or JSON format, and it looks like this. You will specify the name of the service, the function that you're going to create, and the resources. I'm going to give an overview of these concepts in the following slides.

  • 01:26 - Serverless Framework functions and events

    • So the function is basically a Lambda function. And it can serve several purposes such as saving a user into the database, I know that all of you know how Lambda functions. So another thing is, the events that we can manage by using serverless. It is basically anything that can trigger on AWS Lambda. Here I have listed most of the events that can trigger a Lambda, use API gateway, and so on.

  • 02:00 - Serverless Framework plugins and resources

    • The plugins help to extend functionality. And they need to be installed, by your way, using NPM, or by using serverless getting installed. And the other thing is the resources. You can create any resource in serverless. It must be stated in configuration syntax. So you can use anything that your Lambda depends upon. So that's the way that we can use resources in serverless.

  • 02:36 - How to set up a Lambda-ready Docker image

    • About this Lambda container image, this is a fairly new feature that Lambda has. It was presented in December 2020. And now you can hold up to 10 gigs in size. So we can avoid having just 250. And the only pricing that increased is by having an ECR repository where you are going to store your Lambda function Dockerized. So yeah, but that's the whole thing with Lambda container image. About the cold start AWS has mentioned that, or has stated that, it's not that different from Lambda. And basically, they said that it's very early to not have any cold start as it was before. So to set up a Lambda ready looking image and use it serverless. Here's the basic configuration that you will need in your Docker file. AWS already has some images that we can use from the public repository. But this doesn't stop us from using one of our own images, as long as it has the runtime interface client and functions inside your code. There is no problem using a custom image. So here you can see that basically, it's just copying the the index js in this case, I'm using Node js, all the dependencies in the package JSON and then installing the dependencies by running an NPM install, and then setting CMD with the handler that I have in the Lambda code.

  • 04:50 - Using Lambda images and the Serverless Framework

    • So by using Lambda images in serverless, you can create your ECR repository in the same file, let's say in the configuration serverless file. And this is the way that you create one ECR. And then this is the way that you attach, let's say the function to the ECR image. Or we can reference an existing image. And this is the way that you reference it.

  • 05:28 - Step-by-step demo of running Lambda containers using the Serverless Framework

    • So now I'm going to start the demo. So here I have the service, which is the main product that I choose for this. Here, I have the provider. This is where you will have the provider name. Of course, I'm using AWS. This is the stage name, I'm using demo, but you can use dev, prod, or as you please. And the region where I'll be deploying all of the infrastructure that I'll be creating here. Here, I have two ways of using ECR or Docker Lambda image. And here, I'm creating an ECR repository. And I am creating another function with a name. And here I'm specifying the path where this exists, or the Docker file, which is in here. Here's a Docker file that is going to be created and be pushed into the ECR repository that this will create automatically. And here I have an IAM role statement, which is going to be used for this Lambda. Also, I have a plugin that 's auto-installed. This is just for test purposes, but it's going to be used here. And here you have a custom, which is basically you can think of as a variable, let's say, which is the name of a bucket. And here I'm using it in referencing the service, which is this. So it's going to have the name in cloud service serverless, and the provider stage name, that is going to be demo and added Emilio to the bucket. So going to the functions, I have a function which is going to create, it's going to create a bucket, the whole infrastructure is going to say that, okay, well, I'm going to use this bucket to store an image, the, I'm going to pass just the URL, and it's going to store it into a bucket. The other function is just going to create a bunch of random text. And it has an event method by having the method get on the path in the app. So here I'm using the plugin. And the resource that is needed in order to use my Lambda, which is where I'm going to store the images that I previously mentioned, is here. Here, I'm referencing the bucket that is in here. So it's going to happen as I stay in this piece of code. So that's basically it about this. Here I have the two functions that I'm using. Here I have the Docker image created. It's not that complex. I'm just copying the js file, the package JSON, installing all the dependencies and running the handler with CMD. So it's also the same for the other Lambda that I have. Here, I'm going to explain about what's happening. So it's creating the ECR repository, it's building the image, it's going to push it to this repository that I stated in this piece of code. So while it finishes, serverless creates a deployment stack. It also creates an S3 where it's going to store the state of this deployment. And once it's done, I can invoke my Lambdas through serverless. So let's take a look at the formation. The creation is in progress. So once this is done, we can invoke our Lambda. I'm going to show you what I created. Both of the Lambdas have been created. Here I have the two Lambdas. So here are the two Lambdas that I created. Both of them are in package type as image, as you can see here. And the way that you invoke a Lambda, just let me share, so here you have the two buckets. One is the deployment bucket, the serverless that I created here, into the Resources section of the configuration file. I'm just going to show you my pre-run command that I have. I'm just going to store an image that is called a demo. Let's look at the image. It's right here. That's the car. The other function, it's going to generate a bunch of random text. Basically that's how to run and invoke a Lambda from serverless once it has been deployed, in order to do this function you will have to have installed Docker. Any plugin that you will need to use.

Emilio de Leon

Emilio de Leon

DevOps Engineer

nClouds

Emilio is a DevOps Engineer at nClouds. He is experienced in running containers on AWS and has his certification for HashiCorp Certified: Terraform Associate.

Contact Us Now

You can also email us directly at sales@nclouds.com for your inquiries or use the form below

Subscribe to Our Newsletter

Join our community of DevOps enthusiasts. Get free tips, advice, and insights from our industry-leading team of AWS experts.