'Terraform update Route53 DOMAIN NameServers

Is it possible to create terraform script to update domain name server? I want to update the nameservers on the domain itself, not the zone. The domain under "registered domains" needs to set the name servers to point to the zone's name servers.

I have this zone I made:

resource "aws_route53_zone" "redrebelgames" {
  name = "redrebelgames.com"

I can access the name servers using: aws_route53_zone.redrebelgames.nameservers

Does anyone know if this is possible without using a provisioner to run aws sdk methods locally? The only method I can think of is to use a "local-exec" provisioner which would then run a python or ruby script to directly call aws sdk methods. Basically what I am trying to figure out is if it's possible to run this function: https://docs.aws.amazon.com/Route53/latest/APIReference/API_domains_UpdateDomainNameservers.html directly in Terraform, without using an external script.

Solution 1:[1]

I ended up just using a local-exec provisioner which runs this script that I wrote in ruby:

require 'aws-sdk-route53domains'


name_servers = ARGV[2].to_s.split ','

# At the time of writing this, there is only one endpoint for route53domains, at us-east-1, see link below
# https://docs.aws.amazon.com/general/latest/gr/r53.html
client = Aws::Route53Domains::Client.new(
  region: "us-east-1",
  access_key_id: AWS_ACCESS_KEY,
  secret_access_key: AWS_SECRET_KEY

response = client.update_domain_nameservers({
  domain_name: "redrebelgames.com",
  nameservers: name_servers.map{|ns| {name: ns}}

Here is the terraform setup:

resource "aws_route53_zone" "redrebelgames" {
  name = "redrebelgames.com"

  provisioner "local-exec" {
    command = "ruby scripts/update_domain_nameservers.rb ${var.AWS_ACCESS_KEY} ${var.AWS_SECRET_KEY} ${element(aws_route53_zone.redrebelgames.name_servers, 0)},${element(aws_route53_zone.redrebelgames.name_servers, 1)},${element(aws_route53_zone.redrebelgames.name_servers, 2)},${element(aws_route53_zone.redrebelgames.name_servers, 3)}"

Solution 2:[2]

As of May 2022, there's aws_route53domains_registered_domain resource on version ~> 4.0 of hashicorp/aws. For some reason, it's not taking effect. I'm posting this in case someone stumbles upon this Stack Overflow post in the future.

Update: it worked, I just need to wait for a few minutes. Or maybe it's because I ran terraform apply again, not really sure.


This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Gleason
Solution 2