-
Notifications
You must be signed in to change notification settings - Fork 0
Creating AWS Lambda function
Hovhannes Apinyan edited this page Apr 5, 2024
·
1 revision
-
Create a Python File: In your preferred directory, create a Python file named
lambda_function.py.touch lambda_function.py
-
Edit the File: Open
lambda_function.pyin a text editor and write your Lambda function code.def lambda_handler(event, context): # Your code logic return { 'statusCode': 200, 'body': 'Hello from Lambda!' }
-
Create the Virtual Environment: In the directory where your Python file is, run:
python3 -m venv venv
-
Activate the Virtual Environment:
source venv/bin/activate -
Install Dependencies: Install any needed libraries using pip.
pip install requests # Replace 'requests' with your required packages
-
Navigate to the Site-Packages Directory:
Replace
cd venv/lib/python3.x/site-packagespython3.xwith your specific Python version. -
Create the ZIP Archive:
- Add the contents of the
site-packagesdirectory and your Python script to a ZIP file.zip -r9 ../../function.zip . - Move back to the project directory and add your Python file to the ZIP archive.
cd ../../../ zip -g function.zip lambda_function.py
- Add the contents of the
- Go to the AWS Management Console in your web browser.
- Find and open the AWS Lambda service, either through the search bar or the services list.
- Click on the
Create functionbutton to start the process of creating a new Lambda function. - Select
Author from scratch. - Enter a name for your function in the
Function namefield. - In the
Runtimedropdown, select the Python version that matches the one you used in your local environment. - Click on the
Create functionbutton at the bottom of the page to create your Lambda function’s basic configuration.
- Once your function is created, you will be taken to its configuration page.
- Find the
Code sourcesection. - Click on
Upload from, then select.zip file. - Use the file picker to locate and select the zip file you created earlier, which contains your function and its dependencies.
- After selecting the file, click
Saveto upload your function code to AWS Lambda.
By following these steps, you have created a new Lambda function in AWS Lambda, configured its basic settings, and uploaded your Python code packaged in a zip file. Your Lambda function is now ready to be integrated with other AWS services or invoked directly.