New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(aws-route53-targets): add global accelerator target to route53 alias targets #13407
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bdc7b84
Add a Route53 Alias target for Global Accelerator
luketn 733ebaf
Export the new alias
luketn cf24be0
Import the globalaccelerator package
luketn 0e341df
Fix tsc warnings
luketn fcfc690
Add peer dependency
luketn 0c9db0d
Use the standard approach for imports rather than the more explicit i…
luketn d1e2110
Add tests
luketn 8ad1035
Merge remote-tracking branch 'upstream/master'
luketn 5d5047c
Merge remote-tracking branch 'upstream/master'
luketn ad6f811
Add a README entry for the new Alias Target
luketn 3975013
Add an integration test. Ran on personal AWS account and proved working.
luketn 82aeb72
Make the alias record id consistent with the others in the README
luketn df4b240
Fix linting issues
luketn 01e4869
Switch from using a type union on the constructor to having two separ…
luketn f6fce6d
Merge branch 'master' into master
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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 GlobalAcceleratorTarget 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. | ||
* | ||
* If passing a string value, it must be a valid domain name for an existing Global Accelerator. e.g. xyz.awsglobalaccelerator.com | ||
* If passing an instance of an accelerator created within CDK, the accelerator.dnsName property will be used as the target. | ||
*/ | ||
constructor(private readonly accelerator: string | globalaccelerator.IAccelerator) { | ||
} | ||
|
||
bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig { | ||
let acceleratorDomainName; | ||
if (typeof this.accelerator === 'string') { | ||
acceleratorDomainName = this.accelerator; | ||
} else { | ||
acceleratorDomainName = this.accelerator.dnsName; | ||
} | ||
return { | ||
hostedZoneId: GlobalAcceleratorTarget.GLOBAL_ACCELERATOR_ZONE_ID, | ||
dnsName: acceleratorDomainName, | ||
}; | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
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'); | ||
}); | ||
|
||
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.GlobalAcceleratorTarget('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', { | ||
njlynch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
comment: 'Alias to the an existing Global Accelerator', | ||
target: route53.RecordTarget.fromAlias(new targets.GlobalAcceleratorTarget('someexisting.awsglobalaccelerator.com')), | ||
recordName: 'test-existing', | ||
zone, | ||
}); | ||
|
||
app.synth(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we can't use type unions in our public APIs as they cannot be strongly-modeled in all languages the CDK supports.
Solutions:
GlobalAcceleratorTarget.domain('xyz.awsglobalaccelerator.com')
,GlobalAcceleratorTarget.accelerator(accelerator)
).ApiGateway
/ApiGatewayDomain
is an existing, alternative pattern. So you could createGlobalAcceleratorTarget
andGlobalAcceleratorDomainTarget
.GlobalAcceleratorTarget
that accepts anIAccelerator
, and add theGlobalAcceleratorDomainTarget
(that takes the domain name/string) later based on feedback from the community.I think given the existing patterns in this module options 2 or 3 are the principal of least surprise; I'm happy either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with option 2. I think it actually makes the code a little cleaner, although it might not be as intuitive to locate and use.