当前位置: 首页 > 知识库问答 >
问题:

如何调试AWS Api网关&Lambda的“AWS/APIGateway 5xxError”

夏侯自珍
2023-03-14

我有一个运行lambda函数的API网关资源。我正在使用从我的API网关生成的AWS SDK调用API网关资源。

下面是我的客户端的stacktrace中似乎相关的部分:

Caused by: com.amazonaws.mobileconnectors.apigateway.ApiClientException: {"message": "Internal server error"} (Service: DevnetcountableClient; Status Code: 500; Error Code: null; Request ID: 348e8f98-6f55-11e6-97f6-098c2caf220f)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.handleResponse(ApiClientHandler.java:255) at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:88)
    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
    at $Proxy1.accountCreatePost(Unknown Source)

现在查看AWS控制台,在我的Api网关仪表板中,我看到请求进入并导致“AWS/APIGateway5xxError”。然而,有0日志(我可以找到)。我的lambda函数似乎没有被调用,也没有显示lambda日志。

module.exports.createAccount = function(event, context, cb) {
    console.log('createAccount');
    console.log(event);
    console.log(context);
    console.log(cb);
    cb(null, {status: 'SUCCESS', message: 'I ran!'});
};
{
   "AWSTemplateFormatVersion":"2010-09-09",
   "Description":"The AWS CloudFormation template for this Serverless application",
   "Resources":{
      "ServerlessDeploymentBucket":{
         "Type":"AWS::S3::Bucket"
      },
      "IamRoleLambda":{
         "Type":"AWS::IAM::Role",
         "Properties":{
            "AssumeRolePolicyDocument":{
               "Version":"2012-10-17",
               "Statement":[
                  {
                     "Effect":"Allow",
                     "Principal":{
                        "Service":[
                           "lambda.amazonaws.com"
                        ]
                     },
                     "Action":[
                        "sts:AssumeRole"
                     ]
                  }
               ]
            },
            "Path":"/"
         }
      },
      "IamPolicyLambda":{
         "Type":"AWS::IAM::Policy",
         "Properties":{
            "PolicyName":"dev-coolsoftware-lambda",
            "PolicyDocument":{
               "Version":"2012-10-17",
               "Statement":[
                  {
                     "Effect":"Allow",
                     "Action":[
                        "logs:CreateLogGroup",
                        "logs:CreateLogStream",
                        "logs:PutLogEvents"
                     ],
                     "Resource":"arn:aws:logs:us-west-2:*:*"
                  }
               ]
            },
            "Roles":[
               {
                  "Ref":"IamRoleLambda"
               }
            ]
         }
      },
      "createAccount":{
         "Type":"AWS::Lambda::Function",
         "Properties":{
            "Code":{
               "S3Bucket":{
                  "Ref":"ServerlessDeploymentBucket"
               },
               "S3Key":"coolsoftware-1472853507538.zip"
            },
            "FunctionName":"coolsoftware-dev-createAccount",
            "Handler":"handler.createAccount",
            "MemorySize":128,
            "Role":{
               "Fn::GetAtt":[
                  "IamRoleLambda",
                  "Arn"
               ]
            },
            "Runtime":"nodejs4.3",
            "Timeout":30
         }
      },
      "RestApiApigEvent":{
         "Type":"AWS::ApiGateway::RestApi",
         "Properties":{
            "Name":"dev-coolsoftware"
         }
      },
      "ResourceApigEventCreateaccountAccount":{
         "Type":"AWS::ApiGateway::Resource",
         "Properties":{
            "ParentId":{
               "Fn::GetAtt":[
                  "RestApiApigEvent",
                  "RootResourceId"
               ]
            },
            "PathPart":"account",
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            }
         }
      },
      "PutMethodApigEventCreateaccountAccount":{
         "Type":"AWS::ApiGateway::Method",
         "Properties":{
            "AuthorizationType":"AWS_IAM",
            "HttpMethod":"PUT",
            "MethodResponses":[
               {
                  "ResponseModels":{
                     "application/json":"AccountCreationResponseModel"
                  },
                  "ResponseParameters":{

                  },
                  "StatusCode":"200"
               }
            ],
            "RequestParameters":{

            },
            "Integration":{
               "IntegrationHttpMethod":"POST",
               "Type":"AWS",
               "Uri":{
                  "Fn::Join":[
                     "",
                     [
                        "arn:aws:apigateway:",
                        {
                           "Ref":"AWS::Region"
                        },
                        ":lambda:path/2015-03-31/functions/",
                        {
                           "Fn::GetAtt":[
                              "createAccount",
                              "Arn"
                           ]
                        },
                        "/invocations"
                     ]
                  ]
               },
               "RequestTemplates":{
                  "application/json":"\n            #define( $loop )\n              {\n              #foreach($key in $map.keySet())\n                  \"$util.escapeJavaScript($key)\":\n                    \"$util.escapeJavaScript($map.get($key))\"\n                    #if( $foreach.hasNext ) , #end\n              #end\n              }\n            #end\n            {\n              \"body\": $input.json(\"$\"),\n              \"method\": \"$context.httpMethod\",\n              \"principalId\": \"$context.authorizer.principalId\",\n              \"stage\": \"$context.stage\",\n\n              #set( $map = $input.params().header )\n              \"headers\": $loop,\n\n              #set( $map = $input.params().querystring )\n              \"query\": $loop,\n\n              #set( $map = $input.params().path )\n              \"path\": $loop,\n\n              #set( $map = $context.identity )\n              \"identity\": $loop,\n\n              #set( $map = $stageVariables )\n              \"stageVariables\": $loop\n            }\n          "
               },
               "IntegrationResponses":[
                  {
                     "StatusCode":"200",
                     "ResponseParameters":{

                     },
                     "ResponseTemplates":{
                        "application/json":""
                     }
                  }
               ]
            },
            "ResourceId":{
               "Ref":"ResourceApigEventCreateaccountAccount"
            },
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            },
            "RequestModels":{
               "application/json":"AccountCreationRequestModel"
            }
         }
      },
      "DeploymentApigEvent1472853508283":{
         "Type":"AWS::ApiGateway::Deployment",
         "Properties":{
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            },
            "StageName":"dev"
         },
         "DependsOn":[
            "PutMethodApigEventCreateaccountAccount"
         ]
      },
      "createAccountApigPermission":{
         "Type":"AWS::Lambda::Permission",
         "Properties":{
            "FunctionName":{
               "Fn::GetAtt":[
                  "createAccount",
                  "Arn"
               ]
            },
            "Action":"lambda:InvokeFunction",
            "Principal":"apigateway.amazonaws.com"
         }
      },
      "DynamoDBTableAccounts":{
         "Type":"AWS::DynamoDB::Table",
         "DeletionPolicy":"Retain",
         "Properties":{
            "TableName":"dev-coolsoftware-accounts",
            "ProvisionedThroughput":{
               "ReadCapacityUnits":1,
               "WriteCapacityUnits":1
            },
            "AttributeDefinitions":[
               {
                  "AttributeName":"accountid",
                  "AttributeType":"S"
               }
            ],
            "KeySchema":[
               {
                  "AttributeName":"accountid",
                  "KeyType":"HASH"
               }
            ]
         }
      },
      "AccountCreationRequestModel":{
         "Type":"AWS::ApiGateway::Model",
         "Properties":{
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            },
            "ContentType":"application/json",
            "Description":"Schema for AccountCreationRequestModel",
            "Name":"AccountCreationRequestModel",
            "Schema":{
               "$schema":"http://json-schema.org/draft-04/schema#",
               "title":"AccountCreationRequestModel",
               "type":"object",
               "properties":{
                  "publickey":{
                     "type":"string"
                  },
                  "deviceid":{
                     "type":"string"
                  }
               }
            }
         }
      },
      "AccountCreationResponseModel":{
         "Type":"AWS::ApiGateway::Model",
         "Properties":{
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            },
            "ContentType":"application/json",
            "Description":"Schema for AccountCreationResponseModel",
            "Name":"AccountCreationResponseModel",
            "Schema":{
               "$schema":"http://json-schema.org/draft-04/schema#",
               "title":"AccountCreationResponseModel",
               "type":"object",
               "properties":{
                  "status":{
                     "type":"string"
                  },
                  "message":{
                     "type":"string"
                  }
               }
            }
         }
      },
      "FailureResponseModel":{
         "Type":"AWS::ApiGateway::Model",
         "Properties":{
            "RestApiId":{
               "Ref":"RestApiApigEvent"
            },
            "ContentType":"application/json",
            "Description":"Schema for FailureResponseModel",
            "Name":"FailureResponseModel",
            "Schema":{
               "$schema":"http://json-schema.org/draft-04/schema#",
               "title":"FailureResponseModel",
               "type":"object",
               "properties":{
                  "status":{
                     "type":"string"
                  },
                  "message":{
                     "type":"string"
                  }
               }
            }
         }
      }
   },
   "Outputs":{
      "ServerlessDeploymentBucketName":{
         "Value":{
            "Ref":"ServerlessDeploymentBucket"
         }
      },
      "Function1Arn":{
         "Description":"Lambda function info",
         "Value":{
            "Fn::GetAtt":[
               "createAccount",
               "Arn"
            ]
         }
      },
      "ServiceEndpoint":{
         "Description":"URL of the service endpoint",
         "Value":{
            "Fn::Join":[
               "",
               [
                  "https://",
                  {
                     "Ref":"RestApiApigEvent"
                  },
                  ".execute-api.us-west-2.amazonaws.com/dev"
               ]
            ]
         }
      }
   }
}

编辑3:再次更新cloudformation脚本--仍然无法工作。

共有1个答案

涂煌
2023-03-14

如何调试:

  1. 创建一个IAM角色,允许API网关将日志推送到CloudWatch。角色必须附加以下策略:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogGroups",
                "logs:DescribeLogStreams",
                "logs:PutLogEvents",
                "logs:GetLogEvents",
                "logs:FilterLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

使用以下信任策略:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
 类似资料:
  • 我们正在尝试从C#Windows服务调用AWS API网关来执行后台作业。哪个应该触发API网关定期初始化请求? 我们使用RestSharp调用APIendpoint,该类称为AwsAuthenticator,它继承自RestSharp.Authenticators.IAAuthenticator。但当我们调用API Gateway时,收到的错误是“我们计算的请求签名与您提供的签名不匹配。请检查您

  • 我的(非常简单的)代码看起来是这样的: 我将构建好的jar上传到Lambda,并通过创建一个测试事件在AWS控制台中进行测试。它返回正确的响应,正如预期的那样。 但是,我希望API网关调用这个Lambda。我既使用了Lambda代理集成,也定义了自己的主体映射模板。我似乎无法使它工作,我一直得到: 我确定这是一件很简单的事情,我只是错过了一些东西...

  • 我试图使用java脚本访问一个AWS api网关来调用一个带有传递头的lambda。通过邮递员测试效果很好。 我得到以下信息: 我在GET和OPTION方法上启用了CORS,但没有用。 我可以坦率地承认,我只是几乎不明白CORS的概念,我是否错过了一些非常明显的东西?

  • null 我想通过POSTMAN传递给API网关POST方法的请求体是 谢谢!

  • https://skjdfsdj.execute-api.us-east-1.amazonaws.com/test/name/name2 在下一个示例中,它将是prod: https://skjdfsdj.execute-api.us-east-1.amazonaws.com/prod/name/name2 你知道这会怎么做吗? 谢谢,本尼

  • 在我的项目中,我创建了一个py函数,用于检查和修改我的谷歌日历,如下所示: 当我在lambda上测试它时,一切都完成了,但是当我从lambda创建API时: 并对其进行测试,结果是: 2017年12月20日星期三13:35:58 UTC:由于配置错误,执行失败:Lambda代理响应格式错误2017年12月20日星期三13:35:58 UTC:方法已完成,状态为:502 提前谢谢