我正在使用 Terraform 创建一组 CloudWatch 警报,如下所示:
resource "aws_cloudwatch_metric_alarm" "lambda_concurrent_executions" {
for_each = toset(var.function_names)
alarm_name = "${each.key}-concurrent-executions"
alarm_description = "The number of function instances that are processing events."
namespace = "AWS/Lambda"
metric_name = "ConcurrentExecutions"
comparison_operator = "GreaterThanOrEqualToThreshold"
statistic = "Sum"
period = "60"
evaluation_periods = "1"
threshold = "100"
treat_missing_data = "ignore"
dimensions = {
FunctionName = each.key
}
alarm_actions = var.alarm_actions
ok_actions = var.alarm_actions
}
所以它使用for_each
函数名称列表。
接下来,我需要创建一个 CloudWatch 仪表板。它显然是通过提供具有相关名称(文档)的 JSON 来创建的:
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "my-dashboard"
dashboard_body = <<EOF
{
"widgets": [
{
"type": "alarm",
"x": 0,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"title": "Alarms",
"alarms": [
"arn:aws:cloudwatch:eu-west-1:167170067742:alarm:mylambda-concurrent-executions",
"arn:aws:cloudwatch:eu-west-1:167170067742:alarm:mylambda-concurrent-executions-2",
"arn:aws:cloudwatch:eu-west-1:167170067742:alarm:mylambda-concurrent-executions-3",
"arn:aws:cloudwatch:eu-west-1:167170067742:alarm:mylambda-concurrent-executions-4",
/// ... and so on
}
EOF
}
如何在变量中收集创建的警报 ARN 并将其插入仪表板 JSON?