我使用 AWS 控制台设置了一个 Cloud Watch 警报,当 EC2 实例出现故障时,StatusCheck 会失败,该警报将向我的团队发送松弛通知并重新启动受影响的 EC2 实例。
以下是 AWS 控制台中的操作:
现在我想编写一个 terraform 模块来为我进行设置。这是我到目前为止所拥有的:
cloudwatch_metric_alarm/main.tf
locals {
name = format("%s_ec2-instance-down-alarm", var.name_prefix)
}
resource "aws_cloudwatch_metric_alarm" "ec2-instance-alarm" {
name = local.name
description = var.alarm_description
schedule_expression = var.schedule_expression
tags = merge(map("Name", local.name), var.extra_tags)
alarm_name = local.name
comparison_operator = var.comparison_operator
evaluation_periods = var.evaluation_periods
namespace = var.namespace
metric_name = var.metric_name
period = var.period
statistic = var.statistic
threshold = var.threshold
dimensions = {
InstanceId = var.instance_id
}
alarm_actions = [var.alarm_actions]
}
cloudwatch_metric_alarm/variables.tf
variable "extra_tags" {
type = map
default = {}
}
variable "name_prefix" {
type = string
}
variable "comparison_operator" {
type = string
default = "GreaterThanOrEqualToThreshold"
}
variable "evaluation_periods" {
type = number
default = 1
}
variable "namespace" {
type = string
default = "AWS/EC2"
}
variable "metric_name" {
type = string
default = "StatusCheckFailed"
}
variable "period" {
type = string
default = "60"
}
variable "statistic" {
type = string
default = "Average"
}
variable "threshold" {
type = string
default = "1"
}
variable "instance_id" {
type = string
}
variable "alarm_actions" {
type = list(string)
}
variable "alarm_description" {
type = string
default = "This alarm will send a slack notification if the bastion host because unresponsive."
}
我的问题是我在父模块中为 alarm_actions
传递什么。这是父 main.tf
的相关内容:
... other stuff ...
module "my-cloudwatch-metric-alarm" {
source = "./cloudwatch_metric_alarm"
alarm_actions = [???]
}
... other stuff ...
我不知道我应该传递什么作为alarm_actions
。
我应该传递什么?
谢谢!
更新并 self 注释。
我收到错误消息
Error: Creating metric alarm failed: ValidationError: Invalid use of EC2 action. An EC2 action can only be used if the alarm is monitoring an EC2 instance.
此错误的原因是我拼写错误的这一行InstanceId
:
InstatnceId = var.instance_id
最佳答案
首先,你的var.alarm_actions
已经是一个列表,所以在它周围添加括号,如 [var.alarm_actions]
将创建一个列表列表,这会导致错误。您只需将其直接传递给资源,例如:
alarm_actions = var.alarm_actions
对于需要在该列表中传递的第一个值,您需要要将通知发送到的 SNS 主题的 ARN。您可以在 AWS SNS 控制台中找到它。如果 Terraform 正在为您管理 SNS 主题,那么您应该已经可以访问 Terraform 中的主题 ARN。或者您可以通过 a datasource 查找它按主题名称。
对于第二个值,它是一个特殊的 ARN,指示 CloudWatch 重新启动正在监控的实例。它看起来像这样:arn:aws:automate:<region>:ec2:reboot
。例如,如果您的基础设施位于 us-east-1 中,则 ARN 将为 arn:aws:automate:us-east-1:ec2:reboot
。您可以使用 aws_region datasource 根据 Terraform 部署到的区域在 Terraform 代码中动态构建它。在 Terraform AWS 提供商中。
您的最终代码可能如下所示:
data "aws_sns_topic" "alerts" {
name = "Your Topic Name"
}
data "aws_region" "current" {}
module "my-cloudwatch-metric-alarm" {
source = "./cloudwatch_metric_alarm"
alarm_actions = [
data.aws_sns_topic.alerts.arn,
"arn:aws:automate:${data.aws_region.current.name}:ec2:reboot"
]
}
关于amazon-web-services - 如何使用 terraform 定义具有多个操作的 aws_cloudwatch_metric_alarm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67408318/