|
| 1 | +--- |
| 2 | +title: 'Terraform' |
| 3 | +metaTitle: 'Manage Prisma Postgres with Terraform' |
| 4 | +metaDescription: 'Provision and manage Prisma Postgres projects, databases, and connections using Terraform.' |
| 5 | +tocDepth: 3 |
| 6 | +toc: true |
| 7 | +--- |
| 8 | + |
| 9 | +Use the [Prisma Postgres Terraform provider](https://registry.terraform.io/providers/prisma/prisma-postgres/latest) to manage projects, databases, and connections with code. |
| 10 | + |
| 11 | +## Conceptual model |
| 12 | + |
| 13 | +Terraform is a desired-state engine: |
| 14 | + |
| 15 | +- You declare the target infrastructure in `.tf` files. |
| 16 | +- Terraform computes a plan (`terraform plan`) by comparing config vs current state. |
| 17 | +- Terraform applies only the required changes (`terraform apply`) and records the result in state. |
| 18 | + |
| 19 | +For Prisma Postgres, this gives a predictable workflow for creating projects, databases, and connections across environments. |
| 20 | + |
| 21 | +## When to use Terraform |
| 22 | + |
| 23 | +Terraform is a strong fit when: |
| 24 | + |
| 25 | +- You already manage infrastructure in Terraform and want one workflow. |
| 26 | +- You prefer explicit `plan` output before applying changes. |
| 27 | +- Your team standardizes on HCL modules and Terraform state backends. |
| 28 | + |
| 29 | +## What you can manage |
| 30 | + |
| 31 | +The provider currently supports: |
| 32 | + |
| 33 | +- `prisma-postgres_project` |
| 34 | +- `prisma-postgres_database` |
| 35 | +- `prisma-postgres_connection` |
| 36 | +- `prisma-postgres_regions` data source |
| 37 | + |
| 38 | +## Prerequisites |
| 39 | + |
| 40 | +- [Terraform](https://developer.hashicorp.com/terraform/install) `>= 1.0` |
| 41 | +- A Prisma account and workspace in [Prisma Console](https://console.prisma.io) |
| 42 | +- A Prisma service token (see [Management API authentication docs](/postgres/introduction/management-api#service-tokens)) |
| 43 | + |
| 44 | +## 1. Set your service token |
| 45 | + |
| 46 | +Set your token as an environment variable: |
| 47 | + |
| 48 | +```terminal |
| 49 | +export PRISMA_SERVICE_TOKEN="prsc_your_token_here" |
| 50 | +``` |
| 51 | + |
| 52 | +## 2. Create `main.tf` |
| 53 | + |
| 54 | +Create the following Terraform configuration: |
| 55 | + |
| 56 | +```hcl file=main.tf |
| 57 | +terraform { |
| 58 | + required_providers { |
| 59 | + prisma-postgres = { |
| 60 | + source = "prisma/prisma-postgres" |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +
|
| 65 | +provider "prisma-postgres" {} |
| 66 | +
|
| 67 | +resource "prisma-postgres_project" "main" { |
| 68 | + name = "my-app" |
| 69 | +} |
| 70 | +
|
| 71 | +resource "prisma-postgres_database" "production" { |
| 72 | + project_id = prisma-postgres_project.main.id |
| 73 | + name = "production" |
| 74 | + region = "us-east-1" |
| 75 | +} |
| 76 | +
|
| 77 | +resource "prisma-postgres_connection" "api" { |
| 78 | + database_id = prisma-postgres_database.production.id |
| 79 | + name = "api-key" |
| 80 | +} |
| 81 | +
|
| 82 | +output "connection_string" { |
| 83 | + value = prisma-postgres_connection.api.connection_string |
| 84 | + sensitive = true |
| 85 | +} |
| 86 | +
|
| 87 | +output "direct_url" { |
| 88 | + value = prisma-postgres_database.production.direct_url |
| 89 | + sensitive = true |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## 3. Initialize and apply |
| 94 | + |
| 95 | +Initialize your working directory: |
| 96 | + |
| 97 | +```terminal |
| 98 | +terraform init |
| 99 | +``` |
| 100 | + |
| 101 | +Review and apply: |
| 102 | + |
| 103 | +```terminal |
| 104 | +terraform plan |
| 105 | +terraform apply |
| 106 | +``` |
| 107 | + |
| 108 | +After apply, retrieve values: |
| 109 | + |
| 110 | +```terminal |
| 111 | +terraform output -raw connection_string |
| 112 | +terraform output -raw direct_url |
| 113 | +``` |
| 114 | + |
| 115 | +## 4. Clean up (optional) |
| 116 | + |
| 117 | +```terminal |
| 118 | +terraform destroy |
| 119 | +``` |
| 120 | + |
| 121 | +## Optional: discover available regions |
| 122 | + |
| 123 | +If you want to select regions dynamically: |
| 124 | + |
| 125 | +```hcl |
| 126 | +data "prisma-postgres_regions" "available" {} |
| 127 | +
|
| 128 | +output "available_regions" { |
| 129 | + value = [ |
| 130 | + for r in data.prisma-postgres_regions.available.regions : "${r.id} (${r.name})" |
| 131 | + if r.status == "available" |
| 132 | + ] |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Production notes |
| 137 | + |
| 138 | +- Store Terraform state in a secure remote backend (for example, S3 + DynamoDB, Terraform Cloud, etc.). |
| 139 | +- Treat state as sensitive: even with `sensitive = true`, secret values are still stored in state. |
| 140 | +- Keep `PRISMA_SERVICE_TOKEN` in your secret manager or CI secrets, not in code. |
| 141 | +- Use separate Terraform workspaces or stacks for `dev`, `staging`, and `prod`. |
| 142 | +- Rotate credentials intentionally when required by replacing connection resources. |
| 143 | + |
| 144 | +## Import existing resources |
| 145 | + |
| 146 | +You can import existing resources into state: |
| 147 | + |
| 148 | +```terminal |
| 149 | +terraform import prisma-postgres_project.main <project-id> |
| 150 | +terraform import prisma-postgres_database.production <database-id> |
| 151 | +terraform import prisma-postgres_connection.api <database-id>,<connection-id> |
| 152 | +``` |
| 153 | + |
| 154 | +Credentials are only returned at creation time and cannot be recovered after import. |
| 155 | + |
| 156 | +## Common troubleshooting |
| 157 | + |
| 158 | +### Missing token |
| 159 | + |
| 160 | +If provider configuration fails with a missing token error, confirm `PRISMA_SERVICE_TOKEN` is set in the same shell session running Terraform. |
| 161 | + |
| 162 | +### Region issues |
| 163 | + |
| 164 | +If create fails for a region value, use `prisma-postgres_regions` to list currently available regions for your workspace. |
| 165 | + |
| 166 | +### Authorization failures |
| 167 | + |
| 168 | +If you receive authorization errors, verify your service token belongs to the expected workspace and has permission to create projects and databases. |
| 169 | + |
| 170 | +## References |
| 171 | + |
| 172 | +- [Prisma Postgres provider on Terraform Registry](https://registry.terraform.io/providers/prisma/prisma-postgres/latest) |
| 173 | +- [Provider configuration](https://registry.terraform.io/providers/prisma/prisma-postgres/latest/docs) |
| 174 | +- [Project resource](https://registry.terraform.io/providers/prisma/prisma-postgres/latest/docs/resources/project) |
| 175 | +- [Database resource](https://registry.terraform.io/providers/prisma/prisma-postgres/latest/docs/resources/database) |
| 176 | +- [Connection resource](https://registry.terraform.io/providers/prisma/prisma-postgres/latest/docs/resources/connection) |
| 177 | +- [Regions data source](https://registry.terraform.io/providers/prisma/prisma-postgres/latest/docs/data-sources/regions) |
0 commit comments