Skip to content

Commit 8ff8a82

Browse files
committed
add terraform documentation
1 parent 3fbacba commit 8ff8a82

File tree

7 files changed

+212
-93
lines changed

7 files changed

+212
-93
lines changed

terraform/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Terraform Configuration for Student Success Tool
2+
3+
This directory contains the Terraform configuration for the Student Success Tool. The configuration is organized into different environments, such as `dev`, `staging`, and `prod`.
4+
5+
## Environments
6+
7+
Each environment has its own directory under `environments/`. For example, the `dev` environment configuration is located in `environments/dev/`.
8+
9+
## Initial Application of the Configuration
10+
11+
To apply the Terraform configuration, navigate to the desired environment directory and run `terraform apply`. For example, to apply the configuration for the `dev` environment:
12+
13+
```sh
14+
cd environments/dev/
15+
terraform apply
16+
```
17+
18+
You can provide variable values using a `terraform.tfvars` file or by supplying them directly on the command line. For example, to use a `terraform.tfvars` file:
19+
20+
```sh
21+
terraform apply -var-file="terraform.tfvars"
22+
```
23+
24+
Or to supply variables on the command line:
25+
26+
```sh
27+
terraform apply -var="project=my-project" -var="region=us-central1"
28+
```
29+
30+
## Applying Updates
31+
32+
After an environment has been applied for the first time, future updates may be applied via a Cloud Build trigger that can apply Terraform configurations. This allows for automated and continuous deployment of infrastructure changes.
33+
34+
## Configuration Details
35+
36+
### Backend Configuration
37+
38+
The Terraform state is stored in a Google Cloud Storage (GCS) bucket. The backend configuration specifies the bucket name and the prefix for the state files.
39+
40+
```hcl
41+
terraform {
42+
backend "gcs" {
43+
bucket = "sst-terraform-state"
44+
prefix = "dev"
45+
}
46+
}
47+
```
48+
49+
### Providers
50+
51+
The configuration uses the Google Cloud provider and the Random provider. The required versions are specified in the `required_providers` block.
52+
53+
```hcl
54+
terraform {
55+
required_providers {
56+
google = {
57+
source = "hashicorp/google"
58+
version = "6.8.0"
59+
}
60+
random = {
61+
source = "hashicorp/random"
62+
version = "3.6.3"
63+
}
64+
}
65+
}
66+
```
67+
68+
### Google Cloud Provider
69+
70+
The Google Cloud provider is configured with the project ID, region, and zone.
71+
72+
```hcl
73+
provider "google" {
74+
project = var.project
75+
region = var.region
76+
zone = var.zone
77+
}
78+
```
79+
80+
### Modules
81+
82+
The configuration uses two modules: `deployment` and `cloudbuild`.
83+
84+
#### Deployment Module
85+
86+
The `deployment` module is responsible for deploying the application. It requires several variables, such as project ID, region, environment, database version, database name, domain, and Docker images for the web application and frontend.
87+
88+
```hcl
89+
module "deployment" {
90+
source = "../../modules/deployment"
91+
92+
project = var.project
93+
region = var.region
94+
environment = var.environment
95+
zone = var.zone
96+
database_version = var.database_version
97+
database_name = var.database_name
98+
domain = var.domain
99+
webapp_image = var.webapp_image
100+
frontend_image = var.frontend_image
101+
}
102+
```
103+
104+
#### Cloud Build Module
105+
106+
The `cloudbuild` module is responsible for configuring Cloud Build. It requires variables such as project ID, domain, environment, region, and Docker images. It also uses the service account ID from the `deployment` module.
107+
108+
```hcl
109+
module "cloudbuild" {
110+
source = "../../modules/cloudbuild"
111+
112+
project = var.project
113+
domain = var.domain
114+
environment = var.environment
115+
region = var.region
116+
webapp_image = var.webapp_image
117+
frontend_image = var.frontend_image
118+
119+
cloudbuild_service_account_id = module.deployment.iam.cloudbuild_service_account_id
120+
}
121+
```
122+
123+
This module is only configured for the `dev` environment and sets up continuous deployment with cloudbuild triggers on code push. The triggers for `dev` can also deploy the other environments by changing the variable `_ENVIRONMENT` for a manual trigger run.

terraform/modules/cloudbuild/main.tf

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
1-
variable "project" {
2-
description = "The project ID"
3-
}
4-
5-
variable "environment" {
6-
description = "The environment"
7-
}
8-
9-
variable "region" {
10-
description = "The region"
11-
}
12-
13-
variable "cloudbuild_service_account_id" {
14-
description = "The Cloud Build service account ID"
15-
type = string
16-
}
17-
18-
variable "webapp_image" {
19-
description = "The webapp Docker image"
20-
type = string
21-
}
22-
23-
variable "frontend_image" {
24-
description = "The frontend Docker image"
25-
type = string
26-
}
27-
28-
variable "domain" {
29-
type = string
30-
}
31-
321
resource "google_artifact_registry_repository" "student_success_tool" {
332
location = "us-central1"
343
repository_id = "student-success-tool"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "project" {
2+
description = "The project ID"
3+
}
4+
5+
variable "environment" {
6+
description = "The environment"
7+
}
8+
9+
variable "region" {
10+
description = "The region"
11+
}
12+
13+
variable "cloudbuild_service_account_id" {
14+
description = "The Cloud Build service account ID"
15+
type = string
16+
}
17+
18+
variable "webapp_image" {
19+
description = "The webapp Docker image"
20+
type = string
21+
}
22+
23+
variable "frontend_image" {
24+
description = "The frontend Docker image"
25+
type = string
26+
}
27+
28+
variable "domain" {
29+
type = string
30+
}

terraform/modules/load_balancer/main.tf

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
variable "project" {
2-
description = "The GCP project ID"
3-
type = string
4-
}
5-
6-
variable "domain" {
7-
description = "The domain for the managed SSL certificate"
8-
type = string
9-
}
10-
11-
variable "region" {
12-
description = "The region where the Cloud Run service is deployed"
13-
type = string
14-
}
15-
16-
variable "environment" {
17-
description = "The environment name (e.g., dev, prod)"
18-
type = string
19-
}
20-
211
resource "google_compute_global_address" "lb_ip" {
222
name = "tf-cr-lb-1-address"
233
address_type = "EXTERNAL"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "project" {
2+
description = "The GCP project ID"
3+
type = string
4+
}
5+
6+
variable "domain" {
7+
description = "The domain for the managed SSL certificate"
8+
type = string
9+
}
10+
11+
variable "region" {
12+
description = "The region where the Cloud Run service is deployed"
13+
type = string
14+
}
15+
16+
variable "environment" {
17+
description = "The environment name (e.g., dev, prod)"
18+
type = string
19+
}

terraform/modules/migrate/main.tf

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,3 @@
1-
variable "environment" {
2-
type = string
3-
}
4-
5-
variable "region" {
6-
type = string
7-
}
8-
9-
variable "image" {
10-
type = string
11-
}
12-
13-
variable "database_instance_connection_name" {
14-
type = string
15-
}
16-
17-
variable "database_instance_private_ip" {
18-
type = string
19-
}
20-
21-
variable "database_name" {
22-
type = string
23-
}
24-
25-
variable "database_password_secret_id" {
26-
type = string
27-
sensitive = true
28-
}
29-
30-
variable "network_id" {
31-
type = string
32-
}
33-
34-
variable "subnetwork_id" {
35-
type = string
36-
}
37-
38-
variable "cloudrun_service_account_email" {
39-
type = string
40-
}
41-
42-
431
resource "google_cloud_run_v2_job" "migrate" {
442
location = var.region
453
name = "${var.environment}-migrate"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
variable "environment" {
2+
type = string
3+
}
4+
5+
variable "region" {
6+
type = string
7+
}
8+
9+
variable "image" {
10+
type = string
11+
}
12+
13+
variable "database_instance_connection_name" {
14+
type = string
15+
}
16+
17+
variable "database_instance_private_ip" {
18+
type = string
19+
}
20+
21+
variable "database_name" {
22+
type = string
23+
}
24+
25+
variable "database_password_secret_id" {
26+
type = string
27+
sensitive = true
28+
}
29+
30+
variable "network_id" {
31+
type = string
32+
}
33+
34+
variable "subnetwork_id" {
35+
type = string
36+
}
37+
38+
variable "cloudrun_service_account_email" {
39+
type = string
40+
}

0 commit comments

Comments
 (0)