How to Upload Lambda Zip File in Aws Lambda
Deploy AWS Lambda functions using .goose egg file archives
In computer programming, a lambda role refers to a small anonymous function that tin take whatsoever number of arguments.
AWS Lambda is a compute service that lets you run code without provisioning or managing servers. Information technology's a saviour if we need some custom logic to exist integrated with other AWS services, eg. processing images before storing in AWS S3 or creating a backup bucket in S3.
It's equally simple equally it gets, merely write codes in the linguistic communication of your pick (and is supported past AWS Lambda) and configure information technology with a trigger or integrate information technology with Lambda API Gateway. Your life is perfectly fine and relaxed until you come across any imports. I've worked in python runtime for most of my Lambda days and then I'll speak in that context simply feel free to relate as the feeling is quite general. Most of the python modules which we use are non pre-installed on the AWS Lambda runtime. We volition install and apply all those modules using a special feature known as AWS Lambda Layers.
Before we proceed further it'southward good to take some bones agreement of AWS EC2 Instances and Linux terminals. Nosotros volition use a virtual Linux automobile to create a .zip file (which contains all the source and vendor codes) as I've plant that it's the safest way to avoid Bone compatibility issues in every possible situation.
Notation: AWS Lambda runtime runs on a Linux machine and if we create an archive in Windows or Mac it flags Bone compatibility bug while deployment.
Now first things first. Let's go ahead and spin an Amazon Linux on EC2 instance running t2.micro
Connect to your instance using Connect in EC2.
And so run sudo yum update the command to get your auto up to date.
Runsudo su to become into root user mode (now you lot tin can run commands without the use of sudo).
Run cd to come to the base folder.
Now nosotros will install python and pip on our machine
We must install the same version of python we will be using in our Lambda runtime.
Every bit python installation required the GCC compiler on our organization we can apply the following command to install prerequisites for Python before installing information technology.
Download Python using the following command from the Python official site. You tin can also download the latest version in place of the specified below.
Now let's extract the downloaded package.
Now we'll install python. We apply the below set of commands to compile Python source code for our organization using altinstall.
make altinstall is used to forbid replacing the default python binary file /usr/bin/python.
At present we remove the downloaded source archive file from our system
Check the latest version installed of python. We use the control python3.7 instead of python. The new binary will exist installed at /usr/local/bin/python3.7 location:
Create python file
At present we'll create a folder cancel where nosotros make a new cancel.py file needed to deploy on lambda.
Create a file using sudo nano control for Linux and put your code in information technology.
Now run the code using python cancel.py (or python3 abolish.py). You will most probably go the following error.
This is because the pymongo module is not installed. We will install this module using below pip command (which creates a vendor folder in this root folder).
Now running the higher up code will yield no error. We are all prepare to zip up our code.
Zipping information technology upwardly!
This below control will cypher all the files and folders in my root folder (cancel) into a naught file chosen abolish.goose egg
Contents of abolish folder would expect like below.
Upload zip file to S3 saucepan
Before we upload our file to our S3 bucket we have to ensure the below steps.
- Create an IAM role with S3 write admission or admin access
- Map the IAM role to an EC2 example
- Install AWS CLI in EC2 example
- Run the AWS s3 cp command to copy the files to the S3 bucket
Create an IAM role with S3 write access or admin access
Login to your AWS Management Panel and go to IAM.
Create a new IAM role with S3 Admin Access which tin be lateral mapped to the EC2 instance for easy S3 and EC2 integration.
Refer the post-obit GIF to know how to create a new IAM role for S3 access
https://world wide web.middlewareinventory.com/wp-content/uploads/2020/09/S3IAM.mp4?_=1
Map the IAM part to an EC2 instance
Choose the EC2 instance you lot want to assign this IAM role to.
Click on Actions > Security> Modify IAM Office
Search for the IAM Role we have created in the previous step and select it and hit Save.
Install AWS CLI in EC2 instance
This is already installed on your auto as nosotros chose Amazon Linux at first place.
Run the AWS s3 cp command to copy the files to the S3 saucepan
Use the following commands to copy the files to S3 Bucket from EC2.
aws s3 cp abolish.nada s3://<bucket>/cancel.zippo
Running the Lambda function
We can easily utilise the file from S3 by providing it'south S3 URI in AWS Lambda upload choice.
Copy the URI from S3.
Upload it on lambda using option below.
Now the file is uploaded to your AWS Lambda and set up for deployment.
The use of trigger and Lambda API gateway is outside the scope of this article then I'll keep it for another one. Till then may the forcefulness be with you.
Source: https://shashank404.medium.com/deployaws-lambda-functions-using-zip-file-archives-585d2a83371f
Belum ada Komentar untuk "How to Upload Lambda Zip File in Aws Lambda"
Posting Komentar