Deploy to GCP with Terraform
Pre-requisites
Setup
Download Mage maintained Terraform scripts.
Permissions
Go to the IAM management dashboard, find the service account associated to the account you just logged into, and then add these roles to that service account (e.g. choose your account as the principal when adding new roles):
- Artifact Registry Read
- Artifact Registry Writer
- Cloud Run Developer
- Cloud SQL
- Service Account Token Creator
Terraform plan
You can run the following command to see all the resources that will be created by Terraform:
By default, here are the resources that will be created.
1. Install gcloud
CLI
Follow these instructions to install the CLI on your workstation.
For macOS and Homebrew, you can do the following:
2. Log into GCP from CLI
If you don’t already have an account in GCP, create one here.
Once you created an account, from your terminal run the following command to log in:
3. Customize Terraform settings
Project ID (REQUIRED)
Before running any Terraform commands, please change the default
value of the
variable named project_id
in the
./gcp/variables.tf
file.
Docker Image (REQUIRED)
Google Cloud Run supports pulling Docker images directly from Docker Hub. To use the latest Mage image (default):
Simply change the tag to adjust the version of Mage you want to deploy. If you need to build your own image, you can push it to Docker Hub or follow our guide to use GCP Artifact Registry.
Application Name
In the file
./gcp/variables.tf,
you can change the default
value under app_name
:
Region
In the file
./gcp/variables.tf,
you can change the default
value under region
:
Environment variables for application
Set your environment variables in your running cloud environment by adding the
following under the resource named google_cloud_run_service
in the file
./gcp/main.tf:
Secrets
See our docs for configuring secrets in Google Secret Manager and using secrets locally while developing.
Method A: Terraform configurations
-
Once you save a secret in Google Secret Manager, click on the PERMISSIONS tab.
-
Click the button
+ GRANT ACCESS
. -
Under the field labeled New principles, add the service account that is associated to your Google Cloud Run. If you can’t find this, try deploying GCP with Terraform and you’ll receive an error that looks like this:
If you still get this error when re-running
terraform apply
even after granting access, try Method B down below.By default, the service account used by Cloud Run that needs access to the secret is the
Compute Engine
default service account. If you want to grant access to a different service account, configure theservice_account_name
argument in thespec
block inmain.tf
: -
Under the field labeled Select a role, enter the value
Secret Manager Secret Accessor
. -
Click the button SAVE.
-
Mount secrets to Google Cloud Run via Terraform in the file ./gcp/main.tf:
-
In your Python code or in any field that support variable interpolation, enter the following to get the value from the secret: 1
Method B: through GCP UI (aka console but not to be confused with CLI)
-
Go to your Google Cloud Run dashboard, find the service running Mage, and click on it to view its details.
-
Follow these Google provided instructions to manually mount the secret to the running Mage Cloud Run service and grant the right permissions for that service to access those secrets. After you mount the secrets, you can create environment variables in your Cloud Run that reference your secrets. Follow the below example to setup the mounted secret and environment variable for that secret:
Secrets Secret name bigquery_credentials
Reference method Mounted as volume
Mount path /secrets/bigquery
Path 1 bigquery_credentials
Version 1 latest
Environment variables Name path_to_keyfile
Value /secrets/bigquery/bigquery_credentials
More
Other variables defined in ./gcp/variables.tf can also be customized to your needs.
4. Deploy
-
Change directory into scripts folder:
-
Initialize Terraform:
A sample output could look like this:
-
Deploy:
A sample output could look like this:
It’ll take a few minutes for GCP Cloud Run to start up and be ready to receive requests.
After a few minutes, open a browser and go to
http://[IP_address]
Change the
IP_address
to the IP address that was output in your terminal after successfully runningterraform apply
.
Errors
403 Forbidden
If you get this error when trying to open the above IP address in your browser,
open the security group named
[application_name]-security-policy
.
Click on that security group and verify your IP address is whitelisted.
If it isn’t, add a new rule with the following values:
Mode | Match | Action | Priority |
---|---|---|---|
Basic mode | Enter your IP address | Allow | 100 |
If you still get 403, check to see if you’re using http://
and NOT https://
.
Container can’t start
If Cloud Run is deployed but the service failed because the container could’t start, it is usually caused by the Docker image not being built using the correct platform tag.
Pull the Mage Docker image using this command:
Once pulled, tag your image and push to GCP Artifact Registry again.
Updating Mage versions
After Mage is running in your cloud, you can update the Mage Docker image version by running the following command in your CLI tool:
app_name
This is the value you changed when editing the ./gcp/variables.tf file.
Misc
Security
To enable other IP addresses access to Mage, open the security group named
[application_name]-security-policy
.
Click on that security group and add a new rule with the following values:
Mode | Match | Action | Priority |
---|---|---|---|
Basic mode | Enter your IP address | Allow | 100 |
To enable IP addresses access to specific endpoints, you can follow this example to add rules to security policy. Another way is to add a new rule through UI with the following values:
Mode | Match | Action | Priority |
---|---|---|---|
Advanced mode | request.path.startsWith('/api/pipeline_schedules/123/pipeline_runs') && inIpRange(origin.ip, '[IP address]/32') | Allow | 200 |
HTTPS enabling
To enable HTTPS for Mage app deployed on GCP, you need to firstly make sure you have a domain. Then follow the steps below to set up HTTPS:
- In
./gcp/variables.tf
file, update the
default
value ofssl
to betrue
, and set thedefault
value ofdomain
variable to be the domain url you want to use. - Run
terraform apply
to create the HTTPS load balancer. - Get the
service_ip
from the output of terraform command. - Update the DNS A and AAAA records to point the domain url to the load balancer’s IP address: https://cloud.google.com/load-balancing/docs/ssl-certificates/google-managed-certs#update-dns
- Visit the domain url to access your Mage app.
Terminate all resources
If you want to delete all resources created from deploying, run the following command:
A sample output could look like this:
dbt
For information about how to serve dbt docs in production, see here.