hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
43.13k stars 9.58k forks source link

remote-state/pg: Support schema reusability #22338

Open pbusquemdf opened 5 years ago

pbusquemdf commented 5 years ago

Current Terraform Version

0.12.5

Use-cases

When using other remote backends, you have the possibility to specify some sort of storage area (container, buckets, etc), to reuse the same account.

With the PG backend, this is not possible. You can specify a different server, a different user or a different schema, but you cannot share the same account.

As a result, each terraform generation require their one user and schema to store 1 table containing 1 entry.

Attempted Solutions

Using different schema, but this is no efficient.

If you have 50 projects, you would have 50 schema in your database. This is unefficent.

Proposal

There should be a way to specify an "bucket" or container ID, that will be referenced as a column in the database. This would allow the reuse of the same account and table for multiple project.

terraform {
  backend "pg" {
    conn_str = "postgres://user:pass@db.example.com/"
    schema_name= "terraform_backend"
    bucket = "mywebserer"
  }
}

References

sdmorel commented 5 years ago

In my experience, even using a different schemas, you get issues with state locking when using the same account.

thundertaker commented 4 years ago

If you have 50 projects, you would have 50 schema in your database. This is unefficent.

Just curious: why do you think it's inefficient? I have more than 50 schemas in one database, and everything seem to work OK, except, as @sdmorel mentioned, state locking issues (there is a workaround described in #22833 , I would like to try it).