You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/backend_guide.md
+76-10Lines changed: 76 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,11 @@
1
1
---
2
2
page_title: "Using Backend Guide"
3
3
---
4
+
# Configuring Terraform Backends: PostgreSQL vs Object Storage
4
5
5
-
#Terraform Backend
6
+
## Configuring a Terraform Backend with PostgreSQL and State Locking
6
7
7
-
This page describes how to configure a backend by adding the backend block to your configuration with the Terraform Scaleway Provider.
8
+
This guide explains how to configure a remote backend using the Terraform Scaleway Provider with PostgreSQL, enabling remote state management with locking.
8
9
9
10
Terraform provides the option to set up a [“backend”](https://developer.hashicorp.com/terraform/language/backend) of the `state` data files.
10
11
@@ -13,7 +14,7 @@ This option allows you to handle the state and the way certain operations are ex
13
14
Backends can store the state remotely and protect it with locks to prevent corruption;
14
15
it makes it possible for a team to work with ease, or, for instance, to run Terraform within a pipeline.
15
16
16
-
## Create your database
17
+
###Create your database
17
18
18
19
You can create your database resource using terraform itself .
19
20
@@ -60,19 +61,19 @@ and deploy it:
60
61
terraform plan -out "planfile"; terraform apply -input=false -auto-approve "planfile"
61
62
```
62
63
63
-
## Configuring the PostgreSQL Connection String
64
+
####Configuring the PostgreSQL Connection String
64
65
65
66
We choose to set our environment variable for the connection string for this guide. Please check the [secret section](#secrets) for more details.
Hashicorp offers several methods to keep your secrets. Please check the Terraform [partial configuration](https://developer.hashicorp.com/terraform/language/backend#partial-configuration) for this topic.
74
75
75
-
## Create your infrastructure with the Scaleway provider
76
+
####Create your infrastructure with the Scaleway provider
Most of the remote [backends](https://developer.hashicorp.com/terraform/language/backend#backend-types) natively support locking. To run terraform apply, Terraform will automatically acquire a lock;
165
166
if someone else is already running apply, they will already have the lock, and you will have to wait.
166
167
You can run apply with the `-lock-timeout=<TIME>` parameter to tell Terraform to wait up to TIME for a lock to be released (e.g., `-lock-timeout=10m` will wait for 10 minutes).
167
168
168
169
The Lock method prevents opening the state file while already in use.
169
170
170
-
## Share configuration
171
+
###Share configuration
171
172
172
173
You can also share the configuration using the different [data sources](https://www.terraform.io/language/state/remote-state-data).
173
174
This is useful when working on the same infrastructure or the same team.
@@ -177,3 +178,68 @@ data "scaleway_rdb_instance" "mybackend" {
177
178
name = "your-database-name"
178
179
}
179
180
```
181
+
182
+
## Alternative: Store Terraform State in Scaleway Object Storage (Without Locking)
183
+
184
+
[Scaleway object storage](https://www.scaleway.com/en/object-storage/) can be used to store your Terraform state.
185
+
However, this backend does not support state locking, which is critical when multiple users or automated processes might access the same state concurrently.
186
+
Configure your backend as:
187
+
188
+
```
189
+
terraform {
190
+
backend "s3" {
191
+
bucket = "terraform-state"
192
+
key = "my_state.tfstate"
193
+
region = "fr-par"
194
+
endpoint = "https://s3.fr-par.scw.cloud"
195
+
access_key = "my-access-key"
196
+
secret_key = "my-secret-key"
197
+
skip_credentials_validation = true
198
+
force_path_style = true
199
+
skip_region_validation = true
200
+
# Need terraform>=1.6.1
201
+
skip_requesting_account_id = true
202
+
}
203
+
}
204
+
```
205
+
206
+
Warning: This backend does not offer locking. If you're working in a team or running Terraform in CI/CD pipelines, using object storage without locking can lead to state corruption.
207
+
208
+
### Securing credentials
209
+
210
+
To avoid hardcoding secrets in your Terraform configuration, use one of the following secure methods:
211
+
212
+
#### Environment Variables
213
+
214
+
Set the credentials in your shell environment using the AWS-compatible variable names:
215
+
216
+
```shell
217
+
export AWS_ACCESS_KEY_ID=$SCW_ACCESS_KEY
218
+
export AWS_SECRET_ACCESS_KEY=$SCW_SECRET_KEY
219
+
```
220
+
221
+
This approach is simple and works well for scripts, local development, and CI pipelines.
222
+
223
+
#### AWS Credentials Files
224
+
225
+
Store your credentials in:
226
+
227
+
-`~/.aws/credentials` – for secrets
228
+
-`~/.aws/config` – for configuration like profiles or regions
229
+
230
+
Example ~/.aws/credentials file:
231
+
232
+
```
233
+
[default]
234
+
aws_access_key_id = YOUR_SCW_ACCESS_KEY
235
+
aws_secret_access_key = YOUR_SCW_SECRET_KEY
236
+
```
237
+
238
+
This method is ideal for managing multiple profiles or persisting configuration across sessions.
239
+
240
+
Both methods are compatible with Terraform’s S3 backend, which also works with Scaleway Object Storage.
241
+
242
+
For full details, see the official [Terraform S3 backend documentation](https://developer.hashicorp.com/terraform/language/backend/s3#access_key)
243
+
244
+
For example configuration files, refer to the [Object Storage documentation](https://www.scaleway.com/en/docs/object-storage/api-cli/object-storage-aws-cli/)
Copy file name to clipboardExpand all lines: docs/index.md
+2-31Lines changed: 2 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -221,38 +221,9 @@ In addition to [generic provider arguments](https://www.terraform.io/docs/config
221
221
|`region`|`SCW_DEFAULT_REGION`| The [region](./guides/regions_and_zones.md#regions) that will be used as default value for all resources. (`fr-par` if none specified) ||
222
222
|`zone`|`SCW_DEFAULT_ZONE`| The [zone](./guides/regions_and_zones.md#zones) that will be used as default value for all resources. (`fr-par-1` if none specified) ||
223
223
224
-
## Store terraform state on Scaleway S3-compatible object storage
224
+
## Store terraform state
225
225
226
-
[Scaleway object storage](https://www.scaleway.com/en/object-storage/) can be used to store your Terraform state.
227
-
Configure your backend as:
228
-
229
-
```
230
-
terraform {
231
-
backend "s3" {
232
-
bucket = "terraform-state"
233
-
key = "my_state.tfstate"
234
-
region = "fr-par"
235
-
endpoint = "https://s3.fr-par.scw.cloud"
236
-
access_key = "my-access-key"
237
-
secret_key = "my-secret-key"
238
-
skip_credentials_validation = true
239
-
skip_region_validation = true
240
-
# Need terraform>=1.6.1
241
-
skip_requesting_account_id = true
242
-
}
243
-
}
244
-
```
245
-
246
-
Be careful as no locking mechanism are yet supported.
247
-
Using scaleway object storage as terraform backend is not suitable if you work in a team with a risk of simultaneous access to the same plan.
248
-
249
-
Note: For security reason it's not recommended to store secrets in terraform files.
250
-
If you want to configure the backend with environment var, you need to use `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`[source](https://www.terraform.io/docs/backends/types/s3.html#access_key).
251
-
252
-
```bash
253
-
export AWS_ACCESS_KEY_ID=$SCW_ACCESS_KEY
254
-
export AWS_SECRET_ACCESS_KEY=$SCW_SECRET_KEY
255
-
```
226
+
For detailed instructions and best practices, see the full [Backend guide](guides/backend_guide.md)
0 commit comments