In this blog, we will see how we can create an AWS Lambda using AWS ECR Docker image in Python. Creating a Lambda function using a container image can be very complex if you are doing it for the very first time. I myself was when doing it, I didn’t find any on-point blog or article which teaches you that.
That’s why no one else falls in this pit ever again in the future, I am writing this blog for those who are also in the situation I was in 2 to 3 months back because I know how frustrating it could be.
So without any further due let’s do it.
Step 1 – Create a new folder and add your py file to it.
- Simply create a folder and add your code file to it.
Step 2 – Create the Dockerfile in the folder and edit it.
- Create a Dockerfile as shown below and edit it at your convenience.
- In line 1 change the python version as per your requirement. Keep in mind this should be the same as your conda environment in the next step.
- In line 5 change test.py to your file name.
- In line 6 change the test to your python file name. Like my file name was test.py and it contained the function handler so for me it’s ‘test.handler’.
Step 3 – Create a conda environment.
- Use command conda create -n <envname> python=<version> command.
- It will install some basic libraries. When prompted hit ‘y’ and Enter.
Step 4 – Activate the conda environment.
- Activate your conda environment using conda activate <env name> command.
Step 5 – Install required libraries in the conda environment.
- Install required packages/libraries in the conda environment.
- Use command pip install <space separated package names> to install packages in the environment.
Step 6 – Generate the requirements.txt file.
- We need a requirements.txt file for creating our docker container.
- We will use the pip free > requirements.txt command to store all the packages present in the conda environment in the requirements.txt file.
Step 7 – Create a new ECR object on AWS.
- Search ECR in the search bar and click on Elastic Container Registry.
- Then click on Create repository.
- On the next screen, just give a name to your ECR container. I named it test-ecr.
- When successfully created, search for it on ECR main screen and click on it.
Step 8 – Open Push Commands.
- When opened it will look like this.
- As you can see there is no image here as of now.
- Click the View push commands button.
Step 9 – Execute Push Commands.
- Below are the 4 push command you need to execute in your terminal.
- Make sure your conda env is activated.
- The first command helps you to login to AWS. (Make sure aws-cli is installed on your Windows/Linux and you have credentials set on your PC)
- The first time when you will run login on your PC it will ask for credentials. You can get them in IAM > Users > Security Credentials > Create Access key.
- The second step helps in building the image.
- The third step tags the image.
- And the fourth step helps in pushing the image from local to ECR.
- If all 4 commands are executed without any error, your ECR will show one entry with the name ‘latest’.
Step 10 – Create a new Lambda Function.
- In the main search bar search for Lambda.
- Click on the Create Function button.
- It will show a window like below.
- Click on Container Image.
- Give your Lambda function a name and select the container image below it that you want to use.
- This will create your lambda successfully with the ECR Image you selected.
- Your Lambda will look like the following image.
Step 11 – Test if everything is working fine or not.
- My lambda just takes 2 numbers and returns their sum.
- So I have provided 2 numbers and my lambda just returns their sum.
- If everything you did till now is correct, the test result will show a Green prompt with a ‘succeeded’ message.
- You can see the result is 9.
NOTE – Lambda just provides 3 seconds timeout. This means if your Lambda runs for more than 3 seconds it will not work. Go to the configuration > General configuration and change that acc to your need. Keep in mind max you can set is 15 minutes.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…