Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 763
Intetics Avatar asked Sep 18 '20 06:09

Intetics


People also ask

How do I use stage variables in API gateway?

To declare stage variables using the API Gateway console Create an API, create a GET method on the API's root resource, if you have not already done so. Set the HTTP Endpoint URL value as " http://${stageVariables.url} ", and then choose Save. Choose Deploy API. Choose New Stage and enter " beta " for Stage name.

What is deployment stage in API gateway?

A stage is a named reference to a deployment, which is a snapshot of the API. You use a Stage to manage and optimize a particular deployment. For example, you can configure stage settings to enable caching, customize request throttling, configure logging, define stage variables, or attach a canary release for testing.

What are the correct steps to deploy an API gateway in Amazon?

Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway . In the APIs navigation pane, choose the API you want to deploy. In the Resources navigation pane, choose Actions. From the Actions drop-down menu, choose Deploy API.

What are API gateway stage variables?

Stage variables are name-value pairs that you can define as configuration attributes associated with a deployment stage of a REST API. They act like environment variables and can be used in your API setup and mapping templates.


1 Answers

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';
like image 189
Luciano Fantone Avatar answered Sep 19 '22 03:09

Luciano Fantone