Note
This Terraform module can be deployed alongside the terraform-octue-django-api module to create a cloud-based "branch/workspace deployment", or alone to create the buckets needed for a local environment.
A Terraform module for deploying Cloud Storage buckets for use with a Django API server running locally or in the cloud.
Deploying this module creates Cloud Storage buckets for a local or cloud environment. This infrastructure is isolated from other environments' infrastructure. These buckets are deployed:
- A public static assets bucket
- A private assets bucket
- An optional public assets bucket
Add the below blocks to your Terraform configuration and run:
terraform init
terraform plan
If you're happy with the plan, run:
terraform apply
and approve the run.
The suggested way of managing environments is via Terraform workspaces.
You can get started right away with the main
environment by removing the environment
input to the module.
To create and used other environments, see the example configuration below. It contains a locals
block that
automatically generates the environment name from the name of the current Terraform workspace by taking the text after
the final hyphen. This supports uniquely named environments in Terraform Cloud (which must be unique within the
organisation) while keeping the environment prefix short but unique within your GCP project. For this to work well,
ensure your Terraform workspace names are slugified.
For example, if your resource affix was my-project
and your Terraform workspace was called my-project-testing
, the
environment would be called testing
and your resources would be named like this:
- Static assets bucket:
"my-project--static-assets--testing"
- Private assets bucket:
"my-project--private-assets--testing"
# main.tf
terraform {
required_version = ">= 1.8.0, <2"
required_providers {
google = {
source = "hashicorp/google"
version = "6.28.0"
}
}
}
provider "google" {
project = var.google_cloud_project_id
region = var.google_cloud_region
}
# Get the environment name from the workspace.
locals {
workspace_split = split("-", terraform.workspace)
environment = element(local.workspace_split, length(local.workspace_split) - 1)
}
module "octue_django_api" {
source = "git::github.com/octue/terraform-octue-django-api.git?ref=0.1.0"
project = var.google_cloud_project_id
region = var.google_cloud_region
resource_affix = var.resource_affix
environment = local.environment
}
module "octue_django_api_buckets" {
source = "git::github.com/octue/terraform-octue-django-api-buckets.git?ref=0.1.0"
server_service_account_email = module.octue_django_api.server_service_account.email
project = var.google_cloud_project_id
resource_affix = var.resource_affix
environment = local.environment
}
# variables.tf
variable "google_cloud_project_id" {
type = string
default = "<your-google-project-id>"
}
variable "resource_affix" {
type = string
default = "<name-of-your-api>"
}
- Terraform:
>= 1.8.0, <2
- Providers:
hashicorp/google
:~>6.28
- Google cloud APIs:
- The Cloud Resource Manager API must be enabled manually before using the module
- All other required google cloud APIs are enabled automatically by the module
The module needs to authenticate with google cloud before it can be used:
- Create a service account for Terraform and assign it the
editor
andowner
basic IAM permissions - Download a JSON key file for the service account
- If using Terraform Cloud, follow these instructions. before deleting the key file from your computer
- If not using Terraform Cloud, follow these instructions or use another authentication method.
Warning
If the deletion_protection
input is set to true
, it must first be set to false
and terraform apply
run before
running terraform destroy
or any other operation that would result in the destruction or replacement of the Cloud
Storage buckets. Not doing this can lead to a state needing targeted Terraform commands and/or manual > configuration
changes to recover from.
Disable deletion_protection
and run:
terraform destroy
Name | Type | Required | Default |
---|---|---|---|
server_service_account_email |
string |
Yes | N/A |
google_cloud_project_id |
string |
Yes | N/A |
resource_affix |
string |
Yes | N/A |
environment |
string |
No | "main" |
create_public_bucket |
boolean |
No | false |
deletion_protection |
bool |
No | true |
See variables.tf
for descriptions.
There are no outputs.