Home > Blockchain >  Terraform update Route53 DOMAIN NameServers
Terraform update Route53 DOMAIN NameServers

Time:10-25

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.

CodePudding user response:

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

require 'aws-sdk-route53domains'

Aws.use_bundled_cert!

AWS_ACCESS_KEY = ARGV[0]
AWS_SECRET_KEY = ARGV[1]
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)}"
  }
}
  • Related