This pattern is designed to be easy to deploy and maintain, and it is a great starting point for anyone who wants to use Directus in a Google Cloud environment.
The repository includes:
- A Terraform library that defines all the resources required for the pattern
- Instructions for how to use the pattern, as well as additional extra features to expand the pattern further.
This pattern is designed to be a starting point for building your own Directus Cloud Run applications.
Feel free to customize it to meet your specific needs.
Content is pulled from the directus page here: Manual deploy directus to GCP
- Install Google Cloud SDK Installation Instructions
- Install Terraform Installation Instructions
- Ensure your user has
roles/billing.user
on the provided Billing Account ID - Ensure your user has
roles/resourcemanager.projectCreator
on the Organisation - Ensure your user has
roles/resourcemanager.organizationViewer
to view the Organisation
- Authenticate to Google Cloud Platform
gcloud auth login
- Authenticate to Google Cloud Platform using Application Credentials
gcloud auth application-default login
- Set the Default Project to an existing Google Cloud Platform Project
gcloud config set project <PROJECT_ID>
- Set the Default Quota Project to an existing Google Cloud Platform Project
gcloud auth application-default set-quota-project <PROJECT_ID>
- Navigate to the
terraform
directory and runterraform init
- Create a
terraform.auto.tfvars
file in the sameterraform
directory The file should look like:
oauth2_client_id = ""
oauth2_client_secret = ""
org_id = "<YOUR ORG ID>" # THESE MUST BE FILLED OUT
billing_account_id = "<YOUR BILLING ACCOUNT ID>" # THESE MUST BE FILLED OUT
- Upon succesful init you should see
Terraform has been successfully initialized!
- Now run a targetted plan
terraform plan --target module.project_factory
, we need to build the project before we can continue! - Upon a successful plan you should see
Plan: 17 to add, 0 to change, 0 to destroy.
- Now run a targetted apply
terraform apply --target module.project_factory
, this will build the project add--auto-approve
if you want to ignore the secondary check, this process can take upwards of 15 minutes to complete We need to setup theremote state bucket
and theNEW_backend.tf
to use the bucket - Upon successful project creation, you should see
Apply complete! Resources: 17 added, 0 changed, 0 destroyed.
- Now run a targetted plan for the new state bucket and backend
terraform plan --target google_storage_bucket.terraform_state_bucket --target null_resource.org
- Upon successful bucket plan, you should see
Plan: 2 to add, 0 to change, 0 to destroy.
- Now run a targetted apply for the new state bucket and backend
terraform apply --target google_storage_bucket.terraform_state_bucket --target null_resource.org
- Upon successful bucket apply, you should see
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
- Now run
rm backend.tf && mv NEW_backend.tf backend.tf
- Now we need to migrate the local state to the remote state, run
terraform init -migrate-state
and typeyes
to migrate the local state to the remote backend - Now run
rm terraform.tf*
to remove the local state from the folder
- Now that the project and state are deployed, we need to setup the OAuth2 Sign-In Page and Credential. Follow these instructions: Basic Steps
- Now that you have a OAuth2 Credential and Secret, update your
terraform.auto.tfvars
file:
oauth2_client_id = ""
oauth2_client_secret = "<SECRET STRING>"
org_id = "<YOUR ORG ID>" # THESE MUST BE FILLED OUT
billing_account_id = "<YOUR BILLING ACCOUNT ID>" # THESE MUST BE FILLED OUT
- Ensure you add a Redirect URL to your OAuth2.0 Client Authentication as follows:
https://iap.googleapis.com/v1/oauth/clientIds/<YOUR CLIENT ID>:handleRedirect
- Now you can simply run
terraform plan
andterraform apply
from here to deploy the remaining resources
- Create A or AAAA Records on the provided domains to the LB IP Addresses
- Navigate to
https://console.cloud.google.com/security/iap?project=<YOUR PROJECT ID>
- Click the ellipsis on for the line that has the IAP Toggle enabled and is called:
gcp-directus-admin-portal-backend-default
and click settings - Scroll down to
Allowed Domains
and enter youradmin
domain for this page.
- You can now migrate data to your MySQL database using the Cloud SQL Proxy.
- Install
cloud-sql-proxy
by following the steps for your system: Installation Steps - Ensure you are still authenticated to Google Cloud using
gcloud projects list
if data is returned, your good to continue. - Now, following the instructions, mount your GCP MySQL instance locally via the Proxy.
- You can now login to the MySQL Database Locally to migrate data. If you need the credentials, ensure to grab them out of the secrets stored in 'Secrets Manager'
If you need to rename the container, redeploy the container or simply want to remove it, you will need to manually decouple the serverless NEG from the backends. You can do this by going to: Backends then for each of your listed backends that use the NEG, click edit, delete the backend and click save.
You can now delete the cloud run container / update it, as well as delete / update the Serverless NEG.
I don't know why I can't get terraform to handle this transition for me, but I couldn't
Also, in this Terraform I have deployed both an IAP Accessible Load Balancer and a Publically accessible Load Balancer. This is for instructional purposes so that the power of IAP can be shown in combination with a public site.
- You can simply destroy your created project to clean-up this repositories deployed resources:
gcloud projects delete example-foo-bar-1
- You can also delete your infrastructure using terraform:
terraform destroy
Without going SUPER deep on the pricing calculator this respository deploys resources that cost ~$2 AUD / Day, however as it deploys PUBLIC facings infrastructure your costs WILL scale with usage of the containers and the associated storage bucket. This is your warning!
This repository demonstrates an end-to-end deployment pattern for hosting a Docker image using Cloud Run and Identity-Aware Proxy (IAP). However, this is just one approach, and you should always conduct your own research and ensure that the tools and technologies used here meet your specific needs before implementing them in your own projects.