Skip to content

Commit

Permalink
fix(route53): use static s3 website endpoint (#4250)
Browse files Browse the repository at this point in the history
* fix(route53): use static s3 website endpoint

* chore: detailed s3 target example

* chore: match test bucketName with generated records
  • Loading branch information
Jimmy Gaussen authored and mergify[bot] committed Sep 26, 2019
1 parent cb39822 commit 3c252c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 51 deletions.
28 changes: 21 additions & 7 deletions packages/@aws-cdk/aws-route53-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ This library contains Route53 Alias Record targets for:
target: route53.RecordTarget.fromAlias(new alias.CloudFrontTarget(distribution)),
});
```
* S3 Bucket WebSite
```ts
new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new alias.BucketWebsiteTarget(bucket)),
});
```
* ELBv2 load balancers
```ts
new route53.ARecord(this, 'AliasRecord', {
Expand All @@ -48,5 +41,26 @@ This library contains Route53 Alias Record targets for:
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
});
```
* S3 Bucket WebSite:

**Important:** The Bucket name must strictly match the full DNS name.
See [the Developer Guide](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/getting-started.html) for more info.
```ts
const [recordName, domainName] = ['www', 'example.com'];

const bucketWebsite = new Bucket(this, 'BucketWebsite', {
bucketName: [recordName, domainName].join('.'), // www.example.com
publicReadAccess: true,
websiteIndexDocument: 'index.html',
});

const zone = HostedZone.fromLookup(this, 'Zone', {domainName}); // example.com

new route53.ARecord(this, 'AliasRecord', {
zone,
recordName, // www
target: route53.RecordTarget.fromAlias(new alias.BucketWebsiteTarget(bucket)),
});
```

See the documentation of `@aws-cdk/aws-route53` for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ export class BucketWebsiteTarget implements route53.IAliasRecordTarget {
].join(' '));
}

const hostedZoneId = RegionInfo.get(region).s3StaticWebsiteHostedZoneId;
const {s3StaticWebsiteHostedZoneId: hostedZoneId, s3StaticWebsiteEndpoint: dnsName} = RegionInfo.get(region);

if (!hostedZoneId) {
if (!hostedZoneId || !dnsName) {
throw new Error(`Bucket website target is not supported for the "${region}" region`);
}

return {
hostedZoneId,
dnsName: this.bucket.bucketWebsiteDomainName,
};
return {hostedZoneId, dnsName};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@ import s3 = require('@aws-cdk/aws-s3');
import { App, Stack } from '@aws-cdk/core';
import targets = require('../lib');

const [recordName, zoneName] = ['foo', 'test.public'];
const bucketName = [recordName, zoneName].join('.');

test('use S3 bucket website as record target', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'test', {env: {region: 'us-east-1'}});

const bucketWebsite = new s3.Bucket(stack, 'Bucket');
const bucketWebsite = new s3.Bucket(stack, 'Bucket', { bucketName });

// WHEN
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName });
new route53.ARecord(zone, 'Alias', {
zone,
recordName: '_foo',
recordName,
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite))
});

// THEN
expect(stack).toHaveResource('AWS::Route53::RecordSet', {
AliasTarget: {
DNSName: {
"Fn::Select": [
2,
{
"Fn::Split": [
"/",
{
"Fn::GetAtt": [
"Bucket83908E77",
"WebsiteURL"
]
}
]
}
]
},
DNSName: "s3-website-us-east-1.amazonaws.com",
HostedZoneId: "Z3AQBSTGFYJSTF"
},
});
Expand All @@ -48,30 +36,20 @@ test('use S3 bucket website as record target (fromBucketName)', () => {
const app = new App();
const stack = new Stack(app, 'test', {env: {region: 'us-east-1'}});

const bucketWebsite = s3.Bucket.fromBucketName(stack, 'Bucket', 'test');
const bucketWebsite = s3.Bucket.fromBucketName(stack, 'Bucket', bucketName);

// WHEN
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName });
new route53.ARecord(zone, 'Alias', {
zone,
recordName: '_foo',
recordName,
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite))
});

// THEN
expect(stack).toHaveResource('AWS::Route53::RecordSet', {
AliasTarget: {
DNSName: {
"Fn::Join": [
"",
[
"test.s3-website-us-east-1.",
{
Ref: "AWS::URLSuffix"
}
]
]
},
DNSName: "s3-website-us-east-1.amazonaws.com",
HostedZoneId: "Z3AQBSTGFYJSTF"
},
});
Expand All @@ -81,16 +59,16 @@ test('throws if region agnostic', () => {
// GIVEN
const stack = new Stack();

const bucketWebsite = new s3.Bucket(stack, 'Bucket');
const bucketWebsite = new s3.Bucket(stack, 'Bucket', { bucketName });

// WHEN
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName });

// THEN
expect(() => {
new route53.ARecord(zone, 'Alias', {
zone,
recordName: '_foo',
recordName,
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite))
});
}).toThrow(/Cannot use an S3 record alias in region-agnostic stacks/);
Expand All @@ -104,13 +82,13 @@ test('throws if bucket website hosting is unavailable (cn-northwest-1)', () => {
const bucketWebsite = new s3.Bucket(stack, 'Bucket');

// WHEN
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName });

// THEN
expect(() => {
new route53.ARecord(zone, 'Alias', {
zone,
recordName: '_foo',
recordName,
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite))
});
}).toThrow(/Bucket website target is not supported/);
Expand Down

0 comments on commit 3c252c4

Please sign in to comment.