|
| 1 | +> [!NOTE] |
| 2 | +> This Terraform module can be deployed alongside the [terraform-octue-django-api](https://github.com/octue/terraform-octue-django-api) |
| 3 | +> module to create a cloud-based "branch/workspace deployment", or alone to create the buckets needed for a local |
| 4 | +> environment. |
| 5 | +
|
1 | 6 | # terraform-octue-django-api-buckets
|
2 |
| -A Terraform module for deploying storage buckets for a Django API. |
| 7 | +A Terraform module for deploying Cloud Storage buckets for use with a Django API server running locally or in the cloud. |
| 8 | + |
| 9 | + |
| 10 | +# Infrastructure |
| 11 | +Deploying this module creates Cloud Storage buckets for a local or cloud environment. This infrastructure is [isolated |
| 12 | +from other environments' infrastructure](#environments). These buckets are deployed: |
| 13 | +- A public static assets bucket |
| 14 | +- A private assets bucket |
| 15 | +- An optional public assets bucket |
| 16 | + |
| 17 | + |
| 18 | +# Installation and usage |
| 19 | +Add the below blocks to your Terraform configuration and run: |
| 20 | +```shell |
| 21 | +terraform init |
| 22 | +terraform plan |
| 23 | +``` |
| 24 | + |
| 25 | +If you're happy with the plan, run: |
| 26 | +```shell |
| 27 | +terraform apply |
| 28 | +``` |
| 29 | +and approve the run. |
| 30 | + |
| 31 | + |
| 32 | +## Environments |
| 33 | +The suggested way of managing environments is via [Terraform workspaces](https://developer.hashicorp.com/terraform/language/state/workspaces). |
| 34 | +You can get started right away with the `main` environment by removing the `environment` input to the module. |
| 35 | + |
| 36 | +To create and used other environments, see the example configuration below. It contains a `locals` block that |
| 37 | +automatically generates the environment name from the name of the current Terraform workspace by taking the text after |
| 38 | +the final hyphen. This supports uniquely named environments in Terraform Cloud (which must be unique within the |
| 39 | +organisation) while keeping the environment prefix short but unique within your GCP project. For this to work well, |
| 40 | +ensure your Terraform workspace names are slugified. |
| 41 | + |
| 42 | +For example, if your resource affix was `my-project` and your Terraform workspace was called `my-project-testing`, the |
| 43 | +environment would be called `testing` and your resources would be named like this: |
| 44 | +- Static assets bucket: `"my-project--static-assets--testing"` |
| 45 | +- Private assets bucket: `"my-project--private-assets--testing"` |
| 46 | + |
| 47 | + |
| 48 | +## Example configuration |
| 49 | + |
| 50 | +```terraform |
| 51 | +# main.tf |
| 52 | +
|
| 53 | +terraform { |
| 54 | + required_version = ">= 1.8.0, <2" |
| 55 | + |
| 56 | + required_providers { |
| 57 | + google = { |
| 58 | + source = "hashicorp/google" |
| 59 | + version = "6.28.0" |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +
|
| 64 | +
|
| 65 | +provider "google" { |
| 66 | + project = var.google_cloud_project_id |
| 67 | + region = var.google_cloud_region |
| 68 | +} |
| 69 | +
|
| 70 | +
|
| 71 | +# Get the environment name from the workspace. |
| 72 | +locals { |
| 73 | + workspace_split = split("-", terraform.workspace) |
| 74 | + environment = element(local.workspace_split, length(local.workspace_split) - 1) |
| 75 | +} |
| 76 | +
|
| 77 | +
|
| 78 | +module "octue_django_api" { |
| 79 | + source = "git::github.com/octue/terraform-octue-django-api.git?ref=0.1.0" |
| 80 | + project = var.google_cloud_project_id |
| 81 | + region = var.google_cloud_region |
| 82 | + resource_affix = var.resource_affix |
| 83 | + environment = local.environment |
| 84 | +} |
| 85 | +
|
| 86 | +
|
| 87 | +module "octue_django_api_buckets" { |
| 88 | + source = "git::github.com/octue/terraform-octue-django-api-buckets.git?ref=0.1.0" |
| 89 | + server_service_account_email = module.octue_django_api.server_service_account.email |
| 90 | + project = var.google_cloud_project_id |
| 91 | + resource_affix = var.resource_affix |
| 92 | + environment = local.environment |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```terraform |
| 97 | +# variables.tf |
| 98 | +
|
| 99 | +variable "google_cloud_project_id" { |
| 100 | + type = string |
| 101 | + default = "<your-google-project-id>" |
| 102 | +} |
| 103 | +
|
| 104 | +
|
| 105 | +variable "resource_affix" { |
| 106 | + type = string |
| 107 | + default = "<name-of-your-api>" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Dependencies |
| 112 | +- Terraform: `>= 1.8.0, <2` |
| 113 | +- Providers: |
| 114 | + - `hashicorp/google`: `~>6.28` |
| 115 | +- Google cloud APIs: |
| 116 | + - The Cloud Resource Manager API must be [enabled manually](https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com) |
| 117 | + before using the module |
| 118 | + - All other required google cloud APIs are enabled automatically by the module |
| 119 | + |
| 120 | + |
| 121 | +## Authentication |
| 122 | +The module needs to authenticate with google cloud before it can be used: |
| 123 | + |
| 124 | +1. Create a service account for Terraform and assign it the `editor` and `owner` basic IAM permissions |
| 125 | +2. Download a JSON key file for the service account |
| 126 | +3. If using Terraform Cloud, follow [these instructions](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#using-terraform-cloud). |
| 127 | + before deleting the key file from your computer |
| 128 | +4. If not using Terraform Cloud, follow [these instructions](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication-configuration) |
| 129 | + or use another [authentication method](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication). |
| 130 | + |
| 131 | + |
| 132 | +## Destruction |
| 133 | +> [!WARNING] |
| 134 | +> If the `deletion_protection` input is set to `true`, it must first be set to `false` and `terraform apply` run before |
| 135 | +> running `terraform destroy` or any other operation that would result in the destruction or replacement of the Cloud |
| 136 | +> Storage buckets. Not doing this can lead to a state needing targeted Terraform commands and/or manual > configuration |
| 137 | +> changes to recover from. |
| 138 | +
|
| 139 | +Disable `deletion_protection` and run: |
| 140 | +```shell |
| 141 | +terraform destroy |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | +# Input reference |
| 146 | + |
| 147 | +| Name | Type | Required | Default | |
| 148 | +|--------------------------------|------------|----------|------------| |
| 149 | +| `server_service_account_email` | `string` | Yes | N/A | |
| 150 | +| `google_cloud_project_id` | `string` | Yes | N/A | |
| 151 | +| `resource_affix` | `string` | Yes | N/A | |
| 152 | +| `environment` | `string` | No | `"main"` | |
| 153 | +| `create_public_bucket` | `boolean` | No | `false` | |
| 154 | +| `deletion_protection` | `bool` | No | `true` | |
| 155 | + |
| 156 | +See [`variables.tf`](/variables.tf) for descriptions. |
| 157 | + |
| 158 | + |
| 159 | +# Output reference |
| 160 | +There are no outputs. |
0 commit comments