How to build, test, and deploy your first Lambda on AWS – 2024

Hey guys, in today’s blog we will see how to deploy your first Lambda on AWS. In the last blog, we saw a detailed step-by-step guide on How to create an API in AWS API Gateway using AWS Lambda. So without any further due, let’s do it.

Table of Contents

You can create Lambda in 3 different ways:

  • First, you can directly write the code in the AWS Lambda code editor. (For light use case)
  • Second, you can upload a code zip to S3 or directly to the Lambda console. (For medium use case)
  • The third is to use an AWS ECR container to deploy your Lambda. (For heavy use case)

First way

  • Simply search Lambda in the AWS search bar and open it.
deploy your first Lambda on AWS
  • Once you open it, simply click on Create Function button.
  • It will open up a page like this.
deploy your first Lambda on AWS
  • Select Author from scratch and give some name to your Lambda. I named it test-lambda.
  • Select your runtime. I selected Python 3.9.
  • Simply scroll and click Create Function.
  • It will open up a page like this.
deploy your first Lambda on AWS
  • On this page, scroll down and you will see a code editor.
  • Simply write your code there.
deploy your first Lambda on AWS

NOTE – The headers that I have added are used when we need to create an API from this Lambda. You can remove them according to your need.

  • I have added a simple code that takes 2 numbers as parameters from the request and returns their sum.
  • Once done hit the Deploy button.
  • Then click the Test button.
  • It will open up a window like this.
deploy your first Lambda on AWS
  • Create your test event here. I am just sending 2 numbers from my test event here.
  • Scroll and simply hit on the Create button.
  • If everything is done right, you will see a prompt like this.
deploy your first Lambda on AWS
  • Then finally again click on the Test button.
deploy your first Lambda on AWS
  • And here are the results.
  • You can see the body of this response depicts 7 which is the sum of the 2 numbers.

NOTE – Use this method only when you are doing some minor tasks and you don’t have any requirements for extra packages/libraries. If you are having some heavy requirements like Tensorflow, go with the ECR method.

Second way

  • Create a folder and make a file named lambda_function.py in it.
  • Add your code to that file.
1
  • Here I have added NumPy just for showing how to add packages in your zip.
  • Once done create a conda environment using the following command.
  • I name my env testlambda, you can name it anything.
 conda create -n testlambda python=3.9
  • Once created it will ask to install some default libraries, hit y (yes) on that.
  • Once done activate the environment and install NumPy in the environment.
conda activate testlambda
pip install numpy
  • Once installed, run the following commands.
pip freeze > requirements.txt
pip install -r requirements.txt -t .
  • It will install all the packages at the current location.
deploy your first Lambda on AWS
  • Then simply zip all the files except the requirements.txt file.
deploy your first Lambda on AWS
  • Simply search Lambda in the AWS search bar and open it.
deploy your first Lambda on AWS
  • Once you open it, simply click on Create Function button.
  • It will open up a page like this.
deploy your first Lambda on AWS
  • Select Author from scratch and give some name to your Lambda. I named it test-lambda.
  • Select your runtime. I selected Python 3.9.
  • Simply scroll and click Create Function.
  • It will open up a page like this.
deploy your first Lambda on AWS
  • On this page, scroll down and you will see a dropdown Upload from.
deploy your first Lambda on AWS
  • Click Upload from, and select .zip file and then simply select the zip file that you created.
  • Once done, simply test your lambda by hitting the Test button and creating your test event.
  • And if everything you did till now was correct, you will see a result like this.
deploy your first Lambda on AWS

NOTE – For files larger than 10 MB, consider uploading your zip to Amazon S3 and selecting the Amazon S3 location in Upload from dropdown.

Third Way

Check out this blog on how to deploy Lambda using ECR Docker ImageHow to create an AWS Lambda using AWS ECR Docker container Image in Python

So this is how you will deploy your first Lambda on AWS, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net

Leave a Reply

Your email address will not be published. Required fields are marked *