Skip to content

Commit a509c11

Browse files
authored
chore: add tutorial for setting up terraform (#17)
1 parent 58ea898 commit a509c11

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ resource "supabase_settings" "branch" {
113113
}
114114
```
115115

116+
Refer to [our tutorial](docs/tutorial.md) for a step-by-step guide.
117+
116118
## Developing the Provider
117119

118120
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).

docs/tutorial.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Using the Supabase Terraform Provider
2+
3+
## Setting up a TF module
4+
5+
1. Create a Personal Access Token from Supabase Dashboard.
6+
2. Save your access token locally to `access-token` file or a secure credentials store.
7+
3. Create `module/provider.tf` with the following contents:
8+
9+
```tf
10+
terraform {
11+
required_providers {
12+
supabase = {
13+
source = "supabase/supabase"
14+
version = "~> 1.0"
15+
}
16+
}
17+
}
18+
19+
provider "supabase" {
20+
access_token = file("${path.cwd}/access-token")
21+
}
22+
```
23+
24+
Run the command `terraform -chdir=module apply` which should succeed in finding the provider.
25+
26+
## Creating a project
27+
28+
Supabase projects are represented as a TF resource called `supabase_project`.
29+
30+
Create a `module/resource.tf` file with the following contents.
31+
32+
```tf
33+
# Create a project resource
34+
resource "supabase_project" "production" {
35+
organization_id = "<your-org-id>"
36+
name = "tf-example"
37+
database_password = "<your-password>"
38+
region = "ap-southeast-1"
39+
40+
lifecycle {
41+
ignore_changes = [database_password]
42+
}
43+
}
44+
```
45+
46+
Remember to substitue placeholder values with your own. For sensitive fields like password, you may consider retrieving it from a secure credentials store.
47+
48+
Next, run `terraform -chdir=module apply` and confirm creating the new project resource.
49+
50+
### Importing a project
51+
52+
If you have an existing project hosted on Supabase, you may import it into your local terraform state for tracking and management.
53+
54+
Edit `module/resource.tf` with the following changes.
55+
56+
```tf
57+
# Define a linked project variable as user input
58+
variable "linked_project" {
59+
type = string
60+
}
61+
62+
import {
63+
to = supabase_project.production
64+
id = var.linked_project
65+
}
66+
67+
# Create a project resource
68+
resource "supabase_project" "production" {
69+
organization_id = "<your-org-id>"
70+
name = "tf-example"
71+
database_password = "<your-password>"
72+
region = "ap-southeast-1"
73+
74+
lifecycle {
75+
ignore_changes = [database_password]
76+
}
77+
}
78+
```
79+
80+
Run `terraform -chdir=module apply` and you will be prompted to enter the reference ID of an existing Supabase project. If your local TF state is empty, your project will be imported from remote rather than recreated.
81+
82+
Alternatively, you may use the `terraform import ...` command without editing the resource file.
83+
84+
## Configuring a project
85+
86+
Keeping your project settings in-sync is easy with the `supabase_settings` resource.
87+
88+
Create `module/settings.tf` with the following contents.
89+
90+
```tf
91+
# Configure api settings for the linked project
92+
resource "supabase_settings" "production" {
93+
project_ref = var.linked_project
94+
95+
api = jsonencode({
96+
db_schema = "public,storage,graphql_public"
97+
db_extra_search_path = "public,extensions"
98+
max_rows = 1000
99+
})
100+
}
101+
```
102+
103+
Project settings don't exist on their own. They are created and destroyed together with their corresponding project resource referenced by the `project_ref` field. This means there is no difference between creating and updating `supabase_settings` resource while deletion is always a no-op.
104+
105+
You may declare any subset of fields to be managed by your TF module. The Supabase provider always performs a partial update when you run `terraform -chdir=module apply`. The underlying API call is also idempotent so it's safe to apply again if the local state is lost.
106+
107+
To see the full list of settings available, try importing the `supabase_settings` resource instead.
108+
109+
### Configuring branches
110+
111+
One of the most powerful features of TF is the ability to fan out configs to multiple resources. You can easily mirror the configurations of your production project to your branch databases using the `for_each` meta-argument.
112+
113+
Create a `module/branches.tf` file.
114+
115+
```tf
116+
# Fetch all branches of a linked project
117+
data "supabase_branch" "all" {
118+
parent_project_ref = var.linked_project
119+
}
120+
121+
# Override settings for each preview branch
122+
resource "supabase_settings" "branch" {
123+
for_each = { for b in data.supabase_branch.all.branches : b.project_ref => b }
124+
125+
project_ref = each.key
126+
127+
api = supabase_settings.production.api
128+
129+
auth = jsonencode({
130+
site_url = "http://localhost:3001"
131+
})
132+
}
133+
```
134+
135+
When you run `terraform -chdir=module apply`, the provider will configure all branches associated with your `linked_project` to mirror the `api` settings of your production project.
136+
137+
In addition, the `auth.site_url` settings of your branches will be customised to a localhost URL for all branches. This allows your users to login via a separate domain for testing.
138+
139+
## Committing your changes
140+
141+
Finally, you may commit the entire `module` directory to git for version control. This allows your CI runner to run `terraform apply` automatically on new config changes. Any command line variables can be passed to CI via `TF_VAR_*` environment variables instead.
142+
143+
## Resolving config drift
144+
145+
Tracking your configs in TF module does not mean that you lose the ability to change configs through the dashboard. However, doing so could introduce config drift that you need to resolve manually by adding them to your `*.tf` files.

0 commit comments

Comments
 (0)