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

Fix CloudFront origin error #514

Merged
merged 5 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Example usage:
```ts
const sourceBucket = new Bucket(this, 'Bucket');

const distribution = new CloudFrontDistribution(this, 'MyDistribution', {
const distribution = new CloudFrontWebDistribution(this, 'MyDistribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket
},
behaviors : [ {isDefaultBehavior}]
behaviors : [ {isDefaultBehavior: true}]
}
]
});
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ export class CloudFrontWebDistribution extends cdk.Construct {
ALL: ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"],
};

/**
* Private variable that holds a copy of our origins. We use this in the validate method.
*/
private origins: cloudformation.DistributionResource.OriginProperty[];

constructor(parent: cdk.Construct, name: string, props: CloudFrontWebDistributionProps) {
super(parent, name);

Expand Down Expand Up @@ -508,7 +513,10 @@ export class CloudFrontWebDistribution extends cdk.Construct {
"origin-access-identity/cloudfront/", originConfig.s3OriginSource.originAccessIdentity.ref
),
};
} else if (originConfig.s3OriginSource) {
originProperty.s3OriginConfig = {};
}

if (originConfig.customOriginSource) {
originProperty.customOriginConfig = {
httpPort: originConfig.customOriginSource.httpPort || 80,
Expand All @@ -526,6 +534,7 @@ export class CloudFrontWebDistribution extends cdk.Construct {
originIndex++;
}
distributionConfig.origins = origins;
this.origins = origins;

const defaultBehaviors = behaviors.filter(behavior => behavior.isDefaultBehavior);
if (defaultBehaviors.length !== 1) {
Expand Down Expand Up @@ -558,6 +567,16 @@ export class CloudFrontWebDistribution extends cdk.Construct {

}

public validate(): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to add origins after the construct is created (might be a useful feature)? If not, then why not just validate this in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't mind validating in the constructor - I just thought that was the point of the validate method.

Is the idea that validate is only used if you have post-constructor modifications? (That makes sense to me, just trying to get a feel for how you see it)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yap. It's a hook that allows you to perform validations just before synthesis. But the sooner you can perform a validation, the better, because the error will include the most context for the user to be able to understand what they did wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood. I'll drop the validate for the time being - I don't currently think adding a origin post creation is valuable. (In my mind - it's not.)

If someone opens a pull request later - it's easy enough to add that.

const messages: string[] = [];
this.origins.forEach(origin => {
if (!origin.s3OriginConfig && !origin.customOriginConfig) {
messages.push(`Origin ${origin.domainName} is missing either S3OriginConfig or CustomOriginConfig. At least 1 must be specified.`);
}
});
return messages;
}

private toBehavior(input: BehaviorWithOrigin, protoPolicy?: ViewerProtocolPolicy) {
let toReturn = {
allowedMethods: this.METHOD_LOOKUP_MAP[input.allowedMethods || CloudFrontAllowedMethods.GET_HEAD],
Expand Down
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"watch": "cdk-watch",
"lint": "cdk-lint",
"test": "cdk-test",
"integ": "cdk-integ",
"pkglint": "pkglint -f",
"package": "cdk-package"
},
Expand All @@ -47,10 +48,11 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/assert": "^0.8.0",
"aws-sdk": "^2.259.1",
"cdk-build-tools": "^0.8.0",
"cfn2ts": "^0.8.0",
"@aws-cdk/assert": "^0.8.0",
"cdk-build-tools": "^0.8.0",
"cdk-integ-tools": "^0.8.0",
"pkglint": "^0.8.0"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"Resources": {
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"CacheBehaviors": [],
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"ForwardedValues": {
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "https-only"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Origins": [
{
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginReadTimeout": 30,
"OriginSSLProtocols": [
"TLSv1.2"
]
},
"DomainName": "brelandm.a2z.com",
"Id": "origin1",
"OriginCustomHeaders": [
{
"HeaderName": "X-Custom-Header",
"HeaderValue": "somevalue"
}
]
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
}
}
}
27 changes: 27 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import cdk = require('@aws-cdk/cdk');
import cloudfront = require('../lib');

const app = new cdk.App(process.argv);

const stack = new cdk.Stack(app, 'aws-cdk-cloudfront-custom');

new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
originConfigs: [
{
originHeaders: {
"X-Custom-Header": "somevalue",
},
customOriginSource: {
domainName: "brelandm.a2z.com",
},
behaviors: [
{
isDefaultBehavior: true,
}
]
}
]
});

process.stdout.write(app.run());
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"Resources": {
"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
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "https-only"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Origins": [
{
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
"DomainName"
]
},
"Id": "origin1",
"S3OriginConfig": {}
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
}
}
}
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudfront = require('../lib');

const app = new cdk.App(process.argv);

const stack = new cdk.Stack(app, 'aws-cdk-cloudfront');

const sourceBucket = new s3.Bucket(stack, 'Bucket');

new cloudfront.CloudFrontWebDistribution(stack, 'MyDistribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket
},
behaviors : [ {isDefaultBehavior: true}]
}
]
});

process.stdout.write(app.run());
94 changes: 92 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/test/test.basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,94 @@ import { CloudFrontWebDistribution } from '../lib';
// tslint:disable:object-literal-key-quotes

export = {

'distribution with custom origin adds custom origin'(test: Test) {
const stack = new cdk.Stack();

new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
originConfigs: [
{
originHeaders: {
"X-Custom-Header": "somevalue",
},
customOriginSource: {
domainName: "myorigin.com",
},
behaviors: [
{
isDefaultBehavior: true,
}
],
}
]
});

expect(stack).toMatch(
{
"Resources": {
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"CacheBehaviors": [],
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"ForwardedValues": {
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "https-only"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Origins": [
{
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginReadTimeout": 30,
"OriginSSLProtocols": [
"TLSv1.2"
]
},
"DomainName": "myorigin.com",
"Id": "origin1",
"OriginCustomHeaders": [
{
"HeaderName": "X-Custom-Header",
"HeaderValue": "somevalue"
}
]
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
}
}
}
);

test.done();
},

'most basic distribution'(test: Test) {
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');
Expand Down Expand Up @@ -44,7 +132,8 @@ export = {
"DomainName"
]
},
"Id": "origin1"
"Id": "origin1",
"S3OriginConfig": {}
}
],
"ViewerCertificate": {
Expand Down Expand Up @@ -117,7 +206,8 @@ export = {
"DomainName"
]
},
"Id": "origin1"
"Id": "origin1",
"S3OriginConfig": {}
}
],
"ViewerCertificate": {
Expand Down