Reputation: 85
I am using AWS Lambda to write data every 5-mins to a bigquery table and google sheets. I an triggering the Lambda function through EventsBridge - schedule based rule / cron for every 5 mins. This is working fine but in some instances it appears that AWS Lambda is getting executed multiple times and writes data for same batch time twice or thrice. I could link this to Lambda execution time plot from Cloudwatch - whenever there is spike in execution time, I am getting duplicate data set in my big query. Multiple entries appear related to init timeout. I see this line in the logs for multiple entry batches:
INIT_REPORT Init Duration: 9995.80 ms Phase: init Status: timeout.
I had set overall timeout of 6 mins but the init timeout is happening within 10 secs.
Below is the python code snippet for the data write operation:
bq.writeToBigQueryTable('my-project', 'my-dataset', 'my-table', mydataframe)
updateGoogleSheet("my-sheet-id",mylist) #my function which calls Google Sheets API
Here is the graph for Lambda from cloudwatch: https://ibb.co/gWsRtfN
Would be grateful on any pointers on how to fix this.
Regards, dbeings
Upvotes: 0
Views: 126
Reputation: 10210
Take a look at the Lambda Init
phase in the documentation. It states that:
In the Init phase, Lambda performs three tasks:
- Start all extensions (
Extension init
)- Bootstrap the runtime (
Runtime init
)- Run the function's static code (
Function init
)- Run any
beforeCheckpoint
runtime hooks (Lambda SnapStart only)
and that:
The
Init
phase is limited to 10 seconds. If all three tasks do not complete within 10 seconds, Lambda retries theInit
phase at the time of the first function invocation with the configured function timeout.
If the code that writes the data lives outside of a Lambda handler (i.e. is a static code), it gets executed during the Init
phase. If it doesn't finish entirely in 10 seconds it gets retried which might lead to duplicate writes. In your case, it might finish writing the data to BigQuery but timeout during updating the Google Sheet.
Upvotes: 1