Skip to content

Commit b391766

Browse files
authored
Merge pull request #107 from ovh/ava-object-storage-tf-remote-backend
Add the code associated to the blog post
2 parents ccb679f + 61e0b65 commit b391766

File tree

10 files changed

+192
-0
lines changed

10 files changed

+192
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ use-cases/kubeflow/kubeconfig
5353
my_kube_cluster.yaml
5454
kustomize
5555
containers-orchestration/managed-rancher/create-rancher-with-tf/variables.tf
56+
use-cases/create-and-use-object-storage-as-tf-backend/my-app/backend.tf
57+
use-cases/create-and-use-object-storage-as-tf-backend/object-storage-tf/variables.tf
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
3+
## Create an Object Storage with Terraform
4+
5+
### General information
6+
- 🔗 [Using Terraform with OVHcloud](https://help.ovhcloud.com/csm/fr-terraform-at-ovhcloud?id=kb_article_view&sysparm_article=KB0054776)
7+
- 🔗 [How to use Terraform](https://help.ovhcloud.com/csm/en-gb-public-cloud-compute-terraform?id=kb_article_view&sysparm_article=KB0050787)
8+
- 🔗 [cloud_project_storage](https://registry.terraform.io/providers/ovh/ovh/latest/docs/resources/cloud_project_storage)
9+
- 🔗 [OVH token generation page](https://www.ovh.com/auth/api/createToken?GET=/*&POST=/*&PUT=/*&DELETE=/*)
10+
11+
### Set up
12+
- Install the [Terraform CLI](https://www.terraform.io/downloads.html)
13+
- For non Linux users: Install `gettext` (that included `envsubst` command):
14+
```bash
15+
brew install gettext
16+
brew link --force gettext
17+
```
18+
- Get the credentials from the OVHCloud Public Cloud project:
19+
- `application_key`
20+
- `application_secret`
21+
- `consumer_key`
22+
- Get the `service_name` (Public Cloud project ID)
23+
24+
### Demo
25+
26+
- `cd object-storage-tf`
27+
28+
- set the environment variables `OVH_APPLICATION_KEY`, `OVH_APPLICATION_SECRET`, `OVH_CONSUMER_KEY` and `OVH_CLOUD_PROJECT_SERVICE`
29+
30+
```bash
31+
# OVHcloud provider needed keys
32+
export OVH_ENDPOINT="ovh-eu"
33+
export OVH_APPLICATION_KEY="xxx"
34+
export OVH_APPLICATION_SECRET="xxx"
35+
export OVH_CONSUMER_KEY="xxx"
36+
export OVH_CLOUD_PROJECT_SERVICE="xxx"
37+
```
38+
39+
- replace the value of your OVH_CLOUD_PROJECT_SERVICE environment variable in the `variables.tf` file (in the service_name variable)
40+
41+
`envsubst < variables.tf.template > variables.tf`
42+
43+
- use the [s3.tf](./s3.tf) file to define the resources to create
44+
- use the [output.tf](output.tf) file to display the bucket created at the end of Terraform execution
45+
- run the `terraform init` command
46+
- run the `terraform apply -var bucket_name=my-bucket` command
47+
48+
- save the s3 user credentials in environment variables (mandatory for the following optionnal step)
49+
```bash
50+
export AWS_ACCESS_KEY_ID=$(terraform output -raw access_key_id)
51+
export AWS_SECRET_ACCESS_KEY=$(terraform output -raw secret_access_key)
52+
```
53+
54+
- store the created bucket name in an environment variable (necessary for the next section).
55+
56+
```bash
57+
$ export BUCKET_NAME=$(terraform output s3_bucket)
58+
```
59+
60+
## Configure an OVHcloud S3-compatible Object Storage as Terraform Backend
61+
62+
### Demo
63+
64+
- `cd ../my-app`
65+
66+
- use the [backend.tf](./my-app/backend.tf.template) file to define the resources to create
67+
68+
- mandatory for the `terraform init` command: replace the value of your BUCKET_NAME environment variable in the `backend.tf` file (in the bucket variable)
69+
70+
`envsubst < backend.tf.template > backend.tf`
71+
72+
- run the `terraform init` command
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "<my-bucket>"
4+
key = "my-app-tf-1.5.6.tfstate"
5+
region = "gra"
6+
endpoint = "s3.gra.io.cloud.ovh.net"
7+
skip_credentials_validation = true
8+
skip_region_validation = true
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
ovh = {
4+
source = "ovh/ovh"
5+
}
6+
}
7+
}
8+
9+
provider "ovh" {
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = $BUCKET_NAME
4+
key = "my-app.tfstate"
5+
region = "gra"
6+
endpoints = {
7+
s3 = "https://s3.gra.io.cloud.ovh.net/"
8+
}
9+
skip_credentials_validation = true
10+
skip_region_validation = true
11+
skip_requesting_account_id = true
12+
skip_s3_checksum = true
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
ovh = {
4+
source = "ovh/ovh"
5+
}
6+
}
7+
}
8+
9+
provider "ovh" {
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
output "s3_bucket" {
2+
value = "${ovh_cloud_project_storage.s3_bucket.name}"
3+
}
4+
5+
output "access_key_id" {
6+
value = ovh_cloud_project_user_s3_credential.s3_user_cred.access_key_id
7+
}
8+
9+
output "secret_access_key" {
10+
value = ovh_cloud_project_user_s3_credential.s3_user_cred.secret_access_key
11+
sensitive = true
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_providers {
3+
ovh = {
4+
source = "ovh/ovh"
5+
}
6+
7+
random = {
8+
source = "hashicorp/random"
9+
version = "3.6.3"
10+
}
11+
}
12+
}
13+
14+
provider "ovh" {
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
resource "random_string" "bucket_name_suffix" {
2+
length = 16
3+
special = false
4+
lower = true
5+
upper = false
6+
}
7+
8+
resource "ovh_cloud_project_storage" "s3_bucket" {
9+
service_name = var.service_name
10+
region_name = var.bucket_region
11+
name = "${var.bucket_name}-${random_string.bucket_name_suffix.result}" # the name must be unique within OVHcloud
12+
}
13+
14+
resource "ovh_cloud_project_user" "s3_user" {
15+
description = "${var.bucket_name}-${random_string.bucket_name_suffix.result}"
16+
role_name = "objectstore_operator"
17+
}
18+
19+
resource "ovh_cloud_project_user_s3_credential" "s3_user_cred" {
20+
user_id = ovh_cloud_project_user.s3_user.id
21+
}
22+
23+
resource "ovh_cloud_project_user_s3_policy" "s3_user_policy" {
24+
service_name = var.service_name
25+
user_id = ovh_cloud_project_user.s3_user.id
26+
policy = jsonencode({
27+
"Statement": [{
28+
"Action": ["s3:*"],
29+
"Effect": "Allow",
30+
"Resource": ["arn:aws:s3:::${ovh_cloud_project_storage.s3_bucket.name}","arn:aws:s3:::${ovh_cloud_project_storage.s3_bucket.name}/*"],
31+
"Sid": "AdminContainer"
32+
}]
33+
})
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
variable "service_name" {
2+
default = "$OVH_CLOUD_PROJECT_SERVICE"
3+
}
4+
5+
6+
variable bucket_name {
7+
type = string
8+
}
9+
10+
variable bucket_region {
11+
type = string
12+
default = "GRA"
13+
}

0 commit comments

Comments
 (0)