Skip to content

Commit

Permalink
feat(apigatewayv2): support for setting routeSelectionExpression fo…
Browse files Browse the repository at this point in the history
…r an HTTP API (aws#31373)

### Issue # (if applicable)

Closes aws#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*
  • Loading branch information
badmintoncryer authored Sep 13, 2024
1 parent e5d846f commit 36baf51
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 11 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"Type": "AWS::ApiGatewayV2::Api",
"Properties": {
"Name": "HttpApi",
"ProtocolType": "HTTP"
"ProtocolType": "HTTP",
"RouteSelectionExpression": "${request.method} ${request.path}"
}
},
"HttpApiDefaultStage3EEB07D6": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as apigw from 'aws-cdk-lib/aws-apigatewayv2';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-cdk-aws-apigatewayv2');

new apigw.HttpApi(stack, 'HttpApi');
new apigw.HttpApi(stack, 'HttpApi', {
routeSelectionExpression: true,
});

new IntegTest(app, 'http-api', {
testCases: [stack],
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-apigatewayv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ new apigwv2.HttpApi(this, 'HttpProxyApi', {
});
```

The `routeSelectionExpression` option allows configuring the HTTP API to accept only `${request.method} ${request.path}`. Setting it to `true` automatically applies this value.

```ts
new apigwv2.HttpApi(this, 'HttpProxyApi', {
routeSelectionExpression: true,
});
```

### Cross Origin Resource Sharing (CORS)

[Cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is a browser security
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-apigatewayv2/lib/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ export interface HttpApiProps {
* @default - no default authorization scopes
*/
readonly defaultAuthorizationScopes?: string[];

/**
* Whether to set the default route selection expression for the API.
*
* When enabled, "${request.method} ${request.path}" is set as the default route selection expression.
*
* @default false
*/
readonly routeSelectionExpression?: boolean;
}

/**
Expand Down Expand Up @@ -434,6 +443,7 @@ export class HttpApi extends HttpApiBase {
corsConfiguration,
description: props?.description,
disableExecuteApiEndpoint: this.disableExecuteApiEndpoint,
routeSelectionExpression: props?.routeSelectionExpression ? '${request.method} ${request.path}' : undefined,
};

const resource = new CfnApi(this, 'Resource', apiProps);
Expand Down
26 changes: 26 additions & 0 deletions packages/aws-cdk-lib/aws-apigatewayv2/test/http/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,32 @@ describe('HttpApi', () => {
});
});

test('routeSelectionExpression is enabled', () => {
const stack = new Stack();
new HttpApi(stack, 'api', {
routeSelectionExpression: true,
});

Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Api', {
Name: 'api',
ProtocolType: 'HTTP',
RouteSelectionExpression: '${request.method} ${request.path}',
});
});

test.each([false, undefined])('routeSelectionExpression is not enabled', (routeSelectionExpression) => {
const stack = new Stack();
new HttpApi(stack, 'api', {
routeSelectionExpression,
});

Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Api', {
Name: 'api',
ProtocolType: 'HTTP',
RouteSelectionExpression: Match.absent(),
});
});

test('can add a vpc links', () => {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit 36baf51

Please sign in to comment.