我目前正在设置AWS CloudWatch Alarm,使用Terraform检测我的服务器的运行状况。使用AWS Route 53运行状况检查检查运行状况。我的.tf
文件是:
resource "aws_cloudwatch_metric_alarm" "val1-alarm" {
alarm_name = "val-alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "HealthCheckStatus"
namespace = "AWS/Route53"
period = "60"
statistic = "Minimum"
threshold = "0"
dimensions {
HealthCheckId = "${aws_route53_health_check.val1-hc.id}"
}
alarm_description = "This metric monitor whether the server is down or not."
insufficient_data_actions = []
}
resource "aws_route53_health_check" "val1-hc" {
fqdn = "${aws_route53_record.val1-record.name}"
port = 27017
type = "TCP"
failure_threshold = "3"
request_interval = "30"
measure_latency = 1
cloudwatch_alarm_name = "${aws_cloudwatch_metric_alarm.val1-alarm.alarm_name}"
cloudwatch_alarm_region = "eu-central-1"
}
申请时我有这个错误:
Cycle: aws_route53_health_check.val1-hc, aws_cloudwatch_metric_alarm.val1-alarm
循环意味着每个资源调用另一个资源。当我尝试从健康检查中删除cloudwatch_alarm_name
和cloudwatch_alarm_region
时,terraform错误提示我需要这两个参数(即使doc指定这两个是可选的)。如何解决?
任何帮助或建议都非常感谢!
你不能从A
和B
的B
引用A
。
从aws_cloudwatch_metric_alarm.val1-alarm
中删除引用,例如:
resource "aws_cloudwatch_metric_alarm" "val1-alarm" {
alarm_name = "val-alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "HealthCheckStatus"
namespace = "AWS/Route53"
period = "60"
statistic = "Minimum"
threshold = "0"
alarm_description = "This metric monitor whether the server is down or not."
insufficient_data_actions = []
}
resource "aws_route53_health_check" "val1-hc" {
fqdn = "${aws_route53_record.val1-record.name}"
port = 27017
type = "TCP"
failure_threshold = "3"
request_interval = "30"
measure_latency = 1
cloudwatch_alarm_name = "${aws_cloudwatch_metric_alarm.val1-alarm.alarm_name}"
cloudwatch_alarm_region = "eu-central-1"
}
并非您需要在美国东部(弗吉尼亚州北部)拥有您的资源,因为:
如果您选择任何其他区域作为当前区域,则无法使用Amazon Route 53指标。
资料来源:Monitoring Health Check Status and Getting Notifications。
我设法使用eu-west-1
与此模块一起工作:
variable "environment" {}
variable "domain_name" {}
variable "resource_path" {}
provider "aws" {
alias = "use1"
region = "us-east-1"
}
resource "aws_route53_health_check" "health_check" {
fqdn = "${var.domain_name}"
port = 443
type = "HTTPS"
resource_path = "${var.resource_path}"
measure_latency = true
request_interval = 30
failure_threshold = 3
tags = {
Name = "${var.environment}"
Origin = "terraform"
Environment = "${var.environment}"
}
}
resource "aws_sns_topic" "topic" {
name = "${var.environment}-healthcheck"
provider = "aws.use1"
}
resource "aws_cloudwatch_metric_alarm" "metric_alarm" {
provider = "aws.use1"
alarm_name = "${var.environment}-alarm-health-check"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "HealthCheckStatus"
namespace = "AWS/Route53"
period = "60"
statistic = "Minimum"
threshold = "1"
insufficient_data_actions = []
alarm_actions = ["${aws_sns_topic.topic.arn}"]
alarm_description = "Send an alarm if ${var.environment} is down"
dimensions {
HealthCheckId = "${aws_route53_health_check.health_check.id}"
}
}
在Terraform 0.9.3上,我必须执行相反操作并从aws_route53_health_check资源中删除cloudwatch_alarm_name和cloudwatch_alarm_region,以获取连接到运行状况检查的警报。感觉倒退了。 HealthCheckId维度足以将它们连接在一起。
resource "aws_cloudwatch_metric_alarm" "val1-alarm" {
alarm_name = "val-alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "HealthCheckStatus"
namespace = "AWS/Route53"
period = "60"
statistic = "Minimum"
threshold = "0"
dimensions {
HealthCheckId = "${aws_route53_health_check.val1-hc.id}"
}
alarm_description = "This metric monitor whether the server is down or not."
insufficient_data_actions = []
}
resource "aws_route53_health_check" "val1-hc" {
fqdn = "${aws_route53_record.val1-record.name}"
port = 27017
type = "TCP"
failure_threshold = "3"
request_interval = "30"
measure_latency = 1
}
namespace =“AWS / Route53”