[英]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 。
我能夠使用以下代碼自動配置子域。 請注意,這些托管區域共享相同的堆棧,這可能不適用於您的用例。
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.