【问题标题】:How to debug and run multiple lambdas locally如何在本地调试和运行多个 lambda
【发布时间】:2021-07-12 17:30:58
【问题描述】:

我想使用 aws lambdas 构建 .NET HTTP API。这些 lambda 将由 UI 和一些其他系统通过 api 网关调用。显然在本地环境中我想运行/调试这些。

我尝试过的:

a) 使用 AWS 附带的模拟工具 Visual Studio 模板。您可以调用单个 lambda,但我不知道如何调用它们,例如邮递员使用正常的休息电话。我不知道模拟工具是如何进行这些调用的,因为 chrome/firefox 没有显示它们。

b) 使用 sam local start-api。这是我所做的:

sam --version
SAM CLI, version 1.22.0
sam init (choose aws quick start template, package type Image and amazon/dotnet5.0-base as base image)

我可以用sam build 构建解决方案,用sam local start-api 运行它,我可以浏览到http://localhost:3000/hello 并且它可以工作。问题是我每次更改代码时都需要在 VS 中构建 + 执行这些步骤。也没有简单的方法来附加调试器。

那么推荐的方法是什么?我知道您可以在 lambda 中运行整个 .NET Web api,但这听起来不是一个好的技术解决方案。我假设我不是第一个使用 lambdas 构建 HTTP api 的人。

【问题讨论】:

    标签: aws-lambda .net-5 aws-sam-cli


    【解决方案1】:

    可能值得考虑在 Docker 中运行类似 lambda 的环境。

    虽然在实际 Lambda 中包含您需要的 dotnet 工具可能不可行,但将它们包含在 Docker 映像中或绑定安装到 docker 容器中可能是可行的。这些来自 lambci 的图片可以帮助解决这个问题:https://hub.docker.com/r/lambci/lambda/

    【讨论】:

      【解决方案2】:

      你可以使用 sam local

      使用 API 网关示例创建 API

                      Resources:
                        ApiGatewayToLambdaRole:
                          Type: AWS::IAM::Role
                          Properties:
                            AssumeRolePolicyDocument:
                              Statement:
                                - Action: ['sts:AssumeRole']
                                  Effect: Allow
                                  Principal:
                                    Service: ['apigateway.amazonaws.com']
                              Version: '2012-10-17'
                            ManagedPolicyArns:
                              - arn:aws:iam::aws:policy/service-role/AWSLambdaRole
                              - arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
      
                        ApiGateway:
                          Type: AWS::Serverless::Api
                          Properties:
                            StageName: test
                            EndpointConfiguration: REGIONAL
                            DefinitionBody:
                              swagger: "2.0"
                              info:
                                title: "TestAPI"
                                description: TestAPI description in Markdown.
                              paths:
                                /create:
                                  post:
                                    x-amazon-apigateway-integration:
                                      uri:
                                        !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambda.Arn}/invocations
                                      credentials: !GetAtt ApiGatewayToLambdaRole.Arn
                                      responses: {}
                                      httpMethod: POST
                                      type: aws
                              x-amazon-apigateway-request-validators:
                                Validate query string parameters and headers:
                                  validateRequestParameters: true
                                  validateRequestBody: false
      
                        LambdaRole:
                          Type: AWS::IAM::Role
                          Properties:
                            AssumeRolePolicyDocument:
                              Statement:
                                - Action: ['sts:AssumeRole']
                                  Effect: Allow
                                  Principal:
                                    Service: [lambda.amazonaws.com]
                              Version: '2012-10-17'
                            Path: /
                            Policies:
                              - PolicyName: CodeBuildAccess
                                PolicyDocument:
                                  Version: '2012-10-17'
                                  Statement:
                                  - Action:
                                    - logs:*
                                    - lambda:*
                                    - ec2:CreateNetworkInterface
                                    - ec2:DescribeNetworkInterfaces
                                    - ec2:DeleteNetworkInterface
                                    Effect: Allow
                                    Resource: "*"
                                  Version: '2012-10-17'
      
                        MyLambda:
                          Type: AWS::Serverless::Function
                          Properties:
                            Role: !GetAtt LambdaRole.Arn
                            Handler: myfunctionname.lambda_handler
                            CodeUri: ./src/myfunctionname
                            Events:
                              SCAPIGateway:
                                Type: Api
                                Properties:
                                  RestApiId: !Ref ApiGateway
                                  Path: /create
                                  Method: POST
                      ...
      

      构建:

      Time sam build --use-container --template backend/template.yam

      在本地调用 Lambda:

      本地调用Lambda的命令是sam local invoke,使用-e标志指定Lambda事件的路径。

              $ sam local invoke -e event.json
      

      当它运行时,它看起来像这样:

           $ sam local invoke MyLambda -e event.json
      
         2021-04-20 11:11:09 Invoking index.handler 
         2021-04-20 11:11:09 Found credentials in shared credentials file: 
         ~/.aws/credentials
      

      【讨论】:

      • 正如我所写,我使用 sam local 设置本地 api 网关并调用 apis 但至少使用 .NET 5 您需要运行 sam build 来构建容器并在那里安装我的 lambda。每次更改代码时,我都需要再次运行sam build。当我想改变一些东西并测试它时,这是一个非常慢的反馈循环。此外,我没有找到如何将调试器(例如来自 Visual Studio)附加到正在运行的代码的信息。
      • 我从没用过.net,可能是关注链接帮助stackoverflow.com/questions/47402044/…
      猜你喜欢
      • 2022-10-16
      • 2019-11-17
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多