Load RDS from S3 parquet files using lambda function

pathanjali tallapragada
5 min readOct 12, 2023

Below concepts will be discussed

  1. Prerequisites
  2. Role permissions
  3. Lambda code
  4. Utilizing secret manager(best practice)
  5. Batch processing
  6. Test Lambda function
  7. Creating test event
  8. Trigger lambda using API call

Loading files from S3 to RDS SQL server can be tough, if you have very large files and lambda will never be a solution.

This solution works when the file sizes are small.


  1. AWS account
  2. S3 bucket
  3. RDS (SQL server)
  4. some parquet files to load

First of all, let’s set the AWS permissions

You need to have below permissions to the role you are using

Secret manager to manage the RDS password

"Version": "2012-10-17",
"Statement": [
"Sid": "VisualEditor",
"Effect": "Allow",
"Action": [
"Resource": "*"

S3 bucket.

"Version": "2012-10-17",
"Statement": [
"Sid": "VisualEditor",
"Effect": "Allow",
"Action": [
"Resource": "*"

Cloudwatch logs

"Version": "2012-10-17",
"Statement": [
"Sid": "VisualEditor",
"Effect": "Allow",
"Action": [
"Resource": "*"


"Version": "2012-10-17",
"Statement": [
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": "ARN here:*"
"Effect": "Allow",
"Action": [
"Resource": [
"ARN:log-group:log group here:*"

Now that permissions are setup, we need to load the files into S3.

Once the files are uploaded. Let’s create a lambda function

import boto3
import pandas as pd
import pyarrow.parquet as pq
from io import BytesIO
from sqlalchemy import create_engine
import boto3
import os
import pyodbc
import json
import awswrangler as wr
from botocore.exceptions import ClientError

def get_secret():
# we will create the connectionString below and use it in the process
global connectionString
server=os.environ.get('Server') # get the details from environment variables
db=os.environ.get('db') # get the details from environment variables
secret_name =os.environ.get('secret_name') # get the details from environment variables
region_name =os.environ.get('region_name') # get the details from environment variables

# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager', #don't change this

get_secret_value_response = client.get_secret_value(
except ClientError as e:
raise e

# Decrypts secret using the associated KMS key.
username = response.get('username') #change this variable according to the value you provided during secret creation
password = response.get('password')
connectionString= f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={db};UID={username};PWD={password};Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=300;'

def s3_to_df(bucket,keys):
s3Client = boto3.client('s3')
file_objects = s3Client.list_objects_v2(Bucket=bucket, Prefix=keys)['Contents']
for file_object in file_objects:
file_key = file_object['Key']
file_obj = s3Client.get_object(Bucket=bucket, Key=file_key)
parquet_file = pq.ParquetFile(BytesIO(file_obj['Body'].read()))
df = parquet_file.read().to_pandas()
return df

def lambda_handler(event, context):
#get our bucket and file name
bucket = event['Records'][0]['s3']['bucket']['name']

print(f"this is the file name {keys}")

# df = s3_to_df(bucket=bucket,keys=keys)
conn = pyodbc.connect(connectionString)
conn.execute(f"USE {db};")
#you can change the engine connection string to connect to other RDS as well.
engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(connectionString))
s3Client = boto3.client('s3')
file_objects = s3Client.list_objects_v2(Bucket=bucket, Prefix=keys)['Contents']

for file_object in file_objects:
file_key = file_object['Key']
#there are two ways we can read the file.
# 1. use awsdatawrangler. This can help read the files in a single go
# 2. read bigger files in chunks and upload them in every batch

#we can also pass chunked=True, so it will do it automatically or provide a number
dfs=wr.s3.read_parquet(path=[f"s3://{bucket}/{file_key}"], chunked=20_000)

for df in dfs:
df.to_sql('table_name',engine, if_exists='append', index=False)

file_obj = s3Client.get_object(Bucket=bucket, Key=file_key)
parquet_file = pq.ParquetFile(BytesIO(file_obj['Body'].read()))
# you can change the number 100000 with your number of choice to batch them appropriately
for idx,batch in enumerate(parquet_file.iter_batches(100000)):
batch_df = batch.to_pandas()
rows,columns =batch_df.shape
#batch is of 65536 records
#you can further loop this by using the rows variable above, so you can load fewer rows
batch_df.to_sql('table_name',engine, if_exists='append', index=False)

For this to execute we have to create layers.

These are the layers you need. Use the python3.9 runtime

  1. AWSSDKPandas-Python39 (this is available in AWS. This contains numpy and pandas)
  2. sqlalchemy
  3. pyodbc

To create a layer, follow the below steps

  1. create a folder in your local machine. If you are using ubuntu(linux), you can use these directly. If you are using windows, use WSL for linux capabilities to create these.
  2. removing the unnecessary folders reduce the size of layers. AWS has limitation on the size of the layers.
  3. If the layer is >10MB, load it to S3 and upload it to create a layer.
pip3 install sqlalchemy==2.0.21 --target python/
find . -name "tests" -type d | xargs -I{} rm -rf {}
find . -name "__pycache__" -type d | xargs -I{} rm -rf {}
find . -name "docs" -type d | xargs -I{} rm -rf {}
rm -rf boto*~
zip -r sqlalchemy_layer.zip python

Once the layers are ready, upload them to AWS lambda and you can run the lambda function.

To test the lambda function, you can either use a trigger, or create a test event.

Below is the process of creating a test event.

  1. Open AWS Lambda
  2. Click on the function you created
  3. In the code tab, click on down arrow just beside “Test” button
  4. Click on configure test event
  5. Click on create new event
  6. From template select appropriate trigger you need.
  7. Change the values in the json.
  8. save the event and trigger the lambda

You can call Lambda function from API. All you need to do is add a trigger to lambda function and select API gateway. Once you select API gateway, select HTTP and click okay.

Immediately you will get a link for the API gateway. You can call that API from your local machine(as long as you are in the same network) and trigger the API. You can call the API parallelly as well to get the respone. Below code can help create parallel threads

import requests
import asyncio
from timeit import default_timer
from concurrent.futures import ThreadPoolExecutor
import nest_asyncio
nest_asyncio.apply() #this suppresses event loop already started error
import json

START_TIME = default_timer()

def request(session, data,idx):
url = "https://xxxxxxx.amazonaws.com/default/lambda-function-name"
with session.post(url,data=data,headers = {
'Content-Type': 'text/plain'
}) as response:
output = response.text

if response.status_code != 200:

elapsed_time = default_timer() - START_TIME
completed_at = "{:5.2f}s".format(elapsed_time)
print("{0:<30} {1:>20}".format(idx, completed_at))
return output

async def start_async_process():
print("{0:<30} {1:>20}".format("No", "Completed at"))
with ThreadPoolExecutor(max_workers=10) as executor:
with requests.Session() as session:
loop = asyncio.get_event_loop()
START_TIME = default_timer()
tasks = [
"id": idx,
"file_name": i.get('file_name'),
"database_name": i.get('database_name')
for idx,i in enumerate([{'file_name': 'sample-1.parquet',
'table_name': 'sample',
'database_name': 'test'},
{'file_name': 'sample-2.parquet',
'table_name': 'sample',
'database_name': 'test'}])
for response in await asyncio.gather(*tasks):

if __name__ == "__main__":
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(start_async_process())



pathanjali tallapragada

SQL-DB Architect, Data Scientist, Learning full-stack development