Intetics
Intetics

Reputation: 85

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;

Upvotes: 8

Views: 7399

Answers (2)

Connor
Connor

Reputation: 698

For me, the issue was that the deployment was updating the resources for the API, but not for the stage. The fix was to create a new deployment ID every time:

// Create deployment with ID based on current date
const deployment = new apigw.Deployment(this, 'deployment-' + new Date().toISOString(), { api });
  
// Deploy to existing API & stage  
const stage = new apigw.Stage(this, 'stage-alpha', { deployment, stageName: 'alpha' });
api.deploymentStage = stage

With the code you posted, you should see in the Stage > Deployment History tab that it's not adding new deployments until you give it a unique ID.

Note: This might not be ideal since it deploys updates every time you run cdk deploy, even if no other changes were made

Upvotes: 2

Luciano Fantone
Luciano Fantone

Reputation: 76

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';

Upvotes: 6

Related Questions