Using Fn::ImportValue in Dashboard Cloudwatch

Viewed 2602

I'm trying to setup a Dashboard in Cloudformation and want to use an Exported Value to make it dynamic: but it fails saying:

The dashboard body is invalid, there are 1 validation errors: [ { "message": "Invalid metric field type, only \"String\" type is allowed", "dataPath": "/widgets/0/properties/metrics/0/3" } ] (Service: AmazonCloudWatch; Status Code: 400; Error Code: InvalidParameterInput; Request ID: 01f3ebfa-d856-11e8-a2dc-dd8c90ad1109)

the code is:

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}"
                }
            },

it works until I introduce Fn::ImportValue; is there a chance to use Exported Values here?

Merci A

2 Answers

DashboardBody is a string, and the short form (without mapping) of !Sub will only resolve values on the template. If you want to use the Fn::ImportValue function, you have to use the key-value map syntax:

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"

had to do a little formatting but the answer helped, below is what worked for me

---
AWSTemplateFormatVersion: "2010-09-09"

Description: this template does CW

Resources:
  CPUResource:
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName: Mydashboard
      DashboardBody: !Sub
        - |
          {
            "widgets": [
              {
                "type": "metric",
                "x": 0,
                "y": 0,
                "width": 12,
                "height": 6,
                "properties": {
                  "metrics": [
                    [ "AWS/EC2", "CPUUtilization", "InstanceId", "${ec2Id}" ]
                  ],
                  "view": "timeSeries",
                  "stacked": false,
                  "region": "${AWS::Region}",
                  "stat": "Average",
                  "period": 300,
                  "title": "FE, BE, DB CPUUtilization ${ec2Id}"
                }
              }
            ]
          }
        - ec2Id:
            'Fn::ImportValue': "vmId"
Related