首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS CDK部署失败:指定无效的API标识符

AWS CDK部署失败:指定无效的API标识符
EN

Stack Overflow用户
提问于 2022-09-15 09:38:12
回答 1查看 275关注 0票数 0

我试图在API网关中向我现有的REST添加一个新的路由和集成。我正在使用下面的代码片段来实现这一点:

代码语言:javascript
复制
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中错误的外观:

这里有趣的一点是,除了实际的API ID之外,错误消息还显示了AWS帐户ID。

感谢你在这方面的任何帮助!提前感谢

编辑1:apigateway import意味着如何导入API类方法。AWS Cloudformation有两个资源组:

  • AWS::APIGateway
  • 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将一切都结合在一起,可以简单地写成:

代码语言:javascript
复制
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(...);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-20 10:01:07

我检查了cloudformation控制台以跟踪堆栈更新,并发现由ApiGatewayV2模块生成的Cfn模板在API Id前面添加了帐户Id。

因此,我不再使用它,继续使用主要的。下面是起作用的代码片段。I还必须为API阶段部署操作一个私有变量.

代码语言:javascript
复制
// ...

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`
    });
  }
}

// ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73728815

复制
相关文章

相似问题

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