首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS API网关无法执行lambda?

问AWS API网关无法执行lambda?
EN

Stack Overflow用户
提问于 2019-02-01 02:59:56
回答 1查看 1.2K关注 0票数 3

因此,在AWS中,我有一个可以直接从控制台执行的lambda。然而,当我执行API网关时,我得到了这个错误。

代码语言:javascript
复制
{
  "message": "Internal server error"
}


Execution log for request 799250bf-2589-11e9-8e14-6396967e56cf
Thu Jan 31 18:53:19 UTC 2019 : Starting execution for request: 799250bf-2589-11e9-8e14-6396967e56cf
Thu Jan 31 18:53:19 UTC 2019 : HTTP Method: GET, Resource Path: /ComputePi
Thu Jan 31 18:53:19 UTC 2019 : Method request path: {}
Thu Jan 31 18:53:19 UTC 2019 : Method request query string: {}
Thu Jan 31 18:53:19 UTC 2019 : Method request headers: {}
Thu Jan 31 18:53:19 UTC 2019 : Method request body before transformations: 
Thu Jan 31 18:53:19 UTC 2019 : Execution failed due to configuration error: API Gateway does not have permission to assume the provided role arn:aws:iam::061753407487:role/cloudformation-lambda-execution-role
Thu Jan 31 18:53:19 UTC 2019 : Method completed with status: 500

我更新了我的IAM角色以获得访问权限,但仍然不起作用?看起来它需要在云形成本身完成,但不确定在哪里?

这是我的SAM文件:

代码语言:javascript
复制
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs Pi
Resources:
  ComputePi:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      CodeUri: ./lambdaCode
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /ComputePi
            Method: GET

下面是我的构建规范:

代码语言:javascript
复制
version: 0.2
phases:
  install:
    commands:
      - aws cloudformation package --template-file samTemplate.yaml --kms-key-id eee5fba0-67fe-4def-b0be-7bb5d9ef38ef --s3-bucket codepipeline-us-east-2-588194207253 --output-template-file outputSamTemplate.yaml
artifacts:
  type: zip
  files:
    - samTemplate.yaml
    - outputSamTemplate.yaml

更新:

我已经更新了我的samTemplate,使其看起来像这样。我仍然收到一个错误。

代码语言:javascript
复制
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs Pi
Resources:
  ComputePi:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      CodeUri: ./lambdaCode
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /ComputePi
            Method: GET
  LambdaPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !Ref ComputePi
      Principal: apigateway.amazonaws.com
      SourceArn:
        Fn::Join:
          - ''
          - - 'arn:aws:execute-api:'
          - Ref: AWS::Region
          - ":"
          - Ref: AWS::AccountId
          - ":"
          - Ref: API
          - "/*/*/*"

错误:

代码语言:javascript
复制
Execution log for request 0e2aa0c7-25ba-11e9-9f42-2583dd87218f
Fri Feb 01 00:41:04 UTC 2019 : Starting execution for request: 0e2aa0c7-25ba-11e9-9f42-2583dd87218f
Fri Feb 01 00:41:04 UTC 2019 : HTTP Method: GET, Resource Path: /ComputePi
Fri Feb 01 00:41:04 UTC 2019 : Method request path: {}
Fri Feb 01 00:41:04 UTC 2019 : Method request query string: {}
Fri Feb 01 00:41:04 UTC 2019 : Method request headers: {}
Fri Feb 01 00:41:04 UTC 2019 : Method request body before transformations: 
Fri Feb 01 00:41:04 UTC 2019 : Execution failed due to configuration error: API Gateway does not have permission to assume the provided role arn:aws:iam::061753407487:role/cloudformation-lambda-execution-role
Fri Feb 01 00:41:04 UTC 2019 : Method completed with status: 500

更新:

我在添加了LambdaPermission、删除了堆栈,然后在我的lambda代码中更改了响应后,才让它工作。

代码语言:javascript
复制
let response = {
        "statusCode": 200,
        "headers": {},
        "body": pi * 4,
        "isBase64Encoded": false
    };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-01 03:24:43

您需要为API网关提供对lambda:InvokeFunction的访问权限。您可以将以下策略附加到模板:

代码语言:javascript
复制
LambdaPermission:
  Type: "AWS::Lambda::Permission"
  Properties:
    Action: lambda:InvokeFunction
    FunctionName: !Ref YourLambda
    Principal: apigateway.amazonaws.com    
    SourceArn:
      Fn::Join:
      - ''
      - - 'arn:aws:execute-api:'
        - Ref: AWS::Region
        - ":"
        - Ref: AWS::AccountId
        - ":"
        - Ref: YourAPI
        - "/*/*/*"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54467506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档