Hey there, I have problems understanding how to get a remote backend to work. The plan is to not store tfstates in gitlab and instead in a pgdb (S3, AWS, Cloud, …, not possible)
For a first test I created a pg-container on my host using the steps below. It is up and running and after the init I see the schema has been created properly. But the plan fails because there is no remote state found. And sure there is no remote state yet… What did I misunderstood in the how to?
Any help is greatly appreciated.
The password is random. Please ignore the hardcoded pass in the data source, I will provide variables later in an gitignored autofile.
Example provider.tf
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "2.0.2"
}
}
backend "pg" {}
}
provider "vsphere" {
user = var.user
password = var.password
vsphere_server = var.vsphere_server
allow_unverified_ssl = false
}
data "terraform_remote_state" "pgdb" {
backend = "pg"
config = {
conn_str = "postgres://postgres:Asdf1234@localhost/terraform_backend?sslmode=disable"
}
}
Create a new and clean db
docker run --name pg_tfstate -e POSTGRES_PASSWORD=Asdf1234 -e POSTGRES_USER=postgres -e POSTGRES_DB=terraform_backend --publish 127.0.0.1:5432:5432 -d postgres:15.0-alpine
Check workspace
terraform workspace list
* default
Initialise the backend
terraform init -backend-config="conn_str=postgres://postgres:Asdf1234@localhost/terraform_backend?sslmode=disable"
Initializing the backend...
Successfully configured the backend "pg"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/vsphere versions matching "2.0.2"...
- Installing hashicorp/vsphere v2.0.2...
- Installed hashicorp/vsphere v2.0.2 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
[...]
Plan it
terraform plan
data.terraform_remote_state.pgdb: Reading...
data.vsphere_datacenter.datacenter: Reading...
data.vsphere_custom_attribute.owner_name: Reading...
data.vsphere_custom_attribute.admin_contact: Reading...
data.vsphere_custom_attribute.admin_contact: Read complete after 0s [id=310]
data.vsphere_custom_attribute.owner_name: Read complete after 0s [id=304]
data.vsphere_datacenter.datacenter: Read complete after 0s [id=datacenter-49618]
data.vsphere_host.esxi_hosts[0]: Reading...
data.vsphere_compute_cluster.compute_cluster[0]: Reading...
data.vsphere_network.networks[0]: Reading...
data.vsphere_datastore.datastores[0]: Reading...
data.vsphere_virtual_machine.template[0]: Reading...
data.vsphere_network.networks[0]: Read complete after 0s [id=network-49634]
data.vsphere_datastore.datastores[0]: Read complete after 0s [id=datastore-349728]
data.vsphere_compute_cluster.compute_cluster[0]: Read complete after 0s [id=domain-c345684]
data.vsphere_host.esxi_hosts[0]: Read complete after 0s [id=host-345706]
data.vsphere_virtual_machine.template[0]: Read complete after 0s [id=42152b35-6ab4-8e14-4251-3afa13092c42]
╷
│ Error: Unable to find remote state
│
│ with data.terraform_remote_state.pgdb,
│ on provider.tf line 19, in data "terraform_remote_state" "pgdb":
│ 19: data "terraform_remote_state" "pgdb" {
│
│ No stored state was found for the given workspace in the given backend.
╵
And why is the connection string in there? I thought the purpose of using -backend-config
is to prevent that from happening?
.terraform/terraform.tfstate
{
"version": 3,
"serial": 1,
"lineage": "1fef30f4-f401-d745-2722-e41ff99600ee",
"backend": {
"type": "pg",
"config": {
"conn_str": "postgres://postgres:Asdf1234@localhost/terraform_backend?sslmode=disable",
"schema_name": null,
"skip_index_creation": null,
"skip_schema_creation": null,
"skip_table_creation": null
},
"hash": 1949691007
},
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {},
"depends_on": []
}
]
}