how to attach an API gateway endpoint to an API gateway created in a different cdk stack

99 Views Asked by At

I have a main stack (shared resources) and several worker stacks. My main stack contains an api gateway resource, I pass this into the worker stacks that will attach a new endpoint to this resource for their associated functions.

here is a breakdown of the code:

main-stack.ts - creates api gateway resource

export class MainSlackBotStack extends cdk.Stack {
  public readonly basicAuthorizerFunction: apigateway.RequestAuthorizer;
  public readonly api: apigateway.RestApi;
  constructor(scope: Construct, id: string, props: MainSlackBotStackProps) {
    super(scope, id, props);
    // create api gateway 
    this.api = new apigateway.RestApi(this, 'MainSlackBotApi', {
      restApiName: 'MainSlackBotApi',
      description: 'This service is the api gateway for the Slack Bot',
      deployOptions: {
        stageName: "v1",
      },
      defaultCorsPreflightOptions: {
        allowHeaders: [
          "Content-Type",
          "X-Amz-Date",
          "Authorization",
          "X-Api-Key",
        ],
        allowMethods: ["OPTIONS", "POST"],
        allowCredentials: true,
        allowOrigins: ["*"],
      },
    });
  }
}

worker-stack.ts - passed api gateway & creates its own path/ POST request on it

// initialize main api
const mainApi = apigateway.RestApi.fromRestApiAttributes(this, `MainApi`, {
    restApiId: props.apiId,
    rootResourceId: props.apiRootResourceId,
});
// add api gateway endpoint for worker bot
const slackBotEndpoint = mainApi.root.addResource(`${props.namespace}`);
slackBotEndpoint.addMethod(
    "POST",
    new apigateway.LambdaIntegration(commandEntryFunction, {}),
    {
        methodResponses: [{
            statusCode: '200',
            responseParameters: {
                'method.response.header.Content-Type': true,
                'method.response.header.Access-Control-Allow-Origin': true,
            },
        }],
        authorizer: customAuthorizerFunction,
        authorizationType: apigateway.AuthorizationType.CUSTOM,
    },
);

So most things are behaving as expected and the API gateway is created successfully in the main stack, the POST requests are attached to the API gateway resource but they are not being attached at the API gateway stage.

As a result, I can't make any calls to the worker stack endpoint attachments against the deployed API gateway stage. The stage only displays the API root and associated options request that are defined in the main stack. I think I need to force a new deployment against the API gateway resource but I can't seem to find a solid way of going about that. Anyone encounter this before/ have a clean solution?

0

There are 0 best solutions below