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
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;
}
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
}
}
}
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.
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
Copy paste from my comment here: https://github.com/aws/aws-cdk/issues/1477#issuecomment-568652807