The RestApi construct in @aws-cdk/aws-apigateway could be more useful for split stacks
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
The RestApi construct in @aws-cdk/aws-apigateway could be more useful for split stacks if it had relaxed validation when `deploy: false` is specified in its RestApiProps.
The split-stack technique (https://docs.aws.amazon.com/cdk/api/v1/docs/aws-apigateway-readme.html#breaking-up-methods-and-resources-across-stacks) is very useful when managing an API Gateway, but to use the split-stack technique with the RestApi construct, at least one method must be added to the RestApi in the stack where it is defined; however, in the split-stack technique, that stack usually doesn't define any actual methods, so the method becomes a throw-away.
For example, from the docs example
```typescript
const restApi = new RestApi(this, 'RestApi', {
deploy: false,
});
restApi.root.addMethod('ANY');
```
The method is being added to pass validation, here: https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-apigateway/lib/restapi.ts#L831-L840; however, validation is only critical if there is a deployment.
It would be nice if I could get the benefits of using the RestApi construct while using the split-stack technique without having to create a throw-away method in that root stack.
### Use Case
I use the split-stack technique in combination with a Serverless Framework project that defines the stage, where that Serverless Framework project imports the API Gateway Rest API (these stacks are not nested). This technique is documented by the Serverless Framework, here: https://www.serverless.com/framework/docs/providers/aws/events/apigateway#share-api-gateway-and-api-resources
I would like to use the CDK to manage the API Gateway Rest API, and other configuration at that layer, and I would like to use the RestApi construct instead of the CfnRestApi class to do so.
But I'm frustrated that I have a throw-away method defined and deployed that could contribute to stack drift once the real method definitions are applied by deploying the Serverless Framework project.
### Proposed Solution
At https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-apigateway/lib/restapi.ts#L831-L840
I think that the change could be from this current check
```typescript
/**
* Performs validation of the REST API.
*/
protected validate() {
if (this.methods.length === 0) {
return ["The REST API doesn't contain any methods"];
}
return [];
}
```
to a check that invalidates only if there is a deployment
```typescript
/**
* Performs validation of the REST API.
*/
protected validate() {
if (this.methods.length === 0 && this.latestDeployment !== undefined) {
return ["The REST API doesn't contain any methods"];
}
return [];
}
```
### Other Information
_No response_
### Acknowledgements
- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
1.155.0
### Environment details (OS name and version, etc.)
OS and version independent
Contributor guide
Research direction
Start in packages/@aws-cdk/aws-apigateway/lib/restapi.ts around the RestApi validate method at lines 831–840, and read how deploy and latestDeployment relate to validation. The work is done when an API with deploy: false can have no methods without validation failure, while an API with a deployment still rejects an empty method set.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100