Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(apigateway): auto-deployment doesn't work #16324

Closed
rantoniuk opened this issue Sep 1, 2021 · 1 comment
Closed

(apigateway): auto-deployment doesn't work #16324

rantoniuk opened this issue Sep 1, 2021 · 1 comment
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. needs-triage This issue or PR still needs to be triaged.

Comments

@rantoniuk
Copy link

rantoniuk commented Sep 1, 2021

According to AWS CDK Documentation, all ApiGateway changes regarding adding new methods should be by default automatically deployed to the default prod stage.

This works fine for the case where the ApiGateway is managed in the same CDK stack that the methods/resources are being updated. It seems it doesn't work however when the ApiGateway is imported via Fn.importValue by Arn from another stack that is managing it.

In our case, we have the following micro-service repository layout:

  1. we have an infra repository that contains several CDK stacks, one per directory, that are self-isolated

The purpose of this repository is to hold all "shared" components of the infrastructure (like SQS, DynamoDB, ApiGW) to avoid dependency cycles between the components.
This way we know that the infra changes need to be deployed first if there is a new shared component that is added to the architecture stack.

  1. we have per-microservice repositories that have their CDK respective stacks, e.g. MicroserviceXStack, that only deploy Lambda code and rely on shared resources created by the infra stack.

Inside the CDK of that component, the API GW methods are added like this:

    const restApiId = Fn.importValue('restApiIdOutput');
    const rootId = Fn.importValue('parentIdOutput');

    const restApi = RestApi.fromRestApiAttributes(this, 'ApiGateway', {
      restApiId: restApiId,
      rootResourceId: rootId,
    });

...

 const createDocumentApiConstruct = new ApiConstruct(this, 'createDocument', {
      lambdaHandler: 'document-api-handler.create',
      layerVersion: nodeLayer,
      assetCode: lambdaCode,
      resource: documentResourceConstruct.resource,
      httpMethodName: 'POST',
      methodOptions: { apiKeyRequired: true },
    });

where the ApiContruct looks similarly to this:

export class ApiConstruct extends Construct {

  public method: Method;
  public fn: Function;

public constructor(scope: Construct, id: string, props: ApiConstructProps) {
    super(scope, id);

    const lambdaConstruct = new LambdaConstruct(this, `${id}-service`, {
      lambdaHandler: props.lambdaHandler,
      layerVersion: props.layerVersion,
      assetCode: props.assetCode,
      environment: props.environment,
      timeout: props.timeout,
    });

    this.fn = lambdaConstruct.lambdaFunction;

    let methodOptionsForConstruct: MethodOptions;

    const methodConstruct = new MethodConstruct(this, `${id}-method`, {
      lambdaFunction: this.fn,
      resource: props.resource,
      methodOptions: methodOptionsForConstruct,
      httpMethodName: props.httpMethodName,
      roles: props.roles,
    });

    this.method = methodConstruct.method;
  }

}

and MethodConstruct:

export class MethodConstruct extends Construct {

  public method: Method;

  public constructor(scope: Construct, id: string, props: MethodConstructProps) {
    super(scope, id);

    const lambdaIntegration = new LambdaIntegration(props.lambdaFunction, {
      proxy: true,
    });

    this.method = new Method(this, `${id}-method`, {
      httpMethod: props.httpMethodName,
      resource: props.resource,
      options: props.methodOptions,
      integration: lambdaIntegration,
    });
  }
}

What did you expect to happen?

API Gateway stage to be automatically deployed to /prod.

What actually happened?

Resources are visible in the ApiGW in the UI, but they are not deployed to /prod stage automatically.

Environment

CDK 1.119
CodePipelines

Do you have any suggestions how can we debug what is the reason this doesn't work like comparing the generated CF templates?


This is 🐛 Bug Report

@rantoniuk rantoniuk added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 1, 2021
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Sep 1, 2021
@nija-at
Copy link
Contributor

nija-at commented Sep 14, 2021

The auto-deployment story, unfortunately, is not true when used with fromRestApiAttributes().

We're tracking this gap #13526 and #12417. Upvote for prioritization.

As mentioned in the linked issue, you will need to create your own Deployment and salt its logical id whenever any resource changes. The Deployment should deploy at the end (after all the Resource and Methods are deployed).

@nija-at nija-at added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Sep 14, 2021
@github-actions github-actions bot added closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants