求基于延迟的AWS Route53记录集Terraform示例及所有可传属性(含别名健康检查)
Hey there! Let's walk through exactly what you need: a complete Terraform example for AWS Route53 latency-based records, plus a full breakdown of every configurable attribute—including the critical evaluate_target_health setting for alias records.
Terraform配置示例:Route53延迟策略记录集
This example sets up a hosted zone, a health check for an EC2 instance, two latency-routed records (one pointing to EC2, another as an alias to CloudFront with health evaluation), and a sample CloudFront distribution for context.
# 创建Route53托管区 resource "aws_route53_zone" "example" { name = "example.com" } # 为EC2实例创建HTTP健康检查 resource "aws_route53_health_check" "ec2_instance" { fqdn = "server.example.com" port = 80 type = "HTTP" failure_threshold = 3 # 连续3次失败标记为不健康 request_interval = 30 # 每30秒检查一次 } # 延迟策略:指向EC2实例的A记录(关联健康检查) resource "aws_route53_record" "latency_ec2_us_east" { zone_id = aws_route53_zone.example.zone_id name = "app.example.com" type = "A" ttl = "300" records = ["10.0.0.10"] # 替换为你的EC2公网IP # 启用延迟路由策略,指定对应AWS区域 latency_routing_policy { region = "us-east-1" } # 关联健康检查:健康检查失败时,该记录会被Route53排除 health_check_id = aws_route53_health_check.ec2_instance.id } # 延迟策略:指向CloudFront的别名记录(开启目标健康评估) resource "aws_route53_record" "latency_cloudfront_eu_west" { zone_id = aws_route53_zone.example.zone_id name = "app.example.com" type = "A" # 别名记录配置(适用于A/AAAA类型) alias { name = aws_cloudfront_distribution.example.domain_name zone_id = aws_cloudfront_distribution.example.hosted_zone_id # 开启目标健康评估:Route53会校验CloudFront分发的健康状态 evaluate_target_health = true } # 延迟路由策略配置 latency_routing_policy { region = "eu-west-1" } } # 可选:示例CloudFront分发(用于上面的别名记录) resource "aws_cloudfront_distribution" "example" { enabled = true default_root_object = "index.html" origin { domain_name = aws_s3_bucket.example.bucket_regional_domain_name origin_id = "S3-example" } default_cache_behavior { allowed_methods = ["GET", "HEAD"] cached_methods = ["GET", "HEAD"] target_origin_id = "S3-example" forwarded_values { query_string = false cookies { forward = "none" } } viewer_protocol_policy = "redirect-to-https" min_ttl = 0 default_ttl = 3600 max_ttl = 86400 } price_class = "PriceClass_100" viewer_certificate { cloudfront_default_certificate = true } } # 可选:示例S3桶(用于CloudFront原点) resource "aws_s3_bucket" "example" { bucket = "example-static-content-bucket" }
所有可配置属性详解
Below is a breakdown of every attribute you can set for latency-based Route53 records, including alias-specific health check settings:
通用属性(所有Route53记录集适用)
zone_id:必填,Route53托管区的唯一ID(从aws_route53_zone资源的zone_id属性获取)name:必填,记录的完整域名(比如app.example.com)type:必填,DNS记录类型(支持A,AAAA,CNAME,MX,TXT等,延迟策略最常用A/AAAA)ttl:可选(仅非别名记录需要),DNS缓存时间(单位:秒,比如300表示5分钟)records:可选(仅非别名记录需要),目标值列表(比如IP地址、CNAME域名)health_check_id:可选(仅非别名记录可用),关联的Route53健康检查ID,健康检查失败时该记录会被Route53从解析结果中移除
延迟路由策略专属属性
latency_routing_policy块:必填,用于启用延迟路由逻辑region:必填,AWS区域代码(比如us-east-1,ap-southeast-1),Route53会根据用户的地理位置返回对应区域延迟最低的记录
别名记录专属属性(仅A/AAAA类型可用)
alias块:必填(如果是别名记录),配置目标资源的别名指向name:必填,目标资源的域名(比如CloudFront分发域名、ELB负载均衡器域名)zone_id:必填,目标资源所在的托管区ID(比如CloudFront的固定托管区ID是Z2FDTNDATAQYW2,ELB/ALB的ID可从对应资源属性获取)evaluate_target_health:可选(默认false),布尔值,开启后Route53会自动检查目标资源的健康状态:- 对于CloudFront:校验分发的原点健康检查状态
- 对于ELB/ALB:校验负载均衡器的目标组健康状态
- 对于S3静态网站:校验桶的可用性
- 开启后,若目标资源不健康,该别名记录会被Route53排除出解析结果
内容的提问来源于stack exchange,提问作者user8845768
相关产品推荐
相关产品推荐

