简体   繁体   English

使用 IAM 角色权限在本地运行 lambda python 代码

[英]Run lambda python code in local with IAM role permission

I tried to figure out the way to debug lambda code in python locally.我试图找出在本地调试 python 中的 lambda 代码的方法。 Currently, using serverless framework and Visual Studio Code IDE.目前,使用无服务器框架和 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. lambda 代码在 AWS lambda 远程控制台中运行良好,但如何在需要调试时使用附加的 lambda 执行角色在本地测试 python 代码。 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. lambda 代码使用 boto3 访问 AWS 资源,并使用带有 python 包的 lambda 层。

Some suggestions with minimum modification and code change?一些修改和代码更改最少的建议? Serverless invoke local?无服务器调用本地?

For quick debugging, I suggest using sls invoke local .为了快速调试,我建议使用sls invoke local This will not use the AWS Lambda role, but will use AWS credentials if they are available in your env.这不会使用 AWS Lambda 角色,但会使用 AWS 凭证(如果它们在您的环境中可用)。

If I'm having trouble with AWS roles or permissions, I suggest using sls invoke .如果我在使用 AWS 角色或权限时遇到问题,我建议使用sls invoke

You can pass a JSON body to both sls invoke and sls invoke local :您可以将 JSON 正文传递给sls invokesls 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为不同的 AWS 服务创建各种主体:

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然后将其输出通过管道sls invokesls invoke的输入中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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