kislerdm / terraform-provider-neon

Terraform provider to manage Neon SaaS resources
https://registry.terraform.io/providers/kislerdm/neon/latest/docs
Mozilla Public License 2.0
27 stars 10 forks source link

Is there a way to get connection_uri with existing project and you can create multiple dbs inside? #95

Open jeremyrx7 opened 2 months ago

jeremyrx7 commented 2 months ago

Hi there,

Thank you for opening an issue.

Hi, I am using the latest version of the provider and it works great. I was wondering if there is a way to create multiple db's in the same project and get the connection uri for the individual ones. Rather than creating per project and get the URI from there.

Thoughts?

Cheers, Jeremy

jeremyrx7 commented 2 months ago

cc @kislerdm, any thoughts?

kislerdm commented 1 month ago

@jeremyrx7 Hello Jeremy, thank you for your question.

Did I get it right, that you'd like to create several databases in a single Neon project using the terraform provider, and you'd like to read the databases connection strings from the terraform state? If it's so, I can suggest concatenating the database connection details in an output variable of the terraform module.

For example, suppose, you have two databases in a single project with a single branch + endpoint. The first database is owned by the default project's role, the second database is owned by a dedicated role. This scenario can be described using the following terraform manifest:

terraform manifest ```terraform terraform { required_providers { neon = { source = "kislerdm/neon" version = ">= 0.5.0" } } } provider "neon" {} # The parent Neon project resource "neon_project" "this" { name = "Foo" } # The first database, owned by the default project's role locals { db_first_user = neon_project.this.database_user db_first_password = neon_project.this.database_password # Note that the database connection is guaranteed by the default project's branch + endpoint db_first_branch_id = neon_project.this.default_branch_id db_first_branch_uri = neon_project.this.database_host } ## The first database resource "neon_database" "first" { name = "db-first" project_id = neon_project.this.id owner_name = local.db_first_user branch_id = local.db_first_branch_id } output "conn_first" { value = "postgresql://${local.db_first_user}:${local.db_first_password}@${local.db_first_branch_uri}/${neon_database.first.name}?sslmode=require" sensitive = true description = "The connection string of the first database." } # The seconds database, owned by the custom role 'myrole' ## A custom role which will own the second database resource "neon_role" "custom" { name = "myrole" project_id = neon_project.this.id branch_id = neon_project.this.default_branch_id } locals { db_second_user = neon_role.custom.name db_second_password = neon_role.custom.password # Note that the database connection is guaranteed by the default project's branch + endpoint db_second_branch_id = neon_project.this.default_branch_id db_second_branch_uri = neon_project.this.database_host } ## The seconds database resource "neon_database" "second" { name = "db-second" project_id = neon_project.this.id owner_name = local.db_second_user branch_id = local.db_second_branch_id } output "conn_second" { value = "postgresql://${local.db_second_user}:${local.db_second_password}@${local.db_second_branch_uri}/${neon_database.second.name}?sslmode=require" sensitive = true description = "The connection string of the second database." } ```

Once the state is provisioned, you can read the connection strings by running the terraform output command.

For example, you can read the connection string of the first database by running the following command:

terraform output conn_first

Similarly, you can read the connection string of the second database by running the following command:

terraform output conn_second

Important notes

Please note that the terraform state will contain the information which allows to access your databases. It's considered a security risk factor because anyone with access to the terraform state can access and manipulate the data in the databases. If your use case requires, please take all measures necessary to restrict such access. For example, limit access to the terraform backend and consider encrypting it (e.g. consider using new tofu functionality).

Regards, Dmitry