Create Terraform Cloudwatch Dashboards dynamically

0 votes

Overview

Currently, dashboards are being deployed via Terraform using values from a dictionary in locals.tf:

resource "aws_cloudwatch_dashboard" "my_alb" {
  for_each       = local.env_mapping[var.env]
  dashboard_name = "${each.key}_alb_web_operational"
  dashboard_body = templatefile("templates/alb_ops.tpl", {
    environment = each.value.env,
    account     = each.value.account,
    region      = each.value.region,
    alb         = each.value.alb
    tg          = each.value.alb_tg
  }

This leads to fragility because the values of AWS infrastructure resources like the ALB and ALB target group are hard coded. Sometimes when applying updates AWS resources are destroyed and recreated.

Question

What's the best approach to get these values dynamically? For example, this could be achieved by writing a Python/Boto3 Lambda, which looks up these values and then passes them to Terraform as env variables. Are there any other recommended ways to achieve the same?

Jan 24, 2023 in AWS by Tejashwini
• 3,820 points
888 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To dynamically create CloudWatch dashboards in Terraform, you can use the aws_cloudwatch_dashboard resource in Terraform. You can define a Terraform template that creates the dashboard and its widgets, and use Terraform variables to pass in different values for each deployment.

Here's an example of how you can create a simple dashboard with a single line graph widget:



provider "aws" {
  region = "us-west-2"
}

variable "dashboard_name" {
  type = string
}

variable "widget_metric_name" {
  type = string
}

resource "aws_cloudwatch_dashboard" "example" {
  dashboard_name = var.dashboard_name

  dashboard_body = <<EOF
{
  "widgets": [
    {
      "type": "metric",
      "x": 0,
      "y": 0,
      "width": 12,
      "height": 6,
      "properties": {
        "metrics": [
          [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-049df61146f277901" ],
          [ ".", ".", ".", "i-0682af8ca95c71c99" ]
        ],
        "period": 300,
        "stat": "Average",
        "region": "us-west-2",
        "title": "${var.widget_metric_name}"
      }
    }
  ]
}
EOF
}

You can deploy this Terraform template by running terraform apply and passing in the desired values for the dashboard_name and widget_metric_name variables. For example:



javascriptCopy code

terraform apply -var 'dashboard_name=MyDashboard' -var 'widget_metric_name=EC2 CPU Utilization'

This will create a new CloudWatch dashboard with the specified name and a single line graph widget displaying the average CPU utilization for two EC2 instances. You can modify the Terraform template to include additional widgets and customize the dashboard as desired.

answered Feb 13, 2023 by Ashwini
• 5,430 points

edited Mar 5

Related Questions In AWS

0 votes
1 answer

Create Snapshot of EBS and attach to EC2 using Terraform

You can also create a snapshot using ...READ MORE

answered Oct 29, 2018 in AWS by Priyaj
• 58,020 points
2,352 views
+1 vote
1 answer
+3 votes
3 answers

Terraform AWS Cognito App Client

This feature is not currently supported by ...READ MORE

answered Aug 28, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
3,959 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,621 views
0 votes
1 answer

Event for CodeBuild CloudWatch on my Project

In order to create CWE rule for ...READ MORE

answered May 25, 2018 in AWS by Cloud gunner
• 4,670 points
1,409 views
+1 vote
2 answers

Want my AWS s3 Bucket to read Name from CloudWatch Event

CloudTrail events for S3 bucket level operations ...READ MORE

answered May 28, 2018 in AWS by Cloud gunner
• 4,670 points
2,251 views
0 votes
1 answer

Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

Python String and Integer concatenation >>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum)) arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0 >>> print("arn:aws:swf:us-east-2:" ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 58,020 points
1,711 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP