Home > front end >  Is there any way to debug lambda function with s3 trigger locally?
Is there any way to debug lambda function with s3 trigger locally?

Time:01-26

I want to debug my application by breakpoints and this lambda function is triggered by uploading to s3 bucket. Is there any way to debug locally with breakpoints without making the request by hand ? As it is really slow to deploy and test by printing the values.

I am using python for this (but probably it does not matter)

CodePudding user response:

With the Serverless framework, you can test the s3 event locally with:

sls invoke local -f yourFunction -p ./testEvent.json

For that you need to create a mock s3 event and save to a file (in this case named testEvent.json). To create such a mock s3 event, you can customise the Amazon S3 Event example, to suite your needs. Or you can log your real s3 event from the deployed lambda function and copy it from there.

You then save the event to a file (for example testEvent.json) and invoke your function with the command above.

CodePudding user response:

Have you tried using the sam local generate-event command from SAM cli? The command below shows how to generate a JSON file that you can then pass to the local invoke command to trigger the local function.

sam local generate-event s3 put --bucket bucket-name --key key-name > event_file.json

Then you invoke your function with the payload:

sam local invoke function-name -e event_file.json
  •  Tags:  
  • Related