-
Notifications
You must be signed in to change notification settings - Fork 4k
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(cloudfront): REST API origin #20335
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/@aws-cdk/aws-cloudfront-origins/lib/private/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
/** | ||
* Throws an error if a duration is defined and not an integer number of seconds within a range. | ||
*/ | ||
export function validateSecondsInRangeOrUndefined(name: string, min: number, max: number, duration?: cdk.Duration) { | ||
if (duration === undefined) { return; } | ||
const value = duration.toSeconds(); | ||
if (!Number.isInteger(value) || value < min || value > max) { | ||
throw new Error(`${name}: Must be an int between ${min} and ${max} seconds (inclusive); received ${value}.`); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/@aws-cdk/aws-cloudfront-origins/lib/rest-api-origin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as apigateway from '@aws-cdk/aws-apigateway'; | ||
import * as cloudfront from '@aws-cdk/aws-cloudfront'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { validateSecondsInRangeOrUndefined } from './private/utils'; | ||
|
||
/** | ||
* Properties for an Origin for an API Gateway REST API. | ||
*/ | ||
export interface RestApiOriginProps extends cloudfront.OriginOptions { | ||
/** | ||
* Specifies how long, in seconds, CloudFront waits for a response from the origin, also known as the origin response timeout. | ||
* The valid range is from 1 to 180 seconds, inclusive. | ||
* | ||
* Note that values over 60 seconds are possible only after a limit increase request for the origin response timeout quota | ||
* has been approved in the target account; otherwise, values over 60 seconds will produce an error at deploy time. | ||
* | ||
* @default Duration.seconds(30) | ||
*/ | ||
readonly readTimeout?: cdk.Duration; | ||
|
||
/** | ||
* Specifies how long, in seconds, CloudFront persists its connection to the origin. | ||
* The valid range is from 1 to 180 seconds, inclusive. | ||
* | ||
* Note that values over 60 seconds are possible only after a limit increase request for the origin response timeout quota | ||
* has been approved in the target account; otherwise, values over 60 seconds will produce an error at deploy time. | ||
* | ||
* @default Duration.seconds(5) | ||
*/ | ||
readonly keepaliveTimeout?: cdk.Duration; | ||
} | ||
|
||
/** | ||
* An Origin for an API Gateway REST API. | ||
*/ | ||
export class RestApiOrigin extends cloudfront.OriginBase { | ||
|
||
constructor(restApi: apigateway.RestApi, private readonly props: RestApiOriginProps = {}) { | ||
// urlForPath() is of the form 'https://<rest-api-id>.execute-api.<region>.amazonaws.com/<stage>' | ||
// Splitting on '/' gives: ['https', '', '<rest-api-id>.execute-api.<region>.amazonaws.com', '<stage>'] | ||
// The element at index 2 is the domain name, the element at index 3 is the stage name | ||
super(cdk.Fn.select(2, cdk.Fn.split('/', restApi.url)), { | ||
originPath: `/${cdk.Fn.select(3, cdk.Fn.split('/', restApi.url))}`, | ||
...props, | ||
}); | ||
|
||
validateSecondsInRangeOrUndefined('readTimeout', 1, 180, props.readTimeout); | ||
validateSecondsInRangeOrUndefined('keepaliveTimeout', 1, 180, props.keepaliveTimeout); | ||
} | ||
|
||
protected renderCustomOriginConfig(): cloudfront.CfnDistribution.CustomOriginConfigProperty | undefined { | ||
return { | ||
originSslProtocols: [cloudfront.OriginSslPolicy.TLS_V1_2], | ||
originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY, | ||
originReadTimeout: this.props.readTimeout?.toSeconds(), | ||
originKeepaliveTimeout: this.props.keepaliveTimeout?.toSeconds(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/aws-cloudfront-origins/rosetta/default.ts-fixture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/@aws-cdk/aws-cloudfront-origins/test/integ.rest-api-origin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as apigateway from '@aws-cdk/aws-apigateway'; | ||
import * as cloudfront from '@aws-cdk/aws-cloudfront'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as origins from '../lib'; | ||
|
||
const app = new cdk.App(); | ||
|
||
const stack = new cdk.Stack(app, 'integ-cloudfront-rest-api-origin'); | ||
|
||
const api = new apigateway.RestApi(stack, 'RestApi', { endpointTypes: [apigateway.EndpointType.REGIONAL] }); | ||
api.root.addMethod('GET'); | ||
|
||
new cloudfront.Distribution(stack, 'Distribution', { | ||
defaultBehavior: { origin: new origins.RestApiOrigin(api) }, | ||
}); | ||
|
||
app.synth(); |
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/aws-cloudfront-origins/test/rest-api-origin.integ.snapshot/cdk.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":"18.0.0"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't
Fn.parseDomainName
be used here instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know about
Fn.parseDomainName
... but this version extracts the domain name with a single select and split...