簡體   English   中英

AWS CDK -- 我如何通過 AWS CDK 從我新創建的托管區域中檢索我的 NS 記錄

[英]AWS CDK -- How do I retrieve my NS Records from my newly created Hosted Zone by AWS CDK

假設我創建了一個公共托管區域或從查找中獲取了一個托管區域,並且我想檢索 NS 記錄以供其他用途

const zone = new route53.PublicHostedZone(this, domain + 'HostedZone', {
    zoneName: '' + domain
})
const zone = HostedZone.fromLookup(this, 'HostedZone', { domainName: config.zoneName });

當前的 CDK 是否有任何方法可以做到這一點。 我環顧了 API doco,但沒有找到。 有什么建議么?


更新

我確實嘗試了hostedZoneNameServers屬性。 但是,它似乎沒有返回任何東西。

const zone = route53.HostedZone.fromLookup(this, 'DotnetHostedZone', {
      domainName: <myDomain>,
    });

    new CfnOutput(this, `output1`, {
      value: zone.zoneName
    });

    new CfnOutput(this, `output2`, {
      value: zone.hostedZoneId
    });

    new CfnOutput(this, 'output3', {
      value: zone.hostedZoneNameServers?.toString() || 'No NameServer'
    });   
 ✅  test-ops

Outputs:
test-ops.output1 = <myDomain>
test-ops.output2 = <myZoneId>
test-ops.output3 = No NameServer

我向我的區域確認並用於記錄導出,我可以檢索我的所有記錄。

最終目標是自動化子域配置。 但我目前正在這條路線上摸不着頭腦。

object 區域有一個hostedZoneNameServers屬性。

const zone = HostedZone.fromLookup(this, 'HostedZone', { domainName: config.zoneName });
const nsRecords = zone.hostedZoneNameServers;

參考:

我不相信您可以直接從腳本中做到這一點。 這些值將只是“令牌”,在部署之后/期間將被 CloudFormation 替換,但在綜合期間不會。 因此,在合成過程中輸出會使您失明。 我猜你需要在post-process中獲取它們..

我遇到了同樣的問題,這就是我找到你的帖子的原因:D

hostedZoneNameServers沒有為文檔中提到的私有或導入區域定義。 只有在 CDK 中創建區域時才能使用(例如new PublicHostedZone(...).hostedZoneNameServers )。

如果您在其他地方創建區域,請嘗試使用 AWS Route53 GetHostedZone API

正如@JD D提到的,托管區域有一個hostedZoneNameServers屬性,但它們在跨堆棧中不可用。 文檔已更新(或第一次回答時錯過了)以反映這一點。

CDK V1 / CDK V2

托管區域名稱服務器? 類型:字符串[](可選)

返回特定托管區域的名稱服務器集。 例如:ns1.example.com。

對於私有托管區域或從另一個堆棧導入的托管區域,此屬性將未定義。

因此,為了完成您想要的,您需要在創建托管區域的堆棧上將 NS 值設置為 output,並通過引用提供 NS output 的堆棧來使用它們。

我能夠使用以下代碼自動配置子域。 請注意,這些托管區域共享相同的堆棧,這可能不適用於您的用例。

export const hostedZone = new HostedZone(stack, `${env}-hosted-zone`, {
  zoneName: host,
})

// API
const apiHost = `api.${host}`
export const apiHostedZone = new HostedZone(stack, `${env}-hosted-zone-api`, {
  zoneName: apiHost,
})
// note that this record is actually on the parent zone, 
export const apiHostedZoneNsRecord = new NsRecord(stack, `${env}-hosted-zone-ns-api`, {authoritatively pointing to its sub-subdomain
  recordName: apiHost,
  values: apiHostedZone.hostedZoneNameServers as string[],
  zone: hostedZone,
})

這導致了以下 CFT 片段(當然,${env} 和 ${rnd} 替換為具體值):

"ResourceRecords": {
 "Fn::GetAtt": [
  "${env}hostedzoneapi${rnd}",
  "NameServers"
 ]
},

如果您可以接受相同的堆棧約束,您應該能夠完成此操作。 請注意,雖然我可以接受此堆棧的約束,但更廣泛地說,我有一個多賬戶結構,並且必須手動將子賬戶的子域 NS 記錄添加到父賬戶的根域。 此設置的摘要:

root account:
  example.com
    NS child.example.com // manually added
child account:
  child.example.com // contents of `host` below 
    NS api.child.example.com
  api.child.example.com //  automatic subdomain created with code above

這對我有用

 const nsRecords = hostedZone.hostedZoneNameServers; if (nsRecords) { for (let i=0; i<4; i++) { context.cfnOutput(this, `NS Record${i+1}`, Fn.select(i, nsRecords)); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM