You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CloudFormation部署API Gateway V2+Lambda报错:无路由无法部署

解决API Gateway HTTP API部署时"无路由存在"的CloudFormation错误

错误原因

你的CloudFormation模板中,MyApiGatewayDeployment(部署资源)的创建顺序早于MyApiRoute(路由资源)。API Gateway在部署时需要已有路由配置,而部署执行时路由还未创建,因此抛出Unable to deploy API because no routes exist in this API错误。

修复方案

给MyApiGatewayDeployment添加DependsOn属性,明确依赖MyApiRoute和LambdaIntegration,确保部署操作在路由与集成都创建完成后再执行。同时,由于启用了AutoDeploy: true,可以简化MyApiGatewayStage的配置。

修改后的完整模板

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"

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

  MyApiGatewayDeployment:
    Type: AWS::ApiGatewayV2::Deployment
    Properties:
      ApiId: !Ref MyApiGateway
    DependsOn:
      - MyApiRoute
      - LambdaIntegration

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

关键修改点

  1. 添加DependsOn:给MyApiGatewayDeployment添加依赖声明,强制部署操作在路由和集成资源创建完成后执行
  2. 简化Stage配置:启用AutoDeploy后,无需手动指定DeploymentId,API Gateway会自动管理部署
  3. 调整资源顺序:将MyApiRoute移到MyApiGatewayDeployment之前,配合DependsOn进一步确保创建顺序

补充说明

CloudFormation默认按模板中的资源定义顺序创建资源,但当存在逻辑依赖时,必须显式声明DependsOn来强制顺序,否则会出现资源未就绪就执行后续操作的问题。

内容的提问来源于stack exchange,提问作者adl32x

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 10:20:43