CDK将API网关堆栈拆分为2个小堆栈 [英] CDK split API Gateway stack into 2 small stacks

查看:122
本文介绍了CDK将API网关堆栈拆分为2个小堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建CDK堆栈以创建API网关.如果我以小块"的形式创建堆栈,则一切工作均会例外(注释部分资源),但是当我尝试创建完成的堆栈时,却遇到了这个异常:

I'm trying to create a CDK stack in order to create API Gateway. Everything working as excepted if I create the stack in "small pieces" (comment part of the resources), But when I'm trying to create the completed stack I'm getting this exception:

 Number of resources, 224, is greater than maximum allowed, 200

因此,我试图将我的大堆栈分成2个较小的堆栈,一个堆栈创建资源,并创建一半的资源,另一个堆栈填充相关数据.

Therefore, I tried to split my big stack into 2 smaller stacks, One stack creates the resource and creates half of the resources and the other one fills relevant data.

代码段:


const api = new apigateway.RestApi(this, 'ApiGWEndPoint', {
  restApiName: 'API_NAME,
  deployOptions: {
    stageName: 'STAGE_NAME',
  },
  description: "MyDescription",
  endpointTypes: [apigateway.EndpointType.REGIONAL]
});

我尝试创建cross-stacknested-stack并将API数据传递到其中,但到目前为止还算不上成功.

I tried to create cross-stack or nested-stack and pass the API data into it, but no luck so far.

我的目标是创建一个包含2个小堆栈的堆栈-它们都将指向相同的API. 或者,如果有可能,请为资源限制创建解决方法.

My goal is to create one stack which will contain 2 small stacks- both of them will point to the same API. Or, if it is possible, create a workaround for the resource limit.

任何帮助将不胜感激.

更新1.10.2020:

当前,此问题尚无解决方法,最终将API网关拆分为许多API网关.

Currently, there is no workaround for this issue, Ended up splitting the API Gateway Into many API Gateways.

推荐答案

这就是我们现在正在做的事情.我们基本上有多个共享相同API网关类(RestApi)的堆栈

This is how we are doing it right now. We basically have multiple stack that share the same API Gateway class (RestApi)

class MultipleStackConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // Main stack with shared components
    const commonStack = new CommonStack(
      scope,
      `common-stack`
    );

    // Module 1
    const moduleOneStack = new ModulOneStack(
      scope,
      `module-one`,
      {
        apiGatewayRestApi: commonStack.apiGatewayRestApi
      }
    );

    // Module 2, 3, etc.....
  }
}

此接口用于将道具传递到模块堆栈:

This interface is used to pass the props to module stack:

export interface CommonProps extends cdk.StackProps {
  apiGatewayRestApi: apigw.RestApi;
}

公共模块将创建API网关对象:

The common module will create the API Gateway object:

export class CommonStack extends cdk.Stack {
  public readonly apiGatewayRestApi: apigw.RestApi;

  constructor(scope: cdk.Construct, id: string, props?: CommonProps) {
    super(scope, id, props);

    /******** SETUP API ********/
    this.apiGatewayRestApi = new apigw.RestApi(this, "MyAPI", {
      // Options here
    });
}

因此,模块堆栈本身将是这样的:

So the module stack itself will be something like this:

export class ModuleOneStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: CommonProps) {
    super(scope, id, props);

    if (props && props.apiGatewayRestApi) {
      const apiGatewayRestApi = props.apiGatewayRestApi;

      // associate lambda with api gateway
    }
  }
}

在这种情况下,我们仅使用一个具有多个Lambda的API网关,这些Lambda分为多个堆栈,因为我们也遇到了限制问题.

In this case, we are using only one API Gateway with multiple Lambdas that are divided into multiple stack, because we've also encountered the limit problem.

AWS的一个文档正在使用VPC做同样的事情: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html#sharing-vpcs-between-stacks

There is a documentation from AWS that is doing the same thing using VPC: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html#sharing-vpcs-between-stacks

从我的评论中复制粘贴: https://github.com. com/aws/aws-cdk/issues/1477#issuecomment-568652807

Copy paste from my comment here: https://github.com/aws/aws-cdk/issues/1477#issuecomment-568652807

这篇关于CDK将API网关堆栈拆分为2个小堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆