循环地形

时间:2020-09-05 17:08:14

标签: amazon-web-services templates terraform amazon-cloudwatch dashboard

我一直在学习terraform,并且一直在使用仪表板。

我有以下文件会生成一个仪表板。

resource "aws_cloudwatch_dashboard" "main" {
  dashboard_name = "sample_dashboard"
  dashboard_body = <<EOF
{
  "widgets": [
    ${templatefile("${path.module}/cpu.tmpl", { ids = aws_instance.web[*].id })},
    ${templatefile("${path.module}/network.tmpl", { ids = aws_instance.web[*].id })}
  ]
}
EOF
}

这是cpu模板文件。

{
  "type": "metric",
  "x": 0,
  "y": 0,
  "width": 12,
  "height": 6,
  "properties": {
    "metrics": ${jsonencode([for id in ids : ["AWS/EC2","CPUUtilization","InstanceId", "${id}"]])},
    "period": 300,
    "stat": "Average",
    "region": "us-east-1",
    "title": "EC2 Instance CPU"
  }
}

这里有网络模板文件。

{
  "type": "metric",
  "x": 12,
  "y": 0,
  "width": 12,
  "height": 6,
  "properties": {
    "metrics": ${jsonencode([for id in ids :
                  ["AWS/EC2", "NetworkIn", "InstanceId", "${id}"]
                ])},
    "period": 300,
    "stat": "Average",
    "region": "us-east-1",
    "title": "EC2 Instance Network"
  }
}

一切正常,我得到以下信息中心。 dashboards

我遇到的问题是尝试在for循环中添加另一个指标时出现错误。

{
  "type": "metric",
  "x": 12,
  "y": 0,
  "width": 12,
  "height": 6,
  "properties": {
    "metrics": ${jsonencode([for id in ids :
                  ["AWS/EC2", "NetworkIn", "InstanceId", "${id}"],
                  ["AWS/EC2", "NetworkOut", "InstanceId", "${id}"]
                ])},
    "period": 300,
    "stat": "Average",
    "region": "us-east-1",
    "title": "EC2 Instance Network"
  }
}

我收到以下错误消息。

对函数“ templatefile”的调用失败:./network.tmpl:9,70-71: 无效的“ for”表达式; “ for”结束后的多余字符 表达..

一如既往,在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

解决该问题的一种方法是concat您的指标:

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<div class="container">
            <div class="row">
                <div class="col-sm-12">
                    <nav class="main-menu">
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
                                <span class="sr-only">Toggle navigation</span>
                                <span class="icon-bar top-bar"></span>
                                <span class="icon-bar middle-bar"></span>
                                <span class="icon-bar bottom-bar"></span>
                            </button>
                            <a class="navbar-brand" href="index.html">
                                <img class="logo logo-white" src="images/logo.png" alt="logo">
                                <img class="logo logo-color" src="images/logo-color.png" alt="logo">
                            </a>
                        </div><!--End navbar-header -->
                        <div id="navbar" class="navbar-collapse collapse">
                            <ul class="nav navbar-nav navbar-right">
                                <li><a class="active" href="#home">Home</a></li>
                                <li><a href="#about">About</a></li>
                                <li><a href="#service">Service</a></li>
                                <li><a href="#portfolio">Portfolio</a></li>
                                <li><a href="#pricingtable">Pricing</a></li>
                                <li><a href="#blog">Blog</a></li>
                                <li><a class="last" href="#contact">Contact</a></li>
                            </ul>
                        </div><!--End navbar -->
                    </nav>
                </div><!--End col -->
            </div><!--End row -->
        </div><!--End container -->