I wrote a CloudFormation template which creates a vpc, subnets, routes, asg's and instances.
I want CloudFormation to handle the association of the newly created vpc with an existing Route53 hosted zone but I can't find how to do it in CloudFormation.
Using the aws cli, this can be achieved by running (in the user_data script):
aws route53 associate-vpc-with-hosted-zone --hosted-zone-id AAZZZ123AA --vpc VPCRegion=us-west-2,VPCId=$vpcid
But I want CloudFormation to manage these associations so when the stack is deleted then the vpc association will be deleted as well.
I couldn't find online how it can be achieved with CloudFormation, so does anybody know if it can be done?
Best Answer
I couldn't find how to associate a VPC to a Route53 hosted zone, so I've added to the user_data script the following code:
vpcid variable is inherited from the CloudFormation template:
Then, I realized that when a VPC is deleted, it's association to the Route53 hosted zone remains.
In order to make sure that old associations are removed, I've added the following code to the user_data script: