首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >+ API网关V2:无法部署API,因为此API中没有路由

+ API网关V2:无法部署API,因为此API中没有路由
EN

Stack Overflow用户
提问于 2022-10-05 11:31:29
回答 1查看 168关注 0票数 2

我正在尝试使用CloudFormation创建一个API + Lambda集成,但是我仍然坚持这个错误:

无法部署API,因为该API中没有路由(服务: AmazonApiGatewayV2;状态代码: 400;错误代码: BadRequestException;请求ID: f606986f-d3e6-4dfd in 20-77011b15a3f9;代理: null)

下面是我的CloudFormation模板:

代码语言:javascript
复制
AWSTemplateFormatVersion: 2010-09-09

Resources:

  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      Policies:
        - PolicyName: LambdaPolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Action:
                  - 'logs:CreateLogGroup'
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource:
                  - 'arn:aws:logs:*:*:*'
                Effect: Allow
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Action:
              - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com

  Lambda:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: 'getFruit'
      Role: !GetAtt LambdaRole.Arn
      Handler: index.handler
      Runtime: nodejs16.x
      MemorySize: 128
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            const response = {
              body: JSON.stringify([
                { id: 1, name: 'banana', price: 1 },
                { id: 2, name: 'pineapple', price: 2 },
              ]),
              statusCode: 200
            }
            return response
          }

  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref Lambda
      Action: "lambda:InvokeFunction"
      Principal: apigateway.amazonaws.com

  LambdaIntegration:
    Type: AWS::ApiGatewayV2::Integration
    Properties:
      ApiId: !Ref MyApiGateway
      Description: Lambda proxy integration
      IntegrationType: AWS_PROXY
      IntegrationMethod: POST
      PayloadFormatVersion: "2.0"
      IntegrationUri: !Sub 'arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Lambda.Arn}/invocations'

  MyApiGateway:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      Name: "MyApiGateway"
      ProtocolType: "HTTP"

  MyApiGatewayStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      AutoDeploy: true
      DeploymentId: !Ref MyApiGatewayDeployment
      StageName: '$default'
      ApiId: !Ref MyApiGateway
  
  MyApiGatewayDeployment:
    Type: AWS::ApiGatewayV2::Deployment
    Properties:
      ApiId: !Ref MyApiGateway

  MyApiRoute:
    Type: AWS::ApiGatewayV2::Route
    Properties:
      ApiId: !Ref MyApiGateway
      RouteKey: "GET /"
      AuthorizationType: NONE
      Target: !Join
        - /
        - - integrations
          - !Ref LambdaIntegration
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-05 12:23:18

尝试将DependsOn属性添加到您创建的路由的部署中。

代码语言:javascript
复制
  MyApiGatewayDeployment:
    Type: AWS::ApiGatewayV2::Deployment
    DependsOn:
      - MyApiRoute
    Properties:
      ApiId: !Ref MyApiGateway
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73959735

复制
相关文章

相似问题

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