grafana / terraform-provider-grafana

Terraform Grafana provider
https://www.terraform.io/docs/providers/grafana/
Mozilla Public License 2.0
430 stars 229 forks source link

[Bug]: grafana_data_srouce of type prometheus not saved properly in Amazon Managed Grafana #1673

Closed haoliangyu closed 3 months ago

haoliangyu commented 3 months ago

Terraform Version

1.9.1

Terraform Grafana Provider Version

3.0.0

Grafana Version

10.4

Affected Resource(s)

Terraform Configuration Files

provider "grafana" {
  url  = "https://${var.grafana_endpoint}"
  auth = var.grafana_api_key
}

resource "grafana_data_source" "hub_grafana_amp" {
  type       = "prometheus"
  name       = "prometheus-${var.region}"
  is_default = false
  url        = aws_prometheus_workspace.prometheus_observability.prometheus_endpoint

  json_data_encoded = jsonencode({
    "httpMethod"    = "GET"
    "authType"      = "sigv4"
    "sigV4Auth"     = true
    "sigv4AuthType" = "workspace-iam-role"
    "sigV4Region"   = var.region
    "defaultRegion" = var.region
  })
}

Expected Behavior

After running terraform apply, it should create the prometheus datasource in Grafana and the data source can read metrics. In Explorer, we can read the list of metrics.

Actual Behavior

The prometheus data source is created and the configuration is the same as the one created manually with the AWS Data Sources app. However, when I open the Explorer, it shows a connection error

Screenshot 2024-07-09 at 5 18 20 PM

If I go back to the data source configuration page and click the Save & test button without changing anything, it can read the metrics list again. It looks like the data source configuration is not properly saved or loaded during the terraform deployment.

Steps to Reproduce

  1. Run terraform apply
  2. Open the prometheus data source configuration in Grafana
  3. Open the Explorer
  4. An error message will appear immediately
  5. The Explorer cannot load any metric
  6. Open the prometheus data source configuration page again
  7. Click the Save & test button (with success)
  8. Open the Explorer
  9. There is no error message
  10. The Explorer can correctly load metrics

Important Factoids

This seems the regression of #516. It occurs when I try to add the data source of AWS Managed Prometheus to an AWS Managed Grafana workspace.

References

No response

julienduchesne commented 3 months ago

If I look at the payload in my browser's network tab, it looks like:

{
  "httpMethod": "GET",
  "oauthPassThru": false,
  "sigV4AssumeRoleArn": "hello",
  "sigV4Auth": true,
  "sigV4AuthType": "ec2_iam_role",
  "sigV4ExternalId": "13124",
  "sigV4Region": "af-south-1"
}

So I think you've just got the wrong data. It looks OK in the UI when you go in the configure page because the workspace-iam-role doesn't exist so it defaults to the ec2_iam_role value which is saved when you click save

haoliangyu commented 3 months ago

@julienduchesne Thanks for the debugging. The wrong value is one of the reason. Another reason is that I need to specify both sigV4AuthType and sigv4AuthType.

I hope the UI can provider the JSON configuration data (like the dashboard) and the error message can be clearer.