I have a basic understanding but am only 2 days into my terraform experience, hence my problem.
I have a grafana container with persisted data.
Terraform runs on a different container and is executed on deployment. Hence, each time I wish to deploy to an existing grafana volume I encounter terraform errors "already exists" or "managed by someone else".
I would like to "replace" or "overwrite" any existing, regardless of any changes made in between but -replace="<existing.resource>" does not appear to address the "existing resource" problem.
Destroy does not work as I have not imported anything, I have yet to try to import into a skeleton resource, destroy and apply and I would like not to.
What am I missing? Does my approach comply with the declarative approach of terraform? How can I "remind" terraform about the existing resources without to much condition?
The problem appears to be a lack of understanding.
The main.tf creates a datasource and an alert. Dashboards are created using grafonnet, they are not relevant.
terraform {
required_providers {
grafana = {
source = "grafana/grafana"
version = "1.30.0"
}
}
}
provider "grafana" {
url = ...
auth = ...
}
resource "grafana_data_source" "influxdb" {
uid = "datasource_influxdb"
type = "influxdb"
name = "influxdb"
url = "..."
json_data_encoded = jsonencode({
defaultBucket = "..."
httpMode = "POST"
organization = "..."
timeout = 5
tlsAuth = false
tlsAuthWithCACert = false
version = "Flux"
tlsSkipVerify = true
})
secure_json_data_encoded = jsonencode({
token = "..."
})
}
resource "grafana_folder" "rule_folder" {
title = "Alerts"
uid = "rule_folder_uid"
}
resource "grafana_rule_group" "my_alert_rule" {
name = "My Rule Group"
folder_uid = grafana_folder.rule_folder.uid
interval_seconds = 240
org_id = 1
rule {
name = "Alerted CPU"
for = "2m"
condition = "B"
no_data_state = "NoData"
exec_err_state = "Alerting"
annotations = {
__dashboardUid__ = "..."
__panelId__ = 3
}
...
}
}
The entrypoint.sh applies terraform using the official terraform image (which curiously lacks an actual terraform installation):
#!/bin/bash
wget https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_linux_amd64.zip
unzip terraform_1.3.4_linux_amd64.zip
mv terraform /usr/bin/terraform
terraform init
terraform apply -auto-approve
terraform apply -auto-approve -replace="grafana_data_source.influxdb" #problem: terraform does not control this resource
tail -f /dev/null
The log-feed is as follows:
Plan: 3 to add, 0 to change, 0 to destroy.
grafana_folder.rule_folder: Creating...
grafana_data_source.influxdb: Creating...
│ Error: status: 409, body: {"message":"data source with the same name already exists","traceID":""}
│ with grafana_data_source.influxdb,
│ on main.tf line 15, in resource "grafana_data_source" "influxdb":
│ 15: resource "grafana_data_source" "influxdb" {
│ Error: status: 412, body: {"message":"the folder has been changed by someone else","status":"version-mismatch"}
│ with grafana_folder.rule_folder,
│ on main.tf line 36, in resource "grafana_folder" "rule_folder":
│ 36: resource "grafana_folder" "rule_folder" {