简体   繁体   English

如何将 Route53 别名添加到现有 Route53 条目?

[英]How to add a Route53 alias to an existing Route53 entry?

I have a DNS entry already in Route 53. In my CDK stack, I want to set up an alias (effectively a CNAME), so I have a new DNS entry pointing to the existing one.我在 Route 53 中已经有一个 DNS 条目。在我的 CDK 堆栈中,我想设置一个别名(实际上是一个 CNAME),所以我有一个指向现有条目的新 DNS 条目。 For example, I want to make new.example.com point to existing.example.com , where example.com is hosted in Route 53 and existing is already set up and working.例如,我想让new.example.com指向existing.example.com ,其中example.com托管在 Route 53 中并且existing已经设置好并可以正常工作。

Through the AWS console I would just create a new A or CNAME record for new.example.com and set it as an alias of existing.example.com and it would be fine, so I am trying to replicate this in CDK.通过 AWS 控制台,我将为new.example.com创建一个新ACNAME记录,并将其设置为existing.example.com的别名,这样就可以了,所以我试图在 CDK 中复制它。

In the CDK docs , it says I can do this:CDK 文档中,它说我可以这样做:

declare const zone: route53.HostedZone;
declare const record: route53.ARecord;
new route53.ARecord(this, 'AliasRecord', {
  zone,
  target: route53.RecordTarget.fromAlias(new targets.Route53RecordTarget(record)),
});

However, the docs don't say how to populate the record variable.但是,文档没有说明如何填充record变量。

If I look at the route53.ARecord docs , there doesn't appear to be a way to look up an existing record.如果我查看route53.ARecord文档,似乎没有办法查找现有记录。

The closest I could find is using one of the other RecordSet.fromXXX() functions instead of fromAlias , however there doesn't appear to be one that can look up host names:我能找到的最接近的是使用其他RecordSet.fromXXX()函数之一而不是fromAlias ,但是似乎没有可以查找主机名的函数:

...
target: cdkRoute53.RecordTarget.fromValues('target.example.com'), // doesn't work

Unfortunately, RecordTarget.fromValues() only accepts an IP address.不幸的是, RecordTarget.fromValues()只接受 IP 地址。 If you put a hostname in, it tells you:如果你输入一个主机名,它会告诉你:

Invalid Resource Record: 'FATAL problem: ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address) encountered with 'target.example.com'无效的资源记录:'致命问题:'target.example.com' 遇到 ARRDATAIllegalIPv4Address(值不是有效的 IPv4 地址)

So it looks like I can only create the alias if I also created the target records in the same stack - there doesn't appear to be a way to load an existing record, so you can pass it in as the target for the new alias.所以看起来我只能在同一个堆栈中创建目标记录时才能创建别名 - 似乎没有办法加载现有记录,因此您可以将其作为新别名的目标传递.

What am I missing?我错过了什么?

I found a workaround in the meantime thanks to this answer .由于这个答案,我同时找到了解决方法。 Since then they added the functionality that was missing, but it looks like they didn't provide lookups so that workaround still applies.从那时起,他们添加了缺少的功能,但看起来他们没有提供查找功能,因此解决方法仍然适用。

The solution is to provide a bind() function instead that provides the necessary information:解决方案是提供一个bind() function 而不是提供必要的信息:

const route53entry = new cdkRoute53.ARecord(this, 'dns', {
  recordName: 'new.example.com',
  zone: hostedZone,
  target: cdkRoute53.RecordTarget.fromAlias({
    bind: () => ({
      dnsName: 'existing.example.com',
      hostedZoneId: hostedZone.hostedZoneId,
    }),
  }),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM