Skip to content

Commit f40a6f9

Browse files
authored
MRG: Merge pull request #1 from octue/create-module
Create module
2 parents 3d99436 + f7d1159 commit f40a6f9

16 files changed

+889
-2
lines changed

.github/workflows/release.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow releases a new version of the Terraform module.
2+
3+
name: Release
4+
5+
# Only trigger when a pull request into main branch is merged.
6+
on:
7+
pull_request:
8+
types: [closed]
9+
branches:
10+
- main
11+
12+
jobs:
13+
release:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout Repository
17+
uses: actions/checkout@v4
18+
19+
- name: Get module version
20+
id: get-version
21+
run: echo "version=$(cat VERSION.txt)" >> $GITHUB_OUTPUT
22+
23+
- name: Create Release
24+
uses: actions/create-release@v1
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, no need to create your own.
27+
with:
28+
tag_name: ${{ steps.get-version.outputs.version }}
29+
release_name: ${{ github.event.pull_request.title }}
30+
body: ${{ github.event.pull_request.body }}
31+
draft: false
32+
prerelease: false
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This workflow updates the pull request description with an auto-generated section containing the categorised commit
2+
# message headers of the pull request's commits. The auto generated section is enveloped between two comments:
3+
# "<!--- START AUTOGENERATED NOTES --->" and "<!--- END AUTOGENERATED NOTES --->". Anything outside these in the
4+
# description is left untouched. Auto-generated updates can be skipped for a commit if
5+
# "<!--- SKIP AUTOGENERATED NOTES --->" is added to the pull request description.
6+
7+
name: update-pull-request
8+
9+
on: [pull_request]
10+
11+
jobs:
12+
description:
13+
uses: octue/workflows/.github/workflows/generate-pull-request-description.yml@main
14+
secrets:
15+
token: ${{ secrets.GITHUB_TOKEN }}
16+
permissions:
17+
contents: read
18+
pull-requests: write

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ cython_debug/
165165
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166166
# and can be added to the global gitignore or merged into this file. For a more nuclear
167167
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168-
#.idea/
168+
.idea/
169169

170170
# Ruff stuff:
171171
.ruff_cache/

README.md

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

VERSION.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

0 commit comments

Comments
 (0)