带有Terraform的Route 53故障转移策略

时间:2018-10-22 06:52:17

标签: terraform amazon-route53 terraform-provider-aws

我尝试使用terraform在AWS上为域创建故障转移策略,问题是,它仅在route53资源后面附加一个elb dns名称为PRIMARY和SECONDARY。我实际上希望它使用北弗吉尼亚州的elb名称作为PRIMARY,而俄勒冈州使用SECONDARY。

这是一个多区域架构,我用相同的源目录创建了模块。

文件名:route53.tf

resource "aws_route53_record" "www1" {
  zone_id = "Zone-ID"
  name    = "www1"
  type    = "A"
  #ttl     = "5"

  failover_routing_policy {
    type = "PRIMARY"
  }

  set_identifier = "www1"
  #records        = ["${aws_elb.web.dns_name}"]
  alias {
    name                   = "${aws_elb.web.dns_name}"
    zone_id                = "${aws_elb.web.zone_id}"
    evaluate_target_health = true
  }

}
resource "aws_route53_record" "www2" {
  zone_id = "Zone-ID"
  name    = "www2"
  type    = "A"
  #ttl     = "5"

  failover_routing_policy {
    type = "SECONDARY"
  }

  set_identifier = "www2"
  #records        = ["${aws_elb.web.dns_name}"]
  alias {
    name                   = "${aws_elb.web.dns_name}"
    zone_id                = "${aws_elb.web.zone_id}"
    evaluate_target_health = true
  }
}

文件名:alb_elb.tf

resource "aws_elb" "web" {
  name               = "web-elb"
  availability_zones = "${var.az}"

  listener {
    instance_port     = 8000
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }

  health_check {
    healthy_threshold   = 2
    unhealthy_threshold = 2
    timeout             = 3
    target              = "HTTP:8000/"
    interval            = 30
  }

  instances                   = ["${aws_instance.web.*.id}"]
  cross_zone_load_balancing   = true
  idle_timeout                = 400
  connection_draining         = true
  connection_draining_timeout = 400

  tags {
    Name = "foobar-terraform-elb"
  }
}

文件名:main.tf。

module "north-virginia" {
  source = "./modules/production"
  region = "us-east-1"
  az = ["us-east-1a", "us-east-1b", "us-east-1c"]
}

module "oregon" {
  source = "./modules/production"
  region = "us-west-2"
  az = ["us-west-2a", "us-west-2b", "us-west-2c"]
}

文件名:./production/module/main.tf

variable region { }

variable az { 
type = "list" 
}

provider "aws" {
  region = "${var.region}"
  profile = "personal"
  shared_credentials_file = "~/.aws/credentials"
}

data "aws_caller_identity" "current" {}

output "account_id" {
  value = "${data.aws_caller_identity.current.account_id}"
}

目录树:

.
├── main.tf
└── modules
    ├── dev
    │   ├── ec2.tf
    │   ├── main.tf
    │   └── route53.tf
    ├── production
    │   ├── aws_elb.tf
    │   ├── aws_instance.tf
    │   ├── main.tf
    │   └── route53.tf
    └── qa
        ├── ec2.tf
        ├── main.tf
        └── route53.tf

1 个答案:

答案 0 :(得分:1)

在生产/alb_elb.tf中添加以下内容:

output "dns_name" {
value = "${aws_elb.web.dns_name}"
}

这将输出DNS名称。
在main.tf中,为route53创建一个单独的模块。
该模块应如下所示:

module "route53" {
  source = "./modules/route53"
  name1 = "${module.north-virginia.dns_name}"
  name2 = "${module.oregon.dns_name}"
}

您的route53.tf应该类似于:

variable "name1" ()
varibale "name2" ()
resource "aws_route53_record" "www1" {
  zone_id = "Zone-ID"
  name    = "www1"
  type    = "A"
  #ttl     = "5"

  failover_routing_policy {
    type = "PRIMARY"
  }

  set_identifier = "www1"
  #records        = ["${aws_elb.web.dns_name}"]
  alias {
    name                   = "${var.name1}"
    zone_id                = "${aws_elb.web.zone_id}"
    evaluate_target_health = true
  }

}
resource "aws_route53_record" "www2" {
  zone_id = "Zone-ID"
  name    = "www2"
  type    = "A"
  #ttl     = "5"

  failover_routing_policy {
    type = "SECONDARY"
  }

  set_identifier = "www2"
  #records        = ["${aws_elb.web.dns_name}"]
  alias {
    name                   = "${var.name2}"
    zone_id                = "${aws_elb.web.zone_id}"
    evaluate_target_health = true
  }
}