Deploy ECR-Based AWS Lambda
Let's see how to deploy an ECR image-based Lambda onto the AWS Console.
Join the DZone community and get the full member experience.
Join For FreeAWS Lambda is one of the most popular FaaS (function as a service) platforms out there. Its serverless capabilities and low cost per execution make it a go-to service for developers globally. Lambda supports multiple framework applications and multiple deployment models, and it helps developers customize their networking, permissions, and concurrency configurations.
When we talk about multiple deployment models in Lambda, container image-based deployments are one of the most popular ways to deploy their application on Lambda packaged into an image. With the benefits of containerization, developers can easily deploy applications by providing application dependencies in their application's Docker image instead of mentioning their dependencies and configuration separately in their Lambda.
AWS also provides the ECR (Elastic Container Registry) service. Developers store their images in their image repository, which helps them by providing version-based deployment. The only cost you pay is for image storage and data transfer activity.
Let's see how one can deploy an ECR image-based Lambda onto the AWS Console. (If you already have an image repository over ECR, then you can skip the first steps on creating one.)
Create ECR Image Repository and Add Image to It
1. Open AWS Console and navigate to ECR service.
2. Click on "Create repository" and fill out the required parameters like repository name and repository type (whether public or private), and based on your requirements, you can opt for image scan and encryption configuration.
3. Once your repository is created, then go to your repository and click on "View push commands."
4. After clicking on it, you will have a prompt stating the ways in which you can push your image to the repository.
5. Please follow the instruction provided into the prompt and push your image to the repository.
6. Once you push your image to the repository, it should look like this:
Create Lambda With ECR Image Using AWS CLI
1. Please make sure you have AWS CLI installed into your system. You can verify it by typing aws --version
.
2. You also need to configure your AWS credentials using aws configure --profile profile_name
. Please add respective ID and secret of your credentials when asked.
3. You can verify whether you correctly configured your user or not using aws sts get-caller-identity --profile profile_name
. If you get the correct output, then you are good to go. Just make sure your user that is configured into your AWS CLI has the right permissions to create a Lambda and has sufficient ECR permissions to pull an image from it.
4. Now, let's jump to creating a JSON file which will consist of Lambda's configuration.
{
"FunctionName": "ecrtestlambda",
"PackageType": "Image",
"Description": "ECR Testing Lambda",
"Role": "arn:aws:iam::************:role/DemoRoleForEC2",
"Code": {
"ImageUri": "************.dkr.ecr.ap-south-1.amazonaws.com/tushar-batchjob-1609:latest"
}
}
Let's understand this JSON file.
FunctionName
represents the name of the function that you want to create.
PackageType
represents the code package type. We have already mentioned Image
, which means that the code is packaged as an image.
Description
represents the function description, if you have any.
Role
represents the IAM role that you want to configure to your Lambda, which helps in performing actions on behalf of your Lambda.
Code
represents information on the source code location, whether a zip file or image URI.
Note: In this case we will use the ImageUri
option as we are using an ECR image-based deployment. You can copy the image URI from your image repository section by clicking on the "Copy URI" link:
5. Save this JSON file into your known location.
6. Now let's go ahead with the creation of a Lambda. Use the below command to create a Lambda with respective ECR configuration.
aws lambda create-function --cli-input-json fileb://C:\Users\test\Downloads\lambdaconfig.json --profile profile_name --region region_name
Here you just need to mention your file path after the fileb://
section. As you can see, we have provided a --cli-input-json
parameter which states that we are using the JSON file as input configuration for the Lambda.
Also, you need to mention the profile name (make sure it has required access to create Lambda and access ECR image, as explained before) and region name, as per your requirements.
7. With proper access and correct configuration, you should be able to create an ECR-based Lambda.
Important Notes
1. Your configured user on AWS CLI should have proper access to create Lambda and access to ECR images.
2. Your image should not be corrupted.
I hope this article was helpful to you in some way, and I thank you for taking the time to read it and go through it. Do like the post if you think it was helpful to you.
Cheers!
Opinions expressed by DZone contributors are their own.
Comments