-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-route53-targets): add global accelerator target to route53 a…
…lias targets (#13407) Closes #12839 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
7 changed files
with
199 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
packages/@aws-cdk/aws-route53-targets/lib/global-accelerator-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,41 @@ | ||
import * as globalaccelerator from '@aws-cdk/aws-globalaccelerator'; | ||
import * as route53 from '@aws-cdk/aws-route53'; | ||
|
||
|
||
/** | ||
* Use a Global Accelerator domain name as an alias record target. | ||
*/ | ||
export class GlobalAcceleratorDomainTarget implements route53.IAliasRecordTarget { | ||
/** | ||
* The hosted zone Id if using an alias record in Route53. | ||
* This value never changes. | ||
* Ref: https://docs.aws.amazon.com/general/latest/gr/global_accelerator.html | ||
*/ | ||
public static readonly GLOBAL_ACCELERATOR_ZONE_ID = 'Z2BJ6XQ5FK7U4H'; | ||
|
||
/** | ||
* Create an Alias Target for a Global Accelerator domain name. | ||
*/ | ||
constructor(private readonly acceleratorDomainName: string) { | ||
} | ||
|
||
bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig { | ||
return { | ||
hostedZoneId: GlobalAcceleratorTarget.GLOBAL_ACCELERATOR_ZONE_ID, | ||
dnsName: this.acceleratorDomainName, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Use a Global Accelerator instance domain name as an alias record target. | ||
*/ | ||
export class GlobalAcceleratorTarget extends GlobalAcceleratorDomainTarget { | ||
|
||
/** | ||
* Create an Alias Target for a Global Accelerator instance. | ||
*/ | ||
constructor(accelerator: globalaccelerator.IAccelerator) { | ||
super(accelerator.dnsName); | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
packages/@aws-cdk/aws-route53-targets/test/global-accelerator-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,59 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import * as globalaccelerator from '@aws-cdk/aws-globalaccelerator'; | ||
import * as route53 from '@aws-cdk/aws-route53'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import * as targets from '../lib'; | ||
|
||
test('GlobalAcceleratorTarget exposes a public constant of the zone id', () => { | ||
expect(targets.GlobalAcceleratorTarget.GLOBAL_ACCELERATOR_ZONE_ID).toStrictEqual('Z2BJ6XQ5FK7U4H'); | ||
expect(targets.GlobalAcceleratorDomainTarget.GLOBAL_ACCELERATOR_ZONE_ID).toStrictEqual('Z2BJ6XQ5FK7U4H'); | ||
}); | ||
|
||
test('GlobalAcceleratorTarget creates an alias resource with a string domain name', () => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
|
||
// WHEN | ||
new route53.ARecord(stack, 'GlobalAcceleratorAlias', { | ||
target: route53.RecordTarget.fromAlias(new targets.GlobalAcceleratorDomainTarget('xyz.awsglobalaccelerator.com')), | ||
recordName: 'test', | ||
zone, | ||
}); | ||
|
||
// THEN | ||
expect(stack).toHaveResource('AWS::Route53::RecordSet', { | ||
AliasTarget: { | ||
DNSName: 'xyz.awsglobalaccelerator.com', | ||
HostedZoneId: 'Z2BJ6XQ5FK7U4H', | ||
}, | ||
}); | ||
}); | ||
|
||
test('GlobalAcceleratorTarget creates an alias resource with a Global Accelerator reference domain name', () => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
const accelerator = new globalaccelerator.Accelerator(stack, 'Accelerator'); | ||
const logicalId = stack.getLogicalId(<globalaccelerator.CfnAccelerator>accelerator.node.defaultChild); | ||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
|
||
// WHEN | ||
new route53.ARecord(stack, 'GlobalAcceleratorAlias', { | ||
target: route53.RecordTarget.fromAlias(new targets.GlobalAcceleratorTarget(accelerator)), | ||
recordName: 'test', | ||
zone, | ||
}); | ||
|
||
// THEN | ||
expect(stack).toHaveResource('AWS::Route53::RecordSet', { | ||
AliasTarget: { | ||
DNSName: { | ||
'Fn::GetAtt': [ | ||
logicalId, | ||
'DnsName', | ||
], | ||
}, | ||
HostedZoneId: 'Z2BJ6XQ5FK7U4H', | ||
}, | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
...ages/@aws-cdk/aws-route53-targets/test/integ.globalaccelerator-alias-target.expected.json
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,52 @@ | ||
{ | ||
"Resources": { | ||
"Accelerator8EB0B6B1": { | ||
"Type": "AWS::GlobalAccelerator::Accelerator", | ||
"Properties": { | ||
"Name": "aws-cdk-globalaccelerator-integ", | ||
"Enabled": true | ||
} | ||
}, | ||
"HostedZoneDB99F866": { | ||
"Type": "AWS::Route53::HostedZone", | ||
"Properties": { | ||
"Name": "test.public." | ||
} | ||
}, | ||
"LocalGlobalAcceleratorAlias18B4A87A": { | ||
"Type": "AWS::Route53::RecordSet", | ||
"Properties": { | ||
"Name": "test-local.test.public.", | ||
"Type": "A", | ||
"AliasTarget": { | ||
"DNSName": { | ||
"Fn::GetAtt": [ | ||
"Accelerator8EB0B6B1", | ||
"DnsName" | ||
] | ||
}, | ||
"HostedZoneId": "Z2BJ6XQ5FK7U4H" | ||
}, | ||
"Comment": "Alias to the locally created Global Accelerator", | ||
"HostedZoneId": { | ||
"Ref": "HostedZoneDB99F866" | ||
} | ||
} | ||
}, | ||
"ExistingGlobalAcceleratorAlias7ACF888C": { | ||
"Type": "AWS::Route53::RecordSet", | ||
"Properties": { | ||
"Name": "test-existing.test.public.", | ||
"Type": "A", | ||
"AliasTarget": { | ||
"DNSName": "someexisting.awsglobalaccelerator.com", | ||
"HostedZoneId": "Z2BJ6XQ5FK7U4H" | ||
}, | ||
"Comment": "Alias to the an existing Global Accelerator", | ||
"HostedZoneId": { | ||
"Ref": "HostedZoneDB99F866" | ||
} | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/@aws-cdk/aws-route53-targets/test/integ.globalaccelerator-alias-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,31 @@ | ||
#!/usr/bin/env node | ||
import * as globalaccelerator from '@aws-cdk/aws-globalaccelerator'; | ||
import * as route53 from '@aws-cdk/aws-route53'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as targets from '../lib'; | ||
|
||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'aws-cdk-globalaccelerator-integ'); | ||
|
||
let accelerator = new globalaccelerator.Accelerator(stack, 'Accelerator', { | ||
acceleratorName: `${stack.stackName}`, | ||
enabled: true, | ||
}); | ||
|
||
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' }); | ||
|
||
new route53.ARecord(stack, 'LocalGlobalAcceleratorAlias', { | ||
comment: 'Alias to the locally created Global Accelerator', | ||
target: route53.RecordTarget.fromAlias(new targets.GlobalAcceleratorTarget(accelerator)), | ||
recordName: 'test-local', | ||
zone, | ||
}); | ||
|
||
new route53.ARecord(stack, 'ExistingGlobalAcceleratorAlias', { | ||
comment: 'Alias to the an existing Global Accelerator', | ||
target: route53.RecordTarget.fromAlias(new targets.GlobalAcceleratorDomainTarget('someexisting.awsglobalaccelerator.com')), | ||
recordName: 'test-existing', | ||
zone, | ||
}); | ||
|
||
app.synth(); |