我该如何做尺寸块“动态”?
Posted
技术标签:
【中文标题】我该如何做尺寸块“动态”?【英文标题】:How can I do dimensions block "dynamic"? 【发布时间】:2021-02-15 06:57:07 【问题描述】:想知道如何使 dimensions
阻止 aws_cloudwatch_metric_alarm 资源“动态”。
到目前为止,有一些我确定不会工作的代码......但我想问一下应该如何编写它来实现目标。
locals
backend_tg_name = data.terraform_remote_state.network.outputs.backend_tg_name
frontend_tg_name = data.terraform_remote_state.network.outputs.frontend_tg_name
webadmin_tg_name = data.terraform_remote_state.network.outputs.webadmin_tg_name
dimensions = [
LoadBalancer = data.terraform_remote_state.network.outputs.alb_suffix
TargetGroup = data.terraform_remote_state.network.outputs.backend_tg_suffix
,
LoadBalancer = data.terraform_remote_state.network.outputs.alb_suffix
TargetGroup = data.terraform_remote_state.network.outputs.frontend_tg_name
,
LoadBalancer = data.terraform_remote_state.network.outputs.alb_suffix
TargetGroup = data.terraform_remote_state.network.outputs.webadmin_tg_suffix
]
resource "aws_cloudwatch_metric_alarm" "httpcode_target_5xx_count"
for_each =
backend_tg_name = local.backend_tg_name
frontend_tg_name = local.frontend_tg_name
webadmin_tg_name = local.webadmin_tg_name
alarm_name = format("ALB: High amount of 5XX errors on target group %s", each.value)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.tg_evaluation_periods
metric_name = "HTTP_Code_Target_5XX_Count"
namespace = "AWS/ApplicationELB"
period = var.tg_period
statistic = "Sum"
threshold = var.tg_5xx_threshhold
alarm_description = "Average API 5XX target group error code count is too high"
alarm_actions = aws_sns_topic.infra_monitoring.arn
ok_actions = aws_sns_topic.infra_monitoring.arn
treat_missing_data = "notBreaching"
dimensions =
"LoadBalancer" = ???
"TargetGroup" = ???
如何更改尺寸和/或本地块?我想迭代三次并为同一个 ALB 后面的三个不同目标组创建三个相同的警报。 请帮忙。
【问题讨论】:
【参考方案1】:这似乎是有效的,希望它会有所帮助:
locals
alb_suffix = data.terraform_remote_state.network.outputs.alb_suffix
tg_alarms =
"backend_tg" =
tg_name = data.terraform_remote_state.network.outputs.backend_tg_name
tg_suffix = data.terraform_remote_state.network.outputs.backend_tg_suffix
,
"frontend_tg" =
tg_name = data.terraform_remote_state.network.outputs.frontend_tg_name
tg_suffix = data.terraform_remote_state.network.outputs.frontend_tg_name
,
"webadmin_tg" =
tg_name = data.terraform_remote_state.network.outputs.webadmin_tg_name
tg_suffix = data.terraform_remote_state.network.outputs.webadmin_tg_suffix
resource "aws_cloudwatch_metric_alarm" "httpcode_target_5xx_count"
for_each = local.tg_alarms
alarm_name = format("ALB: High amount of 5XX errors on target group %s", each.value["tg_name"])
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.tg_evaluation_periods
datapoints_to_alarm = var.tg_datapoints_to_alarm
metric_name = "HTTPCode_Target_5XX_Count"
namespace = "AWS/ApplicationELB"
period = var.tg_period
statistic = "Sum"
threshold = var.tg_5xx_threshold
alarm_description = "Average API 5XX target group error code count is too high"
alarm_actions = [aws_sns_topic.infra_monitoring.arn]
ok_actions = [aws_sns_topic.infra_monitoring.arn]
treat_missing_data = "notBreaching"
dimensions =
"LoadBalancer" = local.alb_suffix
"TargetGroup" = each.value["tg_suffix"]
【讨论】:
以上是关于我该如何做尺寸块“动态”?的主要内容,如果未能解决你的问题,请参考以下文章