Google Monitoring Dashboards

Create/Update Dashboard

To import existing Monitoring Dashboard into source code and Terraform state, you can either follow below manual steps, or you can use the Bash script at tools/import_monitoring_dashboard.sh which executes those steps automatically.

The easiest way to create or update a Google Monitoring Dashboard is to make changes in Google Cloud Console UI, and then export the dashboard as YAML, and apply it via Terraform. This way all the dashboards will be persisted in the Terraform state and applied consistenly across staging and production environments.

Export as YAML

Using the commands below you can output the dashboard as YAML.

# Change dashboard name
DASHBOARD_NAME="ME Client"

# Filter dashboards by name and output dashboard ID
gcloud monitoring dashboads list \
  --project "ems-mobile-engage" \
  --filter "displayName='$DASHBOARD_NAME'" \
  --format "value(name)"

# Copy Dashboard YAML to clipboard
gcloud monitoring dashboards describe $DASHBOARD_ID | pbcopy

Prepare for Terraform

Remove following properties from the root of the exported YAML code:

  • etag

  • name

These two values are different based on the environment (staging/production), and cannot be updated anyhow, and thus shouldn’t be in the YAML code.

File Location

The prepared YAML should be placed in the gcp/resources/monitoring_dashboards.common folder.

Import into Terraform state

Since you already created a Monitoring Dashboard via Google Cloud Console UI, you must import the dashboard to Terraform state, so that Terraform knows that this is an existing dashboard, and doesn’t create a new one. To do so, run the following command:

# Change to the environment in which you created the dashboard in Google Cloud Console UI
environment="staging" # or "production"

cd "gcp/environments/$environment"

terraform init -upgrade -backend-config gcs.tfbackend

terraform import "module.monitoring_dashboards.google_monitoring_dashboard.dashboard[\"../../resources/monitoring_dashboards.common/$DASHHBOARD_FILENAME.yaml\"]" "$DASHBOARD_ID"

Update All Existing Dashboards

It can happen that Google Cloud slightly changes a format of dashboard YAML, and that we need to update all the dashboards in Terraform state to reflect this new format. In that case you can run the script from this repository:

./tools/update_monitoring_dashboards.sh

This will fetch dashboard YAML content for all existing dashboards and overwrite respective YAML files in the dashboards directory. Then you can commit the changes and apply them in the PR.