-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cloudfront): Use regional endpoint for S3 bucket origins
The regional endpoint has to be used for S3 bucket origins, otherwise CloudFront will receive an HTTP 302 response (redirecting to the regional endpoint), which it will cache. This will lead to users seeing the actual bucket endpoint, instead of it being hidden behind the CloudFront distribution.
- Loading branch information
1 parent
5ac6e77
commit 64c3c6b
Showing
8 changed files
with
240 additions
and
77 deletions.
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
134 changes: 67 additions & 67 deletions
134
packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-alias-target.expected.json
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 |
---|---|---|
@@ -1,78 +1,78 @@ | ||
{ | ||
"Resources": { | ||
"HostedZoneDB99F866": { | ||
"Type": "AWS::Route53::HostedZone", | ||
"Properties": { | ||
"Name": "test.public." | ||
} | ||
}, | ||
"HostedZoneAlias40D2E006": { | ||
"Type": "AWS::Route53::RecordSet", | ||
"Properties": { | ||
"Name": "_foo.test.public.", | ||
"Type": "A", | ||
"AliasTarget": { | ||
"DNSName": { | ||
"Fn::GetAtt": [ | ||
"MyDistributionCFDistributionDE147309", | ||
"DomainName" | ||
] | ||
}, | ||
"HostedZoneId": "Z2FDTNDATAQYW2" | ||
"Resources": { | ||
"HostedZoneDB99F866": { | ||
"Type": "AWS::Route53::HostedZone", | ||
"Properties": { | ||
"Name": "test.public." | ||
} | ||
}, | ||
"HostedZoneAlias40D2E006": { | ||
"Type": "AWS::Route53::RecordSet", | ||
"Properties": { | ||
"Name": "_foo.test.public.", | ||
"Type": "A", | ||
"AliasTarget": { | ||
"DNSName": { | ||
"Fn::GetAtt": [ | ||
"MyDistributionCFDistributionDE147309", | ||
"DomainName" | ||
] | ||
}, | ||
"HostedZoneId": { | ||
"Ref": "HostedZoneDB99F866" | ||
} | ||
"HostedZoneId": "Z2FDTNDATAQYW2" | ||
}, | ||
"HostedZoneId": { | ||
"Ref": "HostedZoneDB99F866" | ||
} | ||
}, | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket" | ||
}, | ||
"MyDistributionCFDistributionDE147309": { | ||
"Type": "AWS::CloudFront::Distribution", | ||
"Properties": { | ||
"DistributionConfig": { | ||
"CacheBehaviors": [], | ||
"DefaultCacheBehavior": { | ||
"AllowedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"CachedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"ForwardedValues": { | ||
"Cookies": { | ||
"Forward": "none" | ||
}, | ||
"QueryString": false | ||
} | ||
}, | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket" | ||
}, | ||
"MyDistributionCFDistributionDE147309": { | ||
"Type": "AWS::CloudFront::Distribution", | ||
"Properties": { | ||
"DistributionConfig": { | ||
"CacheBehaviors": [], | ||
"DefaultCacheBehavior": { | ||
"AllowedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"CachedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"ForwardedValues": { | ||
"Cookies": { | ||
"Forward": "none" | ||
}, | ||
"TargetOriginId": "origin1", | ||
"ViewerProtocolPolicy": "redirect-to-https" | ||
"QueryString": false | ||
}, | ||
"DefaultRootObject": "index.html", | ||
"Enabled": true, | ||
"HttpVersion": "http2", | ||
"IPV6Enabled": true, | ||
"Origins": [ | ||
{ | ||
"DomainName": { | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"DomainName" | ||
] | ||
}, | ||
"Id": "origin1", | ||
"S3OriginConfig": {} | ||
} | ||
], | ||
"PriceClass": "PriceClass_100", | ||
"ViewerCertificate": { | ||
"CloudFrontDefaultCertificate": true | ||
"TargetOriginId": "origin1", | ||
"ViewerProtocolPolicy": "redirect-to-https" | ||
}, | ||
"DefaultRootObject": "index.html", | ||
"Enabled": true, | ||
"HttpVersion": "http2", | ||
"IPV6Enabled": true, | ||
"Origins": [ | ||
{ | ||
"DomainName": { | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"RegionalDomainName" | ||
] | ||
}, | ||
"Id": "origin1", | ||
"S3OriginConfig": {} | ||
} | ||
], | ||
"PriceClass": "PriceClass_100", | ||
"ViewerCertificate": { | ||
"CloudFrontDefaultCertificate": true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
133 changes: 133 additions & 0 deletions
133
packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-s3.expected.json
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,133 @@ | ||
{ | ||
"Resources": { | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket" | ||
}, | ||
"BucketPolicyE9A3008A": { | ||
"Type": "AWS::S3::BucketPolicy", | ||
"Properties": { | ||
"Bucket": { | ||
"Ref": "Bucket83908E77" | ||
}, | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"s3:Get*", | ||
"s3:List*" | ||
], | ||
"Effect": "Allow", | ||
"Principal": { | ||
"CanonicalUser": { | ||
"Fn::GetAtt": [ | ||
"OAI", | ||
"S3CanonicalUserId" | ||
] | ||
} | ||
}, | ||
"Resource": [ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"OAI": { | ||
"Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity", | ||
"Properties": { | ||
"CloudFrontOriginAccessIdentityConfig": { | ||
"Comment": "Allows CloudFront to reach to the bucket!" | ||
} | ||
} | ||
}, | ||
"DistributionCFDistribution882A7313": { | ||
"Type": "AWS::CloudFront::Distribution", | ||
"Properties": { | ||
"DistributionConfig": { | ||
"CacheBehaviors": [], | ||
"DefaultCacheBehavior": { | ||
"AllowedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"CachedMethods": [ | ||
"GET", | ||
"HEAD" | ||
], | ||
"ForwardedValues": { | ||
"Cookies": { | ||
"Forward": "none" | ||
}, | ||
"QueryString": false | ||
}, | ||
"TargetOriginId": "origin1", | ||
"ViewerProtocolPolicy": "redirect-to-https" | ||
}, | ||
"DefaultRootObject": "index.html", | ||
"Enabled": true, | ||
"HttpVersion": "http2", | ||
"IPV6Enabled": true, | ||
"Origins": [ | ||
{ | ||
"DomainName": { | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"RegionalDomainName" | ||
] | ||
}, | ||
"Id": "origin1", | ||
"S3OriginConfig": { | ||
"OriginAccessIdentity": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"origin-access-identity/cloudfront/", | ||
{ | ||
"Ref": "OAI" | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"PriceClass": "PriceClass_100", | ||
"ViewerCertificate": { | ||
"CloudFrontDefaultCertificate": true | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"Outputs": { | ||
"DistributionDomainName": { | ||
"Value": { | ||
"Fn::GetAtt": [ | ||
"DistributionCFDistribution882A7313", | ||
"DomainName" | ||
] | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-s3.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,30 @@ | ||
import iam = require('@aws-cdk/aws-iam'); | ||
import s3 = require('@aws-cdk/aws-s3'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import cloudfront = require('../lib'); | ||
|
||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'integ-cloudfront-s3'); | ||
|
||
const bucket = new s3.Bucket(stack, 'Bucket', { removalPolicy: cdk.RemovalPolicy.Destroy }); | ||
const oai = new cloudfront.CfnCloudFrontOriginAccessIdentity(stack, 'OAI', { | ||
cloudFrontOriginAccessIdentityConfig: { | ||
comment: 'Allows CloudFront to reach to the bucket!', | ||
} | ||
}); | ||
const dist = new cloudfront.CloudFrontWebDistribution(stack, 'Distribution', { | ||
originConfigs: [{ | ||
behaviors: [{ isDefaultBehavior: true }], | ||
s3OriginSource: { | ||
s3BucketSource: bucket, | ||
originAccessIdentityId: oai.cloudFrontOriginAccessIdentityId, | ||
}, | ||
}] | ||
}); | ||
bucket.addToResourcePolicy(new iam.PolicyStatement() | ||
.allow() | ||
.addActions('s3:Get*', 's3:List*') | ||
.addResources(bucket.bucketArn, bucket.arnForObjects('*')) | ||
.addCanonicalUserPrincipal(oai.cloudFrontOriginAccessIdentityS3CanonicalUserId)); | ||
|
||
new cdk.CfnOutput(stack, 'DistributionDomainName', { value: dist.domainName }); |
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