typescriptamazon-web-servicesaws-api-gatewayaws-cdk

How to use an existing stage in API Gateway deployments in AWS CDK?


I have an existing API Gateway with resources and stages. I'm adding a new resource to it via aws cdk. The Gateway is configured with deploy:false, so I have to manually create a new deployment for it. I can import the gateway, but I can't find a similar method (fromLookup?) in the Stage class. I know I can create a new stage but it doesn't sound like a scalable solution.

The code is below:

const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
  restApiId: 'XXX',
  rootResourceId: 'YYYY',
});

const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
  api,
});

// How to get an existing stage here instead of creating a new one?
const stage = new apigateway.Stage(this, 'test_stage', {
  deployment,
  stageName: 'dev',
});

api.deploymentStage = stage;

Solution

  • I was facing the same issue today but I discovered that if you set the stageName property for a deployment resource It will use the existing stage.

    If you check the CloudFormation documentation for the Deployment resource, it has a StageName property (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html).

    But if you check the Deployment implementation for CDK, it doesn't have support for the stageName property (https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-apigateway/lib/deployment.ts#L71) and by following the extends on the Deployment class, it ends on extending from a CfnResource that is expecting a stageName value in the constructor.

    So I end up forcing the Deployment resource to pick the value that I want by doing this:

    const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
      restApiId: 'XXX',
      rootResourceId: 'YYYY',
    });
    
    const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
      api,
    });
    
    deployment.resource.stageName = 'YourStageName';