Skip to content

Rest API gateway method not being associated with a deployment/stage

0

I have a public Rest API gateway. I've defined a method, but unfortunately it is not being attached to the stage and deployment appropriately.

When I look in the rest api gateway in the AWS console, I see the method listed under the 'prod' stage that was created by default, but not the stage I have created. Curling the prod endpoint succeeds, but curling the endpoint on the stage I created fails.

By googling ive seen folklore that says adding a depends on in CDK/cloudformation may fix this some reason, but that doesnt seem to help. Do you have any ideas?

// ...additional cdk setup above

const apiGatewayDeployment = new apigateway.Deployment(this, 'ApiGatewayDeployment', {
  api: restApiGateway
});


const stage = new apigateway.Stage(this, 'ExampleV1', {
  stageName: 'v1',
  deployment: apiGatewayDeployment,
});

const exampleResource = restApiGateway.root.addResource('exampleResource');

const getMethod = messageHistoryLegacyOrgUserResource.addMethod("GET", undefined, {
});

// is this necessary?  it doesnt seem to help
apiGatewayDeployment.node.addDependency(getMethod.resource);

// curl https://<id>.execute-api.us-east-1.amazonaws.com/v1/exampleResource  - fails
1 Answer
1
Accepted Answer

Hello,

If your manually-defined deployment resource's (e.g. apiGatewayDeployment) logical ID does not change — it will continue to represent the snapshot in time in which it was created. This means that any newly-added methods or resources will be attached to restApi.latestDeployment (enabled by default) or won't reflect unless a new deployment resource is created [1].

To replicate the behavior of restApi.latestDeployment for your manually-defined deployment:

  1. Use addToLogicalId() to modify the logical ID of ApiGatewayDeployment whenever stack changes are made:
  • For example, add apiGatewayDeployment.addToLogicalId(new Date().toISOString()) to your stack file [1]
  • Alternatively, you may include a timestamp during the deployment's creation: const apiGatewayDeployment = new apigateway.Deployment(this, 'ApiGatewayDeployment' + new Date().toISOString() [...] [2]
  1. As an extra/optional step (as you've already done) include node.addDependency(dep) — doing so will further replicate the provisioning behavior of restApi.latestDeployment for your manually-defined deployment.

Note: It's still necessary that the logical ID be augmented — as you've mentioned, calling node.addDependency() alone won't suffice.

Lastly, you may opt to set your REST API's deploy property to false [3] — this will prevent new methods/resources from being attached to restApi.latestDeployment (i.e. prod stage).

References:

[1] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.Deployment.html

[2] https://repost.aws/questions/QUAH1opLNSQVWFFnKpF-1ZTQ/the-problem-of-updating-the-apigateway-stage-in-aws-cdk#ANSPC52WQFR2SNKn1bj68vfw

[3] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RestApi.html#deploy

AWS
SUPPORT ENGINEER

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.