The problem of updating the apigateway stage in aws cdk.

0

I am using aws cdk using typescript. The aws-cdk version is being used with 1.134.0 fixed.

To explain the problem, I made an apigateway using RestApi in aws cdk. In the option value, deploy was set to true, and the meaning of this was explained as follows in the aws cdk document.

Indicates if a Deployment should be automatically created for this API, and recreated when the API model (resources, methods) changes.

It doesn't seem to work properly. The addition and modification of resources, methods seem to work well. However, even if RestApi's method is deleted, it is not deleted from the stage.

In addition, my cdk is a simple structure with only the creation of RestApi and the addition of methods through the resource.add method. Did I miss anything?

  • Could you please share a short snippet of your CDK code to illustrate the issue?

1 Answer
1

@awsmaker, did you define a Deployment in your CDK code?

The CDKv1 docs for Deployment indicate that a Deployment of a RestApi is immutable and changes to the RestApi model won't be reflected unless a new Deployment is created. (Using deploy: true when defining the RestApi lets the RestApi construct manage a restApi.latestDeployment deployment that it keeps up to date, but to take advantage of this you'll need to reference the latestDeployment instead of creating another Deployment)

The docs indicate that changing the logical ID of the Deployment resource to include some arbitrary data will invalidate the Deployment and cause it to be redeployed every time the CDK is deployed. The docs recommend using the addToLogicalId(data) method for this. I've also induced the same effect (with deploy: false by simply adding the current timestamp to the id param when initializing a new Deployment, like so:

    this.myApi = new apig.RestApi(this, 'MyApi', {
      deploy: false,
    });
    // force APIG deployment to be recreated every time even if no changes
    const prodDeploy = new apig.Deployment(this, 'MyApiProdDeployment' + new Date().toISOString(), {
      api: this.myApi,
      description: 'prod deployment',
    })
    this.myApiStage = new apig.Stage(this, 'MyApiProdStage', {
      deployment: prodDeploy,
      metricsEnabled: true,
      loggingLevel: apig.MethodLoggingLevel.INFO,
      stageName: this.STAGE_NAME,
    })
    this.myApi.deploymentStage = this.myApiStage;

Just be aware that modifying the logical ID of the Deployment will cause it to be re-created with every cdk deploy- this may not be desirable behavior in every situation.

AWS
evd
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions