AWS Lambda function with requests module

Introduction

In this tutorial, we will create an AWS Lambda function with requests module. Then create a .zip deployment package containing the dependencies.

Prerequisites

Before starting, you should have the following prerequisites configured

  • An AWS account
  • AWS CLI on your computer

Walkthrough

  • Create the deployment package
  • Create AWS Lambda function with requests module

Create the deployment package

Navigate to the project directory containing your lambda_function.py source code file. In this example, the directory is named my_function.

mkdir my_function
cd my_function
ls -alt
AWS Lambda function with requests module

Install “requests” dependencies in the my_function directory.

pip3 install requests --target .
image 1
image 2

Create lambda_function.py source code file. This sample uses region_name=”ap-northeast-1″.(Tokyo region)

import boto3
import requests

def lambda_handler(event, context):
    file_name = "2023-11-26-0.json.gz"
    bucket_name = "hieu320231129"
    print(f'Getting the {file_name} from gharchive')
    res = requests.get(f'https://data.gharchive.org/{file_name}')
    print(f'Uploading {file_name} to s3 under s3://{bucket_name}')
    s3_client = boto3.client('s3', region_name="ap-northeast-1")
    upload_res = s3_client.put_object(
        Bucket=bucket_name,
        Key=file_name,
        Body=res.content
    )

    objects = s3_client.list_objects(Bucket=bucket_name)['Contents']
    objectname= []
    for obj in objects:
        objectname.append(obj['Key'])

    return {
        'object_names': objectname,
        'status_code': '200'
    }

Create a .zip file with the installed libraries and lambda source code file.

zip -r ../my_function.zip .
cd ..
ls -alt
image 3
image 4

Test lambda function from local computer.

# check bucket
aws s3 ls s3://hieu320231129/ --recursive

#invoke lambda function from local
python3 -c "import lambda_function;lambda_function.lambda_handler(None, None)"

#delete uploaded file
aws s3 rm s3://hieu320231129/ --recursive
image 5

Create AWS Lambda function with requests module

I will deploy zip file with python3.11 and change environment setting

image 6

Change environment setting

image 7

Test lambda function with request module

image 8

Conclusion

These steps provide an example of creating a lambda function with dependencies. I used the request module to read a file from a website and put it into an AWS S3 bucket. The specific configuration details may vary depending on your environment and setup. It’s recommended to consult the relevant documentation from AWS for detailed instructions on setting up. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Refer

https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

,

About Dang Nhu Hieu

I'm Vietnamese. In the past, I'm a software developer, now working in Japan on an Infra team. Skill : AWS, VMware, HA architech,setting Database : Oracle DB, PostgresDB ,.. Programming language : Java, C#, Python, Bash linux, Batch windows, powershell ,... Hobbies: badminton, film photo, travel. https://www.linkedin.com/in/hieu-dang-15a0561a6/
View all posts by Dang Nhu Hieu →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.