I have created a Cloudwatch dashboard via AWS Console. Now I would like to convert it to its Cloudformation template for future deployment.
Is there any solution?
In this case there's not much to convert, because the dashboard definition in CloudFormation AWS::CloudWatch::Dashboard
must appear, not defined in YAML or even in JSON, but represented as a literal string representation of the JSON (i.e. a literal JSON representation embedded in the CloudFormation template, not part of the CloudFormation template).You can see this in one of the official CloudFormation templates:
BasicDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: Dashboard1
DashboardBody: '{"widgets":[{"type":"metric","x":0,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-012345"]],"period":300,"stat":"Average","region":"us-east-1","title":"EC2 Instance CPU"}},{"type":"text","x":0,"y":7,"width":3,"height":3,"properties":{"markdown":"Hello world"}}]}'
Thus you simply need to copy and paste the JSON source code of your existing dashboard into the CloudFormation template and add the minimal AWS::CloudWatch::Dashboard
and dashboard name boilerplate.
Tip: You can can paste the multiple lines of JSON as-is using a YAML multiline string, with the following syntax:
BasicDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: Dashboard1
DashboardBody: >-
{
"widgets": [
{
…