forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route53-targets): s3 bucket website target support (aws#3618)
* feat(route53-targets): s3 bucket website target support * adds region-info S3_STATIC_WEBSITE_ZONE_53_HOSTED_ZONE_ID * chore: tslint fixes * chore: fix indent * chore: change unresolved region error message * chore: RegionInfo.get refactor * chore: fix tests
- Loading branch information
1 parent
3ba14c8
commit bccc11f
Showing
8 changed files
with
161 additions
and
4 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
35 changes: 35 additions & 0 deletions
35
packages/@aws-cdk/aws-route53-targets/lib/bucket-website-target.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,35 @@ | ||
import route53 = require('@aws-cdk/aws-route53'); | ||
import s3 = require('@aws-cdk/aws-s3'); | ||
import {Stack, Token} from '@aws-cdk/core'; | ||
import {RegionInfo} from '@aws-cdk/region-info'; | ||
|
||
/** | ||
* Use a S3 as an alias record target | ||
*/ | ||
export class BucketWebsiteTarget implements route53.IAliasRecordTarget { | ||
constructor(private readonly bucket: s3.Bucket) { | ||
} | ||
|
||
public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig { | ||
const {region} = Stack.of(this.bucket.stack); | ||
|
||
if (Token.isUnresolved(region)) { | ||
throw new Error([ | ||
'Cannot use an S3 record alias in region-agnostic stacks.', | ||
'You must specify a specific region when you define the stack', | ||
'(see https://docs.aws.amazon.com/cdk/latest/guide/environments.html)' | ||
].join(' ')); | ||
} | ||
|
||
const hostedZoneId = RegionInfo.get(region).s3StaticWebsiteHostedZoneId; | ||
|
||
if (!hostedZoneId) { | ||
throw new Error(`Bucket website target is not supported for the "${region}" region`); | ||
} | ||
|
||
return { | ||
hostedZoneId, | ||
dnsName: this.bucket.bucketWebsiteUrl, | ||
}; | ||
} | ||
} |
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,4 +1,5 @@ | ||
export * from './api-gateway-domain-name'; | ||
export * from './bucket-website-target'; | ||
export * from './classic-load-balancer-target'; | ||
export * from './cloudfront-target'; | ||
export * from './load-balancer-target'; |
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
68 changes: 68 additions & 0 deletions
68
packages/@aws-cdk/aws-route53-targets/test/bucket-website-target.test.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,68 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import route53 = require('@aws-cdk/aws-route53'); | ||
import s3 = require('@aws-cdk/aws-s3'); | ||
import { App, Stack } from '@aws-cdk/core'; | ||
import targets = require('../lib'); | ||
|
||
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'); | ||
|
||
// WHEN | ||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
new route53.ARecord(zone, 'Alias', { | ||
zone, | ||
recordName: '_foo', | ||
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite)) | ||
}); | ||
|
||
// THEN | ||
expect(stack).toHaveResource('AWS::Route53::RecordSet', { | ||
AliasTarget: { | ||
DNSName: { "Fn::GetAtt": [ "Bucket83908E77", "WebsiteURL"] }, | ||
HostedZoneId: "Z3AQBSTGFYJSTF" | ||
}, | ||
}); | ||
}); | ||
|
||
test('throws if region agnostic', () => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
const bucketWebsite = new s3.Bucket(stack, 'Bucket'); | ||
|
||
// WHEN | ||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
|
||
// THEN | ||
expect(() => { | ||
new route53.ARecord(zone, 'Alias', { | ||
zone, | ||
recordName: '_foo', | ||
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite)) | ||
}); | ||
}).toThrow(/Cannot use an S3 record alias in region-agnostic stacks/); | ||
}); | ||
|
||
test('throws if bucket website hosting is unavailable (cn-northwest-1)', () => { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app, 'test', {env: {region: 'cn-northwest-1'}}); | ||
|
||
const bucketWebsite = new s3.Bucket(stack, 'Bucket'); | ||
|
||
// WHEN | ||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
|
||
// THEN | ||
expect(() => { | ||
new route53.ARecord(zone, 'Alias', { | ||
zone, | ||
recordName: '_foo', | ||
target: route53.RecordTarget.fromAlias(new targets.BucketWebsiteTarget(bucketWebsite)) | ||
}); | ||
}).toThrow(/Bucket website target is not supported/); | ||
}); |
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
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