Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigatewayv2): support for setting routeSelectionExpression for an HTTP API #31373

Merged
merged 13 commits into from
Sep 13, 2024
Merged

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', () => {
Copy link
Contributor

@go-to-k go-to-k Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, it might be good to write a test for RouteSelectionExpression: Match.absent() when routeSelectionExpression is undefined (or false too?), as a test for the ternary operator, just in case.

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