aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.63k stars 3.91k forks source link

route53: No way to alias an existing record #23048

Closed adam-nielsen closed 6 months ago

adam-nielsen commented 1 year ago

Describe the bug

I am trying to create a Route53 alias to an existing record.

The test code demonstrates how to do this if you create the target record in the same stack.

However if you haven't created the target record and instead need to load an existing record, there does not appear to be a way of doing so.

You could try fromValues() but it only accepts IP addresses:

new route53.ARecord(this, 'Alias', {
  zone,
  target: route53.RecordTarget.fromValues('target.example.com'),
});

[Invalid Resource Record: 'FATAL problem: ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address) encountered with 'target.example.com'']

It seems it is not possible to create an alias in one stack to a Route53 record from another stack, even if both are in the same hosted zone. It can only be done via the AWS Console.

Expected Behavior

I expected fromValues() to allow me to look up a Route53 record by name, or for there to be some other way to load a Route53 record so it can be used as a target.

Current Behavior

The only current way that might work to do this - with fromValues() - only accepts IP addresses, not hostnames.

Reproduction Steps

Use the test code but have it load an existing Route53 record to use as the alias target, instead of targeting the record it just created.

Possible Solution

Either allow fromValues() to look up via hostname, or add lookup methods to Route53.ARecord and the other record classes to allow existing records to be retrieved.

I did find a workaround online, which is to pass a bind() function in instead:

new route53.ARecord(this, 'Alias', {
  zone,
  target: route53.RecordTarget.fromAlias({
    bind: () => ({
      dnsName: 'existing.example.com',
      hostedZoneId: zone.hostedZoneId,
    }),
  }),
});

However this seems a bit hacky.

Additional Information/Context

I have the target DNS entry created in a primary CDK stack, and I want to make an alias for it in a few secondary CDK stacks for other projects. This way if the IP address ever changes in the primary stack, the alias in the secondary stacks won't need to be updated, and I won't have to hard-code any IP addresses from the primary's resources in the secondary stacks.

CDK CLI Version

2.51.1 (latest docs online)

Framework Version

No response

Node.js Version

N/A (just looking at the docs, no code yet)

OS

Linux

Language

Typescript

Language Version

No response

Other information

No response

pahud commented 1 year ago

Hi @adam-nielsen

Please check out my sample below.

At this moment it is possible to create an Alias ARecord to an existing route53 record set as long as the existing one is created by CDK. However, if the existing record set is created outsides of CDK, it seems not possible at this moment. Are you trying to create an alias to an existing target record set created outsides of CDK(e.g. manually created)?

const zone = new route53.HostedZone(stack, 'Zone', {
  zoneName: 'demo.com',
});

const record = new route53.RecordSet(stack, 'target', {
  zone,
  recordName: 'target.demo.com',
  recordType: route53.RecordType.A,
  target: route53.RecordTarget.fromIpAddresses('1.2.3.4'),
});

// alias test.demo.com to the record set of 'target.demo.com'
new route53.ARecord(stack, 'Alias', {
  zone,
  recordName: 'test.demo.com',
  target: route53.RecordTarget.fromAlias(new targets.Route53RecordTarget(record)),
});
adam-nielsen commented 1 year ago

Hi @pahud, yes the existing record has been created manually, so I need to somehow load it into my CDK stack, in order to pass it to Route53RecordTarget.

pahud commented 1 year ago

I think we probably need a construct method like

route53.ARecord.fromAttributes(...) 

to import existing ARecords. I am making this as P2 but any PRs from the community would be highly appreciated!

czabriskie commented 1 year ago

I need this functionality too.

raveenplgithub commented 1 year ago

+1

pahud commented 1 year ago

please help us 👍 on the original issue post rather than +1 in the comments.

According to the contributing guide, any P2 issue with 20 or more +1s will be automatically upgraded from P2 to P1.

github-actions[bot] commented 8 months ago

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

github-actions[bot] commented 6 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.