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(cloudfront): Add connectionAttempts, connectionTimeout in origin configuration #8573

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/@aws-cdk/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,26 @@ new cloudfront.CloudFrontWebDistribution(stack, 'MyDistribution', {
//...
geoRestriction: GeoRestriction.whitelist('US', 'UK')
});
```
```

### Connection behaviors between CloudFront and your origin.

CloudFront provides you even more control over the connection behaviors between CloudFront and your origin. You can now configure the number of connection attempts CloudFront will make to your origin and the origin connection timeout for each attempt.

See [Origin Connection Attempts](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#origin-connection-attempts)

See [Origin Connection Timeout](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#origin-connection-timeout)

Example usage:

```ts
const distribution = new CloudFrontWebDistribution(this, 'MyDistribution', {
originConfigs: [
{
...,
connectionAttempts: 3,
connectionTimeout: cdk.Duration.seconds(10),
}
]
});
```
34 changes: 31 additions & 3 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,31 @@ export interface LoggingConfiguration {
* One or the other must be passed, and it is invalid to pass both in the same SourceConfiguration.
*/
export interface SourceConfiguration {
/**
* The number of times that CloudFront attempts to connect to the origin.
* You can specify 1, 2, or 3 as the number of attempts.
*
* @default 3
*/
readonly connectionAttempts?: number;

/**
* The number of seconds that CloudFront waits when trying to establish a connection to the origin.
* You can specify a number of seconds between 1 and 10 (inclusive).
*
* @default cdk.Duration.seconds(10)
*/
readonly connectionTimeout?: cdk.Duration;

/**
* An s3 origin source - if you're using s3 for your assets
*/
readonly s3OriginSource?: S3OriginConfig
readonly s3OriginSource?: S3OriginConfig;

/**
* A custom origin source - for all non-s3 sources.
*/
readonly customOriginSource?: CustomOriginConfig,
readonly customOriginSource?: CustomOriginConfig;

/**
* The behaviors associated with this source.
Expand All @@ -161,7 +177,7 @@ export interface SourceConfiguration {
*
* @default /
*/
readonly originPath?: string,
readonly originPath?: string;

/**
* Any additional headers to pass to the origin
Expand Down Expand Up @@ -771,6 +787,16 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
}
}

const connectionAttempts = originConfig.connectionAttempts ?? 3;
if (connectionAttempts < 1 || 3 < connectionAttempts || !Number.isInteger(connectionAttempts)) {
throw new Error('connectionAttempts: You can specify 1, 2, or 3 as the number of attempts.');
}

const connectionTimeout = (originConfig.connectionTimeout || cdk.Duration.seconds(10)).toSeconds();
if (connectionTimeout < 1 || 10 < connectionTimeout || !Number.isInteger(connectionTimeout)) {
throw new Error('connectionTimeout: You can specify a number of seconds between 1 and 10 (inclusive).');
}

const originProperty: CfnDistribution.OriginProperty = {
id: originId,
domainName: originConfig.s3OriginSource
Expand All @@ -791,6 +817,8 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
originSslProtocols: originConfig.customOriginSource.allowedOriginSSLVersions || [OriginSslPolicy.TLS_V1_2],
}
: undefined,
connectionAttempts,
connectionTimeout,
};

for (const behavior of originConfig.behaviors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
},
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down Expand Up @@ -114,6 +116,8 @@
},
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"IPV6Enabled": false,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"IPV6Enabled": true,
"Origins": [
{
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
Expand Down
Loading