Skip to content

Commit

Permalink
feat(s3): export bucket websiteURL (#1521) (#1544)
Browse files Browse the repository at this point in the history
fixes #1521
  • Loading branch information
AlexChesters authored and Elad Ben-Israel committed Jan 15, 2019
1 parent 32a4b29 commit 4e46d3c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/@aws-cdk/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export interface IBucket extends cdk.IConstruct {
*/
export interface BucketImportProps {
/**
* The ARN fo the bucket. At least one of bucketArn or bucketName must be
* The ARN of the bucket. At least one of bucketArn or bucketName must be
* defined in order to initialize a bucket ref.
*/
bucketArn?: string;
Expand All @@ -199,6 +199,13 @@ export interface BucketImportProps {
* @default Inferred from bucket name
*/
bucketDomainName?: string;

/**
* The website URL of the bucket (if static web hosting is enabled).
*
* @default Inferred from bucket name
*/
bucketWebsiteUrl?: string;
}

/**
Expand Down Expand Up @@ -571,6 +578,7 @@ export class Bucket extends BucketBase {
public readonly bucketArn: string;
public readonly bucketName: string;
public readonly domainName: string;
public readonly bucketWebsiteUrl: string;
public readonly dualstackDomainName: string;
public readonly encryptionKey?: kms.IEncryptionKey;
public policy?: BucketPolicy;
Expand Down Expand Up @@ -599,6 +607,7 @@ export class Bucket extends BucketBase {
this.bucketArn = resource.bucketArn;
this.bucketName = resource.bucketName;
this.domainName = resource.bucketDomainName;
this.bucketWebsiteUrl = resource.bucketWebsiteUrl;
this.dualstackDomainName = resource.bucketDualStackDomainName;

// Add all lifecycle rules
Expand All @@ -621,6 +630,7 @@ export class Bucket extends BucketBase {
bucketArn: new cdk.Output(this, 'BucketArn', { value: this.bucketArn }).makeImportValue().toString(),
bucketName: new cdk.Output(this, 'BucketName', { value: this.bucketName }).makeImportValue().toString(),
bucketDomainName: new cdk.Output(this, 'DomainName', { value: this.domainName }).makeImportValue().toString(),
bucketWebsiteUrl: new cdk.Output(this, 'WebsiteURL', { value: this.bucketWebsiteUrl }).makeImportValue().toString()
};
}

Expand Down Expand Up @@ -956,6 +966,7 @@ class ImportedBucket extends BucketBase {
public readonly bucketArn: string;
public readonly bucketName: string;
public readonly domainName: string;
public readonly bucketWebsiteUrl: string;
public readonly encryptionKey?: kms.EncryptionKey;

public policy?: BucketPolicy;
Expand All @@ -972,6 +983,7 @@ class ImportedBucket extends BucketBase {
this.bucketArn = parseBucketArn(this, props);
this.bucketName = bucketName;
this.domainName = props.bucketDomainName || this.generateDomainName();
this.bucketWebsiteUrl = props.bucketWebsiteUrl || this.generateBucketWebsiteUrl();
this.autoCreatePolicy = false;
this.policy = undefined;
}
Expand All @@ -986,4 +998,8 @@ class ImportedBucket extends BucketBase {
private generateDomainName() {
return `${this.bucketName}.s3.amazonaws.com`;
}

private generateBucketWebsiteUrl() {
return `${this.bucketName}.s3-website-${new cdk.Aws().region}.amazonaws.com`;
}
}
36 changes: 34 additions & 2 deletions packages/@aws-cdk/aws-s3/test/test.bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ export = {
test.deepEqual(bucket.node.resolve(bucketRef), {
bucketArn: { 'Fn::ImportValue': 'MyStack:MyBucketBucketArnE260558C' },
bucketName: { 'Fn::ImportValue': 'MyStack:MyBucketBucketName8A027014' },
bucketDomainName: { 'Fn::ImportValue': 'MyStack:MyBucketDomainNameF76B9A7A' }
bucketDomainName: { 'Fn::ImportValue': 'MyStack:MyBucketDomainNameF76B9A7A' },
bucketWebsiteUrl: { 'Fn::ImportValue': 'MyStack:MyBucketWebsiteURL9C222788' }
});
test.done();
},
Expand All @@ -346,7 +347,8 @@ export = {
test.deepEqual(bucket.node.resolve(bucketRef), {
bucketArn: { 'Fn::ImportValue': 'MyStack:MyBucketBucketArnE260558C' },
bucketName: { 'Fn::ImportValue': 'MyStack:MyBucketBucketName8A027014' },
bucketDomainName: { 'Fn::ImportValue': 'MyStack:MyBucketDomainNameF76B9A7A' }
bucketDomainName: { 'Fn::ImportValue': 'MyStack:MyBucketDomainNameF76B9A7A' },
bucketWebsiteUrl: { 'Fn::ImportValue': 'MyStack:MyBucketWebsiteURL9C222788' }
});
test.done();
},
Expand Down Expand Up @@ -465,6 +467,17 @@ export = {
"Export": {
"Name": "S1:MyBucketDomainNameF76B9A7A"
}
},
"MyBucketWebsiteURL9C222788": {
"Value": {
"Fn::GetAtt": [
"MyBucketF68F3FF0",
"WebsiteURL"
]
},
"Export": {
"Name": "S1:MyBucketWebsiteURL9C222788"
}
}
}
});
Expand Down Expand Up @@ -898,6 +911,17 @@ export = {
"Export": {
"Name": "MyBucketDomainNameF76B9A7A"
}
},
"MyBucketWebsiteURL9C222788": {
"Value": {
"Fn::GetAtt": [
"MyBucketF68F3FF0",
"WebsiteURL"
]
},
"Export": {
"Name": "MyBucketWebsiteURL9C222788"
}
}
}
});
Expand Down Expand Up @@ -1190,6 +1214,14 @@ export = {
}
}));
test.done();
},
'exports the WebsiteURL'(test: Test) {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Website', {
websiteIndexDocument: 'index.html'
});
test.deepEqual(bucket.node.resolve(bucket.bucketWebsiteUrl), { 'Fn::GetAtt': [ 'Website32962D0B', 'WebsiteURL' ] });
test.done();
}
}
};

0 comments on commit 4e46d3c

Please sign in to comment.