Wednesday, July 8, 2020

AWS Lambda Python - Build from your local machine - Upload as zip file - boto3 - import modules - Linux - Ubuntu

Hi Folks,

Now everything is PaaSified :)

So today we are going to looking in to creating and deploying AWS Lambda - Python from ZERO.


Here we go...



Step1. Create a new AWS Lambda using AWS portal or AWS CLI

Step 2. Install Python and pip in your local machine

Step 3. Create a new folder in your local machine

Step4. create a new file YOUR_FILE_NAME.py in that folder

Step5. Do some coding in that file

Sample code
import json print('Loading function') def lambda_handler(event, context): print("This is from Lambda ") return "Lambda Executed Successfully !"

Step7. Save the file

Step8.  Install boto3 modules for making python function ready

pip install boto3 -t ./

Step9. Make a ZIP package with correct file permissions



cd YOUR-FOLDER-NAME
chmod -R 755 .
zip -r ../myDeploymentPackage.zip .

Step10. Upload myDeploymentPackage.zip in Lambda using AWS portal or AWS CLI


  1. Open the AWS Lambda console.
  2. Choose Functions on the navigation pane, and then open your function.
  3. In the Function code section, expand the Code entry type drop-down list, and then choose Upload a .ZIP file.
  4. Choose Upload, and then select your .zip file.
  5. Choose Save.
  6. Choose Test.
You can also upload your .zip file from the AWS Command Line Interface (AWS CLI) using the update-function-code command.


Step11.  Make sure that you have proper settings


Handler should set as YOUR_FILE_NAME.YOUR_HANDLER_METHOD_NAME

For example

If you saved your file as "lambda_function.py" and the handler method is  "lambda_handler(event, context)" you should set "lambda_function.lambda_handler" as Handler in settings. Otherwise you will get error.


Same like step8 you can install any other modules for your code.


You are all set... All the best ! Happy Coding...!!


No comments:

Post a Comment