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
9 changes: 9 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,15 @@ new apigwv2.HttpApi(this, 'HttpProxyApi', {
});
```

The `routeSelectionExpression` option can be configured.
In the HTTP API, only the value `$request.method $request.path` is allowed, and it can be configured by enabling `routeSelectionExpression`.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a bit of redundancy in the first and second halves, could you simplify it a bit?


```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 === true ? '$request.method $request.path' : undefined,
badmintoncryer marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Why $request.method $request.path and not ${request.method} ${request.path}?

It is probably correct as the deployment has not failed. However, the CFn documentation also says it in brackets, and the selection expression guide says the following.

To use multiple variables in a selection expression, enclose the variable in brackets. For example, ${request.path.name} ${request.path.id}.

https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-parameter-mapping.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is my mistakes! Thank you for your confirmation!!

};

const resource = new CfnApi(this, 'Resource', apiProps);
Expand Down
13 changes: 13 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,19 @@ 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('can add a vpc links', () => {
// GIVEN
const stack = new Stack();
Expand Down