Reputation: 1467
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.
Upvotes: 4
Views: 5222
Reputation: 1467
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.
Upvotes: 6
Reputation: 503
"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.
Upvotes: -1