簡體   English   中英

AWS CDK 部署失敗:指定的 API 標識符無效

[英]AWS CDK Deployment fails: Invalid API identifier specified

我正在嘗試在 AWS API 網關中向我現有的 REST API 添加新的路由和集成。 我正在使用下面的代碼片段來實現它:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

export interface IApiGatewayIntegrationProps extends cdk.StackProps {
  /**
   * Application Name. Will be used to name all the resources
   */
  appName: string;

  /**
   * Route name to add the API Gateway Integration onto.
   * For example: setting `admin` for admin-api, the invocation url will be `${apiGatewayInvocationUrl}/admin`
   */
  apiPath: string;

  /**
   * REST API ID for an existing API
   */
  restApiId: string;

  /**
   * ID for the root resource in the API
   */
  restApiRootResourceId: string;

  /**
   * VPC Link ID
   */
  VpcLink: string;

  /**
   * URL for the Network Load Balancer (NLB)
   */
  NLBDns: string;

  /**
   * Listener port on the NLB
   */
  NLBPort: number;
}

export class CustomApiGatewayIntegration extends Construct {
  constructor(scope: Construct, id: string, props: IApiGatewayIntegrationProps) {
    super(scope, id);

    const api = cdk.aws_apigateway.RestApi.fromRestApiAttributes(scope, 'api', {
      restApiId: props.restApiId,
      rootResourceId: props.restApiRootResourceId,
    });

    const proxyIntegration = new cdk.aws_apigatewayv2.CfnIntegration(this, 'gateway-integration', {
      apiId: api.restApiId,
      connectionId: props.VpcLink,
      connectionType: 'VPC_LINK',
      description: 'API Integration',
      integrationMethod: 'ANY',
      integrationType: 'HTTP_PROXY',
      integrationUri: `http://${props.NLBDns}:${props.NLBPort}/${props.apiPath}/{proxy}`,
    });

    new cdk.aws_apigatewayv2.CfnRoute(this, 'gateway-route', {
      apiId: api.restApiId,
      routeKey: 'ANY somepath/{proxy+}',
      target: `integrations/${proxyIntegration.ref}`,
    });
  }
}

部署 CDK 堆棧后,我在終端中收到以下錯誤:

failed: Error: The stack named $STACK_NAME failed to deploy: UPDATE_ROLLBACK_COMPLETE: Invalid API identifier specified $AWS_ACCOUNT_ID:$REST_API_ID

終端中的錯誤消息

這是錯誤在 Cloudformation 控制台中的外觀:

Cloudformation 控制台中的錯誤消息

這里有趣的是,除了實際的 API ID 之外,錯誤消息還顯示了 AWS 賬戶 ID。 我該如何解決這個問題?

感謝您對此的任何幫助! 提前致謝

編輯 1: apigateway import表示如何導入 API 網關 class 方法。 AWS Cloudformation 有兩個資源組:

  • AWS::API網關
  • AWS::APIGatewayV2

兩者都有不同的能力。 在舊版本的 AWS CDK (v1.x) 中,您必須分別導入兩個資源組:

舊: import * as apigateway from '@aws-cdk/aws-api-gateway';

新: import * as apigatewayv2 from '@aws-cdk/aws-api-gatewayv2';

較新的 CDK 將所有內容整合在一起,可以簡單地寫成:

import * as cdk from 'aws-cdk-lib';

// Call to v1 Resource Group:
const api = new cdk.aws_apigateway.RestApi(...);

// Call to v2 Resources:
const apiv2 = new cdk.aws_apigatewayv2.CfnRestApi(...);

我檢查了 cloudformation UI Console 以跟蹤堆棧更新,發現 ApiGatewayV2 模塊生成的 Cfn 模板在 API Id 前面附加了 Account Id。

所以我停止使用它並繼續使用 API 網關的主 CDK 導入。 下面是有效的代碼片段。 我還必須為 API 網關階段部署操作一個私有變量。

// ...

export class CustomApiGatewayIntegration extends constructs.Construct {
  constructor(scope: constructs.Construct, id: string, props: IApiGatewayIntegrationProps) {
    super(scope, id);

    const api = cdk.aws_apigateway.RestApi.fromRestApiAttributes(scope, 'api', {
      restApiId: props.restApiId,
      rootResourceId: props.restApiRootResourceId,
    });

    const vpcLink = cdk.aws_apigateway.VpcLink.fromVpcLinkId(this, 'vpc-link', props.VpcLink);

    const gatewayResource = api.root.addResource(props.apiPath);

    const endpoint = `http://${props.NLBDns}:${props.NLBPort}/${props.apiPath}`;

    const proxyResource = gatewayResource.addProxy({
      anyMethod: true,
      defaultIntegration: new cdk.aws_apigateway.Integration({
        type: cdk.aws_apigateway.IntegrationType.HTTP_PROXY,
        integrationHttpMethod: 'ANY',
        uri: `${endpoint}/{proxy}`,
        options: {
          vpcLink,
          connectionType: cdk.aws_apigateway.ConnectionType.VPC_LINK,
        },
      })
    });

    const deployment = new cdk.aws_apigateway.Deployment(this, 'api-deployment-' + new Date().toISOString(), { api });
    
    // Private member manipulation
    (deployment as any).resource.stageName = 'api';

    // Forcing dependency of deployment on the `proxyResource`
    // for sequential deployment in cloudformation
    deployment.node.addDependency(proxyResource);

    new CfnOutput(this, 'ServiceEndpoint', {
      value: endpoint,
      description: `Endpoint for ${props.appName} microservice`,
      exportName: `${props.org}-${props.environment}-service-endpoint`
    });
  }
}

// ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM