Skip to content

Commit dd574cc

Browse files
Jimmy GaussenElad Ben-Israel
authored andcommitted
feat(s3): bucket websiteRedirect (#3392)
Implements missing Bucket WebsiteConfiguration [RedirectAllRequestsTo](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-websiteconfiguration.html#cfn-s3-websiteconfiguration-redirectallrequeststo) property. The constructor checks that the following requirement is fulfilled: > If you specify this property, you can't specify any other property. Fixes #1083
1 parent f53f845 commit dd574cc

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,31 @@ export interface CorsRule {
695695
readonly exposedHeaders?: string[];
696696
}
697697

698+
/**
699+
* All http request methods
700+
*/
701+
export enum RedirectProtocol {
702+
HTTP = 'http',
703+
HTTPS = 'https',
704+
}
705+
706+
/**
707+
* Specifies a redirect behavior of all requests to a website endpoint of a bucket.
708+
*/
709+
export interface RedirectTarget {
710+
/**
711+
* Name of the host where requests are redirected
712+
*/
713+
readonly hostName: string;
714+
715+
/**
716+
* Protocol to use when redirecting requests
717+
*
718+
* @default - The protocol used in the original request.
719+
*/
720+
readonly protocol?: RedirectProtocol;
721+
}
722+
698723
export interface BucketProps {
699724
/**
700725
* The kind of server-side encryption to apply to this bucket.
@@ -762,6 +787,15 @@ export interface BucketProps {
762787
*/
763788
readonly websiteErrorDocument?: string;
764789

790+
/**
791+
* Specifies the redirect behavior of all requests to a website endpoint of a bucket.
792+
*
793+
* If you specify this property, you can't specify "websiteIndexDocument" nor "websiteErrorDocument".
794+
*
795+
* @default - No redirection.
796+
*/
797+
readonly websiteRedirect?: RedirectTarget;
798+
765799
/**
766800
* Grants public read access to all objects in the bucket.
767801
* Similar to calling `bucket.grantPublicAccess()`
@@ -1214,17 +1248,22 @@ export class Bucket extends BucketBase {
12141248
}
12151249

12161250
private renderWebsiteConfiguration(props: BucketProps): CfnBucket.WebsiteConfigurationProperty | undefined {
1217-
if (!props.websiteErrorDocument && !props.websiteIndexDocument) {
1251+
if (!props.websiteErrorDocument && !props.websiteIndexDocument && !props.websiteRedirect) {
12181252
return undefined;
12191253
}
12201254

12211255
if (props.websiteErrorDocument && !props.websiteIndexDocument) {
12221256
throw new Error(`"websiteIndexDocument" is required if "websiteErrorDocument" is set`);
12231257
}
12241258

1259+
if (props.websiteRedirect && (props.websiteErrorDocument || props.websiteIndexDocument)) {
1260+
throw new Error('"websiteIndexDocument" and "websiteErrorDocument" cannot be set if "websiteRedirect" is used');
1261+
}
1262+
12251263
return {
12261264
indexDocument: props.websiteIndexDocument,
1227-
errorDocument: props.websiteErrorDocument
1265+
errorDocument: props.websiteErrorDocument,
1266+
redirectAllRequestsTo: props.websiteRedirect,
12281267
};
12291268
}
12301269
}

packages/@aws-cdk/aws-s3/test/test.bucket.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,38 @@ export = {
15241524
});
15251525
test.deepEqual(stack.resolve(bucket.bucketWebsiteUrl), { 'Fn::GetAtt': ['Website32962D0B', 'WebsiteURL'] });
15261526
test.done();
1527-
}
1527+
},
1528+
'adds RedirectAllRequestsTo property'(test: Test) {
1529+
const stack = new cdk.Stack();
1530+
new s3.Bucket(stack, 'Website', {
1531+
websiteRedirect: {
1532+
hostName: 'www.example.com',
1533+
protocol: s3.RedirectProtocol.HTTPS
1534+
}
1535+
});
1536+
expect(stack).to(haveResource('AWS::S3::Bucket', {
1537+
WebsiteConfiguration: {
1538+
RedirectAllRequestsTo: {
1539+
HostName: 'www.example.com',
1540+
Protocol: 'https'
1541+
}
1542+
}
1543+
}));
1544+
test.done();
1545+
},
1546+
'fails if websiteRedirect and another website property are specified'(test: Test) {
1547+
const stack = new cdk.Stack();
1548+
test.throws(() => {
1549+
new s3.Bucket(stack, 'Website', {
1550+
websiteIndexDocument: 'index.html',
1551+
websiteErrorDocument: 'error.html',
1552+
websiteRedirect: {
1553+
hostName: 'www.example.com'
1554+
}
1555+
});
1556+
}, /"websiteIndexDocument" and "websiteErrorDocument" cannot be set if "websiteRedirect" is used/);
1557+
test.done();
1558+
},
15281559
},
15291560

15301561
'Bucket.fromBucketArn'(test: Test) {

0 commit comments

Comments
 (0)