Skip to content

Commit

Permalink
feat(cloudfront): Add connectionAttempts, connectionTimeout in origin…
Browse files Browse the repository at this point in the history
… configuration (#8573)

Closes #8572

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
civilizeddev authored Jul 8, 2020
1 parent 61e7094 commit 84b923f
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 4 deletions.
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

0 comments on commit 84b923f

Please sign in to comment.