-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
45 lines (36 loc) · 1.58 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import awswrangler as wr
import pandas as pd
import urllib.parse
import os
os_input_s3_cleansed_layer = os.environ['s3_cleansed_layer']
os_input_glue_catalog_db_name = os.environ['glue_catalog_db_name']
os_input_glue_catalog_table_name = os.environ['glue_catalog_table_name']
os_input_write_data_operation = os.environ['write_data_operation']
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
# Extract region from the file name
filename = key.split('/')[-1] # Get the file name from the key
region = filename[:2].lower() # Assuming the first two characters define the region
try:
# Creating DF from content
df_raw = wr.s3.read_json(f's3://{bucket}/{key}')
# Extract required columns:
df_step_1 = pd.json_normalize(df_raw['items'])
# Add region column
df_step_1['region_name'] = region
# Write to S3
wr_response = wr.s3.to_parquet(
df=df_step_1,
path=os_input_s3_cleansed_layer,
dataset=True,
database=os_input_glue_catalog_db_name,
table=os_input_glue_catalog_table_name,
mode=os_input_write_data_operation
)
return wr_response
except Exception as e:
print(e)
print(f'Error getting object {key} from bucket {bucket}. Make sure they exist and your bucket is in the same region as this function.')
raise e