简体   繁体   中英

Run lambda python code in local with IAM role permission

I tried to figure out the way to debug lambda code in python locally. Currently, using serverless framework and Visual Studio Code IDE.

The lambda code is work well in AWS lambda remote console, but how can I test python code locally with attached lambda execution role whenever I need to debug. I do have the sample event and database credentials.

The lambda code is using boto3 to access AWS resources and using lambda layer with python packages.

Some suggestions with minimum modification and code change? Serverless invoke local?

For quick debugging, I suggest using sls invoke local . This will not use the AWS Lambda role, but will use AWS credentials if they are available in your env.

If I'm having trouble with AWS roles or permissions, I suggest using sls invoke .

You can pass a JSON body to both sls invoke and sls invoke local :

sls invoke -f myFunctionName -d '{"Records":[{"messageId":"059f36b4-87a3-44ab-83d2-661975830a7d","receiptHandle":"AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...","body":"{\"functionArn\": \"arn:aws:lambda:us-west-2:377024778620:function:my-function-name\"}","attributes":{"ApproximateReceiveCount":"1","SentTimestamp":"1545082649183","SenderId":"AIDAIENQZJOLO23YVJ4VO","ApproximateFirstReceiveTimestamp":"1545082649185"},"messageAttributes":{},"md5OfBody":"098f6bcd4621d373cade4e832627b4f6","eventSource":"aws:sqs","eventSourceARN":"arn:aws:sqs:us-east-2:123456789012:my-queue","awsRegion":"us-east-2"}]}'

I use sls generate-event to create various bodies for different AWS services:

sls generate-event -t aws:sqs -b '{"functionArn": "arn:aws:lambda:us-east-1:123456:function:ci-service-fakeError"}'

And then pipe the output of that into the input of sls invoke

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM