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;
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.
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.
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.
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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With