微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于 alaram 仪表板的 cloudformation 模板

如何解决用于 alaram 仪表板的 cloudformation 模板

我已经为 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,"x": 9,"properties": {
                "title": "{"Ref DashboardTitleMemory"}","annotations": {
                    "alarms": [
                        "arn:aws:cloudwatch:us-east-1:468491150671:alarm: {"Ref AlarmNameForMemory"}"
                    ]
                },"view": "singleValue"
            }
        }
    ]
}}}}]}'

解决方法

您需要为此使用 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,"x": 9,"properties": {
                    "title": "${DashboardTitleMemory}","annotations": {
                        "alarms": [
                            "arn:aws:cloudwatch:us-east-1:468491150671:alarm:${AlarmNameForMemory}"
                        ]
                    },"view": "singleValue"
                }
            }
        ]
    }'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。