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
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
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
try: import json import sys import requests print("All imports ok ...") except Exception as e: print("Error Imports : {} ".format(e)) def lambda_handler(event, context): print("Hello AWS!") print("event = {}".format(event)) return { 'statusCode': 200, } |
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.
1 2 3 4 5 6 7 |
FROM public.ecr.aws/lambda/python:3.10 COPY shoe1.png ./ COPY shoe2.png ./ COPY requirements.txt ./ RUN pip3 install -r requirements.txt COPY myfunction.py ./ CMD ["myfunction.lambda_handler"] |
Different ways to create a Docker Container
- 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.
- Run this command to build an image on the container, random similarity is the image name
1 |
docker build -t image-similarity . |
2. Using Amazon EC2 Ubuntu
- Launch an Amazon EC2 with the Ubuntu Server 20.04 AMI
- SSH into the Server and Install Docker
1 2 3 4 5 6 7 |
sudo su apt-get update apt install docker.io snap install docker docker –version apt-get install awscli –y docker pull public.ecr.aws/lambda/python:3.7 |
- AWS IAM role with the below permission
1 2 |
AmazonEC2ContainerRegistryFullAccess AmazonElasticContainerRegistryPublicFullAccess |
- Pull the AWS base image python3.7 from Amazon Gallery: https://gallery.ecr.aws/lambda/python
1 |
docker pull public.ecr.aws/lambda/python:3.10 |
- Using the below command, check docker image is successfully pulled or not:
1 |
docker image ls |
- Run the image as a container
1 2 |
docker run -it -d IMAGEID /bin/bash docker exec -it CONTAINERID bash |
- Install all the dependencies inside the docker container
1 2 3 4 5 6 |
pip install awslambdaric Pip install numpy Pip install pandas Pip install scikit-learn Pip install opencv-python Pip install matplotlib |
Steps to upload Docker image from local/Amazon EC2 to Amazon ECR
- Create a private repository Amazon ECR as shown below:
2. After the repository is created. Select the repository and click on view push commands
3. Execute all the above commands
4. After running all the commands, we could see that image is pushed in Amazon ECR
Steps to create AWS Lambda using Amazon ECR
- 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.
2. Once AWS Lambda is created, click Test.
Conclusion
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
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.
Click to Comment