【问题标题】:cloudformation templates for alaram dashboard用于 alaram 仪表板的 cloudformation 模板
【发布时间】:2021-08-02 02:09:50
【问题描述】:

我已经为 CloudWatch 控制面板创建了 CloudFormation 模板,我正在尝试使用 AlarmName 参数,但 Ref 值没有通过,并且在下面的 AWS CloudFormation 中出现错误。

The field DashboardBody must be a valid JSON object (Service: AmazonCloudWatch; Status Code: 400; Error Code: InvalidParameterInput;
Request ID: e88b103e-8fba-42bc-b825-ed0e4bd6749a; Proxy: null)

模板:

AWSTemplateFormatVersion: 2010-09-09
Description: "AWS CloudFormation Sample Template for Cloudwatch alert and Dashboard configuration."
Parameters: 
    AlarmNameForCPU: 
    Default: Service_CpuUtilization
    Description: AlarmName
    MaxLength: "99"
    MinLength: "1"
    Type: String
    AlarmNameForMemory: 
    Default: Service_MemoryUtilization
    Description: AlarmName
    MaxLength: "99"
    MinLength: "1"
    Type: String
    DashboardName: 
    Default: ServiceDashboard
    Description: Dashboard
    MaxLength: "99"
    MinLength: "1"
    Type: String
    DashboardTitleCPU: 
    Default: CpuUtilization
    Description: DashboardTitle
    MaxLength: "99"
    MinLength: "1"
    Type: String
    DashboardTitleMemory: 
    Default: MemoryUtilization
    Description: DashboardTitle
    MaxLength: "99"
    MinLength: "1"
    Type: String
Resources:
    Dashboard:
    Type: "AWS::CloudWatch::Dashboard"
    Properties:
        DashboardName: !Ref DashboardName
        DashboardBody: '{
    "widgets":[
        {
            "height": 3,
            "width": 9,
            "y": 0,
            "x": 0,
            "type": "metric",
            "properties": {
                "title": "{"Ref DashboardTitleCPU"}",
                "annotations": {
                    "alarms": [
                        "arn:aws:cloudwatch:us-east-1:468491150671:alarm: {"Ref" : "AlarmNameForCPU"}"
                    ]
                },
                "view": "singleValue"
            }
        },
        {
            "height": 3,
            "width": 9,
            "y": 0,
            "x": 9,
            "type": "metric",
            "properties": {
                "title": "{"Ref DashboardTitleMemory"}",
                "annotations": {
                    "alarms": [
                        "arn:aws:cloudwatch:us-east-1:468491150671:alarm: {"Ref AlarmNameForMemory"}"
                    ]
                },
                "view": "singleValue"
            }
        }
    ]
}}}}]}'

【问题讨论】:

    标签: amazon-cloudformation aws-cloudformation-custom-resource


    【解决方案1】:

    您需要为此使用Sub function。

    Resources:
      Dashboard:
        Type: "AWS::CloudWatch::Dashboard"
        Properties:
          DashboardName: !Ref DashboardName
          DashboardBody: !Sub |
    '{
            "widgets":[
                {
                    "height": 3,
                    "width": 9,
                    "y": 0,
                    "x": 0,
                    "type": "metric",
                    "properties": {
                        "title": "${DashboardTitleCPU}",
                        "annotations": {
                            "alarms": [
                                "arn:aws:cloudwatch:us-east-1:468491150671:alarm:${AlarmNameForCPU}"
                            ]
                        },
                        "view": "singleValue"
                    }
                },
                {
                    "height": 3,
                    "width": 9,
                    "y": 0,
                    "x": 9,
                    "type": "metric",
                    "properties": {
                        "title": "${DashboardTitleMemory}",
                        "annotations": {
                            "alarms": [
                                "arn:aws:cloudwatch:us-east-1:468491150671:alarm:${AlarmNameForMemory}"
                            ]
                        },
                        "view": "singleValue"
                    }
                }
            ]
        }'
    

    【讨论】:

    • 我使用了 Sub 函数,但得到了同样的错误。 DashboardBody 字段必须是有效的 JSON 对象(服务:AmazonCloudWatch;状态代码:400;错误代码:InvalidParameterInput;属性验证失败:[属性值{/DashboardBody} 与类型 {String} 不匹配]
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 2021-07-07
    • 2020-06-23
    • 2019-11-18
    • 1970-01-01
    • 2022-08-03
    • 2022-01-27
    • 2019-04-07
    相关资源
    最近更新 更多