AWS, Cloud Computing, Kubernetes

5 Mins Read

Empowering Serverless Apps: AWS Lambda with Container Image Deployment

Voiced by Amazon Polly

Overview

AWS Lambda with Container Image Deployment enhances serverless app development, allowing developers to package their entire application into a container image, including code, runtime, and dependencies. Overcoming the limitations of AWS Lambda layers, this approach offers more control, enabling the inclusion of required libraries directly in the image. It streamlines workflows, simplifies testing, and integrates seamlessly with container-based tools. This empowers developers to build feature-rich serverless apps with scalability and cost-effectiveness.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Introduction to Amazon ECR

Amazon ECR stands for Amazon Elastic Container Registry. It is a fully managed container registry service provided by Amazon Web Services (AWS). Amazon ECR allows you to store, manage, and deploy container images, making it easier to build, test, and run containerized applications in the AWS ecosystem.

Benefits of Amazon ECR

  • Secure and Private Registry: Amazon ECR provides secure storage for your container images, ensuring they are encrypted at rest and in transit. It integrates with AWS Identity and Access Management (IAM) to manage access control, allowing you to define fine-grained permissions for users and AWS services.
  • Integration with AWS Services: Amazon ECR seamlessly integrates with other AWS services, such as Amazon Elastic Container Service (ECS), AWS Fargate, and AWS Lambda. You can use Amazon ECR to store your container images and easily deploy them to these services for running your applications.
  • Scalability and Performance: Amazon ECR is built on AWS’s highly scalable infrastructure, ensuring that you can push and pull container images quickly and reliably, even with high levels of concurrency. It automatically scales to handle increased demand without impacting performance.
  • Lifecycle Policies: Amazon ECR allows you to define lifecycle policies for your container images. You can configure rules to automatically expire or delete older images, helping you manage storage costs and ensure that only the necessary images are retained.
  • Integration with Docker CLI: Amazon ECR seamlessly integrates with the Docker CLI, allowing you to push and pull container images to and from your Amazon ECR repositories using familiar Docker commands. This simplifies the workflow for developers already using Docker in their local environments.
  • Registry Permissions and Sharing: Amazon ECR provides options for sharing container images with other AWS accounts or making them publicly accessible. You can control who can access your container images and easily collaborate with teams or share images with external users.

Pre-requisite setup

  1. Create a requirements.txt: This file will contain all the dependencies which are needed to execute the Python file in a docker container like the one below:
  • requests==2.25.1
  • pip==23.0.1
  • opencv-python-headless==4.7.0.72
  • numpy==1.24.2
  • matplotlib==3.7.0

2. Create a python file containing the code as below sample code:

3. Create Dockerfile: This text file contains instructions for building a Docker image. It serves as a blueprint or recipe for creating a Docker container with a specific configuration and set of dependencies. When you build a Docker image using a Dockerfile, it automates creating a reproducible and consistent environment for your application.

Different ways to create a Docker Container

  1. From local
  • Once you create these files under a specific folder, use the docker desktop to run the container. When you go to the docker desktop, you can see the command like the following on the homepage and execute the below command on CMD of Visual Studio Code.

docker

docker2

  • Run this command to build an image on the container, random similarity is the image name

docker3

2. Using Amazon EC2 Ubuntu

  • Launch an Amazon EC2 with the Ubuntu Server 20.04 AMI

docker4

  • SSH into the Server and Install Docker
  • AWS IAM role with the below permission
  • Using the below command, check docker image is successfully pulled or not:
  • Run the image as a container
  • Install all the dependencies inside the docker container

Steps to upload Docker image from local/Amazon EC2 to Amazon ECR

  1. Create a private repository Amazon ECR as shown below:

ec

2. After the repository is created. Select the repository and click on view push commands

ec2

ec3

3. Execute all the above commands

ec4

ec5

ec6

ec7

4. After running all the commands, we could see that image is pushed in Amazon ECR

ecr4

Steps to create AWS Lambda using Amazon ECR

  1. Go to the AWS Lambda function. Click on create function, choose Container Image, provide the name for AWS Lambda, browse the container image, and click Create.

ecr

ecr2

2. Once AWS Lambda is created, click Test.

ecr3

Conclusion

Deploying an AWS Lambda function using a container image provides several advantages for running serverless applications. By leveraging AWS Lambda’s support for custom container environments, you can package your application code, dependencies, and configurations into a container image and deploy it as an AWS Lambda function.

Drop a query if you have any questions regarding AWS Lambda, Amazon ECR and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. Why should we create AWS lambda using Amazon ECR?

ANS: – By leveraging an Amazon ECR Docker image for your Lambda function, you can work around dependencies that don’t work well with AWS Lambda layers and gain more flexibility in managing dependencies within a containerized environment. However, it’s essential to monitor and optimize your function’s performance and resource utilization, considering the size and content of the Docker image.

2. Can the AWS Lambda function be created using multiple Docker images from different repositories?

ANS: – No, an AWS Lambda function can only use a single Docker image from a single Amazon ECR repository. AWS Lambda functions are designed to be self-contained and operate within the context of a single container image. If your application requires multiple Docker images, you would typically split the functionality into separate AWS Lambda functions, each using its container image.

3. Can I update the Docker image in Amazon ECR without redeploying the AWS Lambda function?

ANS: – Yes, you can update the Docker image in Amazon ECR without redeploying the Lambda function. To update the image, you can build a new version of the image with the desired changes, tag it with a new version or tag, and push it to the ECR repository. The AWS Lambda function can be configured to use the latest version or a specific version or tag of the image. The next invocation of the Lambda function will automatically use the updated image.

WRITTEN BY Hridya Hari

Hridya Hari works as a Research Associate - Data and AIoT at CloudThat. She is a data science aspirant who is also passionate about cloud technologies. Her expertise also includes Exploratory Data Analysis.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!