amazon-web-services - 如何设置 - 方法响应 HTTP - 状态 : Proxy in APIGW via CFT

标签 amazon-web-services aws-lambda aws-cloudformation devops

在过去的几天里,我正在研究云形成模板,它将创建一个 API 网关,并在其后面连接一个 lambda,APIGW 将只有一个具有 ANY 方法的 {proxy+} 资源,我想处理所有后端 lambda 中的逻辑。

我能够通过控制台创建相同的内容:

enter image description here

现在我想通过云形成模板复制相同的内容 我当前的 CloudFormation 模板

AWSTemplateFormatVersion: 2010-09-09
Description: My API Gateway and Lambda function

Parameters:
  apiGatewayName:
    Type: String
    Default: proxy-apigw
  apiGatewayStageName:
    Type: String
    Default: v1
  apiGatewayHTTPMethod:
    Type: String
    Default: ANY
  lambdaFunctionName:
    Type: String
    AllowedPattern: "[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+"
    Default: proxy-lambda

Resources:
  apiGateway:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Description: Example API Gateway
      EndpointConfiguration:
        Types:
          - REGIONAL
      Name: !Ref apiGatewayName

  apiGatewayLambdaResource:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      RestApiId: !Ref apiGateway
      PathPart: '{proxy+}'
      ParentId: !GetAtt apiGateway.RootResourceId

  apiGatewayLambdaResourceMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      AuthorizationType: NONE
      RestApiId: !Ref apiGateway
      ResourceId: !Ref apiGatewayLambdaResource
      HttpMethod: ANY
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub 
          - >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
          - lambdaArn: !GetAtt lambdaFunction.Arn
      MethodResponses:
        - StatusCode: 200
          ResponseModels: { "application/json": "Empty" }

  apiGatewayDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn:
      - apiGatewayLambdaResourceMethod
    Properties:
      RestApiId: !Ref apiGateway
      StageName: !Ref apiGatewayStageName

  lambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          exports.handler = async (event) => {
              // TODO implement
              const response = {
                  statusCode: 200,
                  body: JSON.stringify(event),
              };
              return response;
          };
      Description: Example Lambda function
      FunctionName: !Ref lambdaFunctionName
      Handler: index.handler
      Role: !GetAtt lambdaIAMRole.Arn
      Runtime: nodejs12.x

  lambdaApiGatewayInvoke:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt lambdaFunction.Arn
      Principal: apigateway.amazonaws.com
      # note: if route *not* at API Gateway root, `SourceArn` would take the form of:
      #               arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/PATH_PART
      SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/

  lambdaIAMRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Action:
              - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
      Policies:
        - PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Effect: Allow
                Resource:
                  - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*
          PolicyName: lambda

  lambdaLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${lambdaFunctionName}
      RetentionInDays: 90

Outputs:
  apiGatewayInvokeURL:
    Value: !Sub https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/${apiGatewayStageName}

  lambdaArn:
    Value: !GetAtt lambdaFunction.Arn

这样我就能够在根目录下创建一个 {proxy+} 资源以及与/{proxy+} 资源关联的任何方法,但是当我调用 APIGW URL 时,我收到内部服务器错误

patelnab@3c22fb980312 ~ % curl --request GET https://nx9gwoz5de.execute-api.us-east-1.amazonaws.com/v1/test/route
{"message": "Internal server error"}%

这是APIGW的样子,是由Cloudformation模板创建的

enter image description here

我能够指出的一个区别是在方法响应中,我通过控制台创建的响应具有HTTP Status: Proxy,而它是通过CloudFormation创建的没有那个。我尝试在文档中搜索,但找不到太多相关信息。非常感谢对此的任何帮助

最佳答案

您的权限不正确。您缺少 /*:

  lambdaApiGatewayInvoke:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt lambdaFunction.Arn
      Principal: apigateway.amazonaws.com
      # note: if route *not* at API Gateway root, `SourceArn` would take the form of:
      #               arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/PATH_PART
      SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/*

如果你想填充MethodRequest,你可以这样做:

      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub
          - >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
          - lambdaArn: !GetAtt lambdaFunction.Arn
        IntegrationResponses:
          - ResponseTemplates:
              application/json: ""
            StatusCode: 200
        PassthroughBehavior: WHEN_NO_TEMPLATES

关于amazon-web-services - 如何设置 - 方法响应 HTTP - 状态 : Proxy in APIGW via CFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68920953/

相关文章:

node.js - MongoDB Node.js 驱动程序 4.0.0 : Cursor session id issues in production on Vercel

amazon-web-services - 使用 CloudFormation 创建 SpotFleet 时出错

aws-cloudformation - AWS CLI : Windows CLI -- SSLError: [Errno 2] No such file or directory

amazon-web-services - 作为源的数据目录表

amazon-web-services - Spot 实例终止时是否适用 ELB 连接耗尽?

aws-lambda - Lambda 支持的自定义资源 cf 模板返回 'CREATE_FAILED'

amazon-web-services - AWS - Cloudformation RDS 资源创建错误 - 数据库实例不稳定

powershell - 如何将文件从 s3 服务下载到本地文件夹

glassfish - 亚马逊 EC2 : How install glassfish in EC2?

java - Exec 任务的动态 gradle 命令失败