CloudWatch dashboard creation with CloudFormation

0

Hi

Using CloudFormation, is it possible to create an 'empty' CloudWatch dashboard with one stack and then have other stacks add metrics to that dashboard? I'm able to create the empty dashboard with one stack:

Stack A:
  FooDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardBody: '{"widgets":[]}'
      DashboardName: Foo-Dashboard

And then in another stack, I can import that dashboard and try and add metrics:

Stack B:
  FooServiceDashboardMetrics:
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName:
        Fn::ImportValue: StackA-FooDashboard
      DashboardBody: {"widgets":[{"type":"metric","x":0,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/Lambda","ConcurrentExecutions","FunctionName","foo-service-LambdaFunctionEvent"]],"view":"timeSeries","stacked":false,"region":"us-east-1","title":"Foo Service Lambda"}}]}

However when I deploy Stack B, I get an error:

StackA-FooDashboard already exists in stack arn:aws:cloudformation:us-east-1:1234567890:stack/StackA/8507-0e97b0c8

Is this possible? Thanks!

1 Answer
0

Creating an empty CloudWatch dashboard with one stack and then adding metrics to it from other stacks is not directly possible in the way you've described. The issue you're encountering is because CloudFormation treats the AWS::CloudWatch::Dashboard resource as a unique entity, and you can't have two resources with the same logical ID (in this case, the dashboard name) across different stacks.

However, there are alternative approaches you can consider to achieve a similar result:

  1. Use a nested stack approach: Create a parent stack that defines the dashboard, and then use nested stacks to add widgets to it. This way, you're working within a single stack, but can still modularize your configuration.

  2. Use a custom resource: Create a Lambda function as a custom resource in CloudFormation. This function can use the PutDashboard API to update the existing dashboard with new widgets. This approach allows you to add widgets from different stacks without creating a new dashboard resource.

  3. Use AWS Systems Manager Parameter Store: Store the dashboard JSON in a parameter. Each stack can then retrieve the current dashboard configuration, modify it to add its widgets, and then update the dashboard using a custom resource.

  4. Consolidate dashboard creation: If possible, consider creating the dashboard with all necessary widgets in a single stack. This approach is simpler but may not be feasible if you need to separate concerns across multiple stacks.

Remember, CloudWatch dashboards are global resources and not region-specific. When updating a dashboard, you need to include all existing widgets along with any new ones you want to add, as the update operation replaces the entire dashboard content.

While your original approach of creating separate dashboard resources in different stacks isn't possible, these alternative methods can help you achieve the desired outcome of having multiple stacks contribute to a single dashboard.
Sources
AWS::CloudWatch::Dashboard - AWS CloudFormation
Creating a CloudWatch dashboard - Amazon CloudWatch
PutDashboard - Amazon CloudWatch

profile picture
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions