我尝试使用 terraform 为 AWS 上的域创建故障转移策略,问题是它只在 route53 资源后面附加一个 elb dns 名称作为主要和辅助。我实际上希望它使用北弗吉尼亚易北河名称作为主要名称,俄勒冈州作为次要名称。
这是一个多区域架构,我已经使用相同的源目录创建了模块。
文件名: 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
在生产/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
}
}