Python script running on AWS Lambda with functions:
- Connect to PostgreSQL database
- Generate CSV files
- Upload the CSV files to AWS S3
Developed in Dec 2024.
- AWS account
- AWS Access Key ID (can be obtained at IAM > Users)
- AWS Secret Access Key (can be obtained at IAM > Users)
- Region (e.g. ap-southeast-1)
- Role / ARN (e.g. arn:aws:iam::123456789012:role/lambda-role)
- DB_HOST
- DB_PORT
- DB_NAME
- DB_USER
- DB_PASSWORD
- S3_BUCKET
- S3_KEY_PREFIX (e.g. reports)
This project utilizes awscli
to configure AWS, manage function code and function configuration. So, before clone this project, make sure you have awscli
installed on your machine.
# Install the AWS CLI
pip install awscli
# Verify the awscli installation
aws --version
Then configure the AWS account.
aws configure
Input your details and input json
as default output format.
You may use this script by clone this project using git command or download as .zip file then extract it.
This Project
│
├── package/ # folder to be uploaded to AWS Lambda
│ ├── boto3
│ ├── lambda_function.py # python script that we created to run on Lambda
│ ├── psycopg2
│ ├── requirements.txt # contains dependencies - install it using the command
│ │ # "pip install -r requirements.txt -t ."
│ ├── ... (other dependencies)
│
├── event.json # used for testing running scripts
└── README.md
After prepare the dependencies and the main script, compress it to .zip file
# make sure you are in the "package" directory
cd package
# zip all the files
zip -r ../lambda_function.zip .
For creating a new function in Lambda, we must have AWS Role/ARN
(for creating Role/ARN, please see "Create AWS New Role/ARN" section below)
# make sure you are outside the "package" directory, you are in the root directory of this project
cd ..
# create a new Lambda function
aws lambda create-function \
--function-name GenerateReports \
--runtime python3.11 \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://lambda_function.zip
Note:
GenerateReports
is function namepython3.11
is runtime Python versionrole
is your AWS role/ARNhandler
is your function namezip-file
is your ZIP file
aws lambda update-function-configuration \
--function-name GenerateReports \
--environment "Variables={DB_HOST=localhost,DB_PORT=5432,DB_NAME=xxx_db,DB_USER=user,DB_PASSWORD=xxx,S3_BUCKET=bucket-name,S3_KEY_PREFIX=reports}"
aws lambda update-function-configuration \
--function-name GenerateReports \
--timeout 900
aws lambda invoke \
--function-name GenerateReports \
--payload file://event.json \
response.json
It will generate response.json
, then you can check the response details.
aws lambda update-function-code \
--function-name GenerateReports \
--zip-file fileb://lambda_function.zip
- psycopg2-binary
- boto3
We need psycopg2-binary
for connect to PostgreSQL, to install it - please use from https://github.com/jkehler/awslambda-psycopg2. Clone it first, then copy the files based on your Python version (in this project we will use Python 3.11) then paste it into the “package” directory.
- Login to AWS Management Console
- Go to IAM service
- Choose “Roles” on the sidebar menu, then click “Create role”
- Choose “AWS Service” as trusted entity
- Choose “Lambda” from service list, then click “Next”
- For Permissions, add ”AmazonS3FullAccess” so this role can access to S3, then click “Next”
- Input the role name, like “lambda-s3-role”
- Click “Create role”