3

I am trying to create a CloudFormation template to create a CloudWatch dashboard. Following is the template code -

Parameters:
    MyEnvironment:
        Type: String
        Default: "do"
        Description: "Specifies the environment of the platform."
Resources:
    MyServiceDashboard:
        Type: AWS::CloudWatch::Dashboard
        Properties:
            DashboardName: "Test-My-Dashboard"
            DashboardBody: >
                {
                    "widgets": [
                        {
                            "type": "metric",
                            "x": 15,
                            "y": 18,
                            "width": 6,
                            "height": 6,
                            "properties": {
                                "view": "timeSeries",
                                "stacked": false,
                                "metrics": [
                                    [ "AWS/Kinesis", "GetRecords.IteratorAgeMilliseconds", "StreamName", 
"${MyEnvironment}-my-data-out"
                                    ]...

I am trying to use MyEnvironment parameter which will be provided when I will actually use this template to create a stack.

Problem is stack/dashbaord gets created using this but the parameter value is not getting used in the Dashboard metric instead it uses value as "${MyEnvironment}-my-data-out" instead of "Dev-my-data-out" assuming I have provided "MyEnvironment" value as "Dev"

I tried the method specified in this link - Use Pseudo Variables in Cloudwatch Dashboard Template (Cloudformation) but CloudFormation shows invalid template when using Sub >- keyword.

Sachin Gorade
  • 1,427
  • 12
  • 32
  • Check this out: https://stackoverflow.com/questions/56616110/setting-the-widget-for-cloudwatch-dashboard-getting-the-error-the-dashboard-bo/56618152#56618152 – Dejan Peretin Aug 02 '19 at 04:29

2 Answers2

5

After trying various options, found the solution, important point is to use !Sub function otherwise parameter values are not replaced in the JSON string.

        DashboardName: "Test-My-Dashboard"
        DashboardBody: !Sub '{
                "widgets": [

Notice the !Sub function in the DashboardBody attribute. After this you can refer to parameter values by using ${PARAMETER_NAME} format.

!Sub function documentation

Sachin Gorade
  • 1,427
  • 12
  • 32
-1

"MyEnvironment" is not a pseudo variable. It is for AWS variables only like region. You need to use 'Ref' if you want to parameterize.

    Parameters:
MyEnvironment:
    Type: String
    Default: "do"
    Description: "Specifies the environment of the platform."
    Resources:
MyServiceDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties:
        DashboardName: "Test-My-Dashboard"
        DashboardBody: >
            {
                "widgets": [
                    {
                        "type": "metric",
                        "x": 15,
                        "y": 18,
                        "width": 6,
                        "height": 6,
                        "properties": {
                            "view": "timeSeries",
                            "stacked": false,
                            "metrics": [
                                [ "AWS/Kinesis", "GetRecords.IteratorAgeMilliseconds", "StreamName",  {"Ref" : "MyEnvironment"}

                              ]... 

Pass "Dev-my-data-out" in the MyEnvironment parameter directly to avoid complication.

Parul Singh
  • 363
  • 3
  • 11
  • This does not work for me, Dashboard creation fails using this, and if I use escaped string then dashboard creation succeeds but uses the escaped string instead of parameter value. – Sachin Gorade Aug 02 '19 at 03:35