|
| 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. |
0 commit comments