Skip to content

Commit 36baf51

Browse files
feat(apigatewayv2): support for setting routeSelectionExpression for an HTTP API (#31373)
### Issue # (if applicable) Closes #31104. ### Reason for this change Cloudformation supports for configuring `routeSelectionExpression` but AWS CDK doesn't support this. ### Description of changes Added `routeSelectionExpression` prop to `HttpApiProps`. For HTTP API, `routeSelectionExpression` must be `${request.method} ${request.path}`. Therefore, I defined `routeSelectionExpression` as boolean and set it to `${request.method} ${request.path}`. ### Description of how you validated changes Added unit and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e5d846f commit 36baf51

File tree

11 files changed

+59
-11
lines changed

11 files changed

+59
-11
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/aws-cdk-aws-apigatewayv2.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/aws-cdk-aws-apigatewayv2.template.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"Type": "AWS::ApiGatewayV2::Api",
55
"Properties": {
66
"Name": "HttpApi",
7-
"ProtocolType": "HTTP"
7+
"ProtocolType": "HTTP",
8+
"RouteSelectionExpression": "${request.method} ${request.path}"
89
}
910
},
1011
"HttpApiDefaultStage3EEB07D6": {

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/httpapiDefaultTestDeployAssert77633A40.assets.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/manifest.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.js.snapshot/tree.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-apigatewayv2/test/http/integ.api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import * as apigw from 'aws-cdk-lib/aws-apigatewayv2';
66
const app = new cdk.App();
77
const stack = new cdk.Stack(app, 'aws-cdk-aws-apigatewayv2');
88

9-
new apigw.HttpApi(stack, 'HttpApi');
9+
new apigw.HttpApi(stack, 'HttpApi', {
10+
routeSelectionExpression: true,
11+
});
1012

1113
new IntegTest(app, 'http-api', {
1214
testCases: [stack],

packages/aws-cdk-lib/aws-apigatewayv2/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ new apigwv2.HttpApi(this, 'HttpProxyApi', {
9393
});
9494
```
9595

96+
The `routeSelectionExpression` option allows configuring the HTTP API to accept only `${request.method} ${request.path}`. Setting it to `true` automatically applies this value.
97+
98+
```ts
99+
new apigwv2.HttpApi(this, 'HttpProxyApi', {
100+
routeSelectionExpression: true,
101+
});
102+
```
103+
96104
### Cross Origin Resource Sharing (CORS)
97105

98106
[Cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is a browser security

packages/aws-cdk-lib/aws-apigatewayv2/lib/http/api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ export interface HttpApiProps {
160160
* @default - no default authorization scopes
161161
*/
162162
readonly defaultAuthorizationScopes?: string[];
163+
164+
/**
165+
* Whether to set the default route selection expression for the API.
166+
*
167+
* When enabled, "${request.method} ${request.path}" is set as the default route selection expression.
168+
*
169+
* @default false
170+
*/
171+
readonly routeSelectionExpression?: boolean;
163172
}
164173

165174
/**
@@ -434,6 +443,7 @@ export class HttpApi extends HttpApiBase {
434443
corsConfiguration,
435444
description: props?.description,
436445
disableExecuteApiEndpoint: this.disableExecuteApiEndpoint,
446+
routeSelectionExpression: props?.routeSelectionExpression ? '${request.method} ${request.path}' : undefined,
437447
};
438448

439449
const resource = new CfnApi(this, 'Resource', apiProps);

0 commit comments

Comments
 (0)