在资讯主页Cloudwatch中使用Fn :: ImportValue

时间:2018-10-25 13:05:48

标签: amazon-cloudformation

我正在尝试在Cloudformation中设置仪表板,并希望使用Exported Value使其动态化:但是失败了:

仪表板主体无效,存在1个验证错误:[{“ message”:“无效的度量标准字段类型,仅允许\” String \“类型”,“ dataPath”:“ / widgets / 0 / properties / metrics / 0/3“}](服务:AmazonCloudWatch;状态代码:400;错误代码:InvalidParameterInput;请求ID:01f3ebfa-d856-11e8-a2dc-dd8c90ad1109)

代码是:

Resources:
 NATDashboard:
 Type: AWS::CloudWatch::Dashboard
 Properties:
  DashboardName: NAT-Dashboard
  DashboardBody: !Sub |
    {
        "widgets": [
            {
                "type": "metric",
                "x": 0,
                "y": 0,
                "width": 3,
                "height": 9,
                "properties": {
                    "metrics": [
                        [ "AWS/NATGateway", "ActiveConnectionCount", "NatGatewayId", {"Fn::ImportValue": {"Fn::Sub": "${EnvironmentName}-NATGateway1Id"}}, { "stat": "Maximum" } ],
                        [ "...", "nat-057236a417c993781", { "stat": "Maximum" } ],
                        [ "...", "nat-09b8d012addc7e0fe", { "stat": "Maximum" } ]
                    ],
                    "view": "singleValue",
                    "region": "${AWS::Region}"
                }
            },

它起作用直到我介绍Fn :: ImportValue;有机会在这里使用导出的值吗?

Merci A

1 个答案:

答案 0 :(得分:2)

DashboardBody是一个字符串,!Sub的缩写形式(无映射)将为only resolve values on the template。如果要使用Fn::ImportValue函数,则必须使用键值映射语法:

Resources:
 NATDashboard:
 Type: 'AWS::CloudWatch::Dashboard'
 Properties:
  DashboardName: 'NAT-Dashboard'
  DashboardBody: !Sub 
    - |
        {
            "widgets": [
                {
                    "type": "metric",
                    "x": 0,
                    "y": 0,
                    "width": 3,
                    "height": 9,
                    "properties": {
                        "metrics": [
                            [ "AWS/NATGateway", "ActiveConnectionCount", "NatGatewayId", "${NatGatewayId}", { "stat": "Maximum" } ],
                            [ "...", "nat-057236a417c993781", { "stat": "Maximum" } ],
                            [ "...", "nat-09b8d012addc7e0fe", { "stat": "Maximum" } ]
                        ],
                        "view": "singleValue",
                        "region": "${AWS::Region}"
                    }
                }
            ]
        }
    - NatGatewayId: 
        'Fn::ImportValue': !Sub "${EnvironmentName}-NATGateway1Id"