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

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

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-1
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": {

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+3-1
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],

Diff for: packages/aws-cdk-lib/aws-apigatewayv2/README.md

+8
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

Diff for: packages/aws-cdk-lib/aws-apigatewayv2/lib/http/api.ts

+10
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);

Diff for: packages/aws-cdk-lib/aws-apigatewayv2/test/http/api.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ describe('HttpApi', () => {
234234
});
235235
});
236236

237+
test('routeSelectionExpression is enabled', () => {
238+
const stack = new Stack();
239+
new HttpApi(stack, 'api', {
240+
routeSelectionExpression: true,
241+
});
242+
243+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Api', {
244+
Name: 'api',
245+
ProtocolType: 'HTTP',
246+
RouteSelectionExpression: '${request.method} ${request.path}',
247+
});
248+
});
249+
250+
test.each([false, undefined])('routeSelectionExpression is not enabled', (routeSelectionExpression) => {
251+
const stack = new Stack();
252+
new HttpApi(stack, 'api', {
253+
routeSelectionExpression,
254+
});
255+
256+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Api', {
257+
Name: 'api',
258+
ProtocolType: 'HTTP',
259+
RouteSelectionExpression: Match.absent(),
260+
});
261+
});
262+
237263
test('can add a vpc links', () => {
238264
// GIVEN
239265
const stack = new Stack();

0 commit comments

Comments
 (0)