如何使用CloudFormationpipe理VPC关联到Route53托pipe区域?

我写了一个CloudFormation模板,创build一个vpc,子网,路由,asg和实例。

我希望CloudFormation处理新创build的vpc与现有的Route53托pipe区域的关联,但是我无法find如何在CloudFormation中执行的操作。

使用aws cli,可以通过运行(在user_data脚本中)来实现:

aws route53 associate-vpc-with-hosted-zone --hosted-zone-id AAZZZ123AA --vpc VPCRegion=us-west-2,VPCId=$vpcid 

但是我希望CloudFormationpipe理这些关联,所以当堆栈被删除时,vpc关联也会被删除。

我无法在网上find如何使用CloudFormation来实现,所以有人知道它是否可以完成?

我找不到如何将VPC关联到Route53托pipe区域,所以我已经将以下代码添加到了user_data脚本中:

 aws route53 get-hosted-zone --id XXXAAA12345 | grep -q $vpcid if [[ ! $? -eq 0 ]]; then aws route53 associate-vpc-with-hosted-zone --hosted-zone-id XXXAAA12345 --vpc VPCRegion=us-west-2,VPCId=$vpcid else echo "VPC $vpcid is already associated to hosted zone company-private" fi 

vpcidvariables是从CloudFormation模板inheritance的:

 { "Fn::Join": [ "=", [ "vpcid", { "Ref": "VPC" } ] ] }, 

然后,我意识到,当VPC被删除时,它与Route53托pipe区域的关联仍然存在。

为了确保删除旧的关联,我将下面的代码添加到了user_data脚本中:

 defaultvpc="vpc-20AAAA4b" vpc_array=() for vpc in $(aws route53 get-hosted-zone --id XXXAAA12345 | grep vpc | awk '{print $2}' | tr -d '\"|,'); do vpc_array+=($vpc) done for i in ${!vpc_array[@]}; do if [[ ! ${vpc_array[$i]} = $vpcid && ! ${vpc_array[$i]} = $defaultvpc ]] ; then echo "VPC ${vpc_array[$i]} doesnt exist anymore - removing association to Route53 hosted zone" aws route53 disassociate-vpc-from-hosted-zone --hosted-zone-id XXXAAA12345 --vpc VPCRegion=us-west-2,VPCId=${vpc_array[$i]} fi done