If you use a package manager on macOS, Windows, or Linux, you can use it to install Terraform.
First, install the HashiCorp tap (official Homebrew repository):
brew tap hashicorp/tapInstall Terraform:
brew install hashicorp/tap/terraformCheck your Terraform version:
terraform -versionTerraform configuration files are plain text files written in HashiCorp Configuration Language (HCL) and end with .tf.
We recommend using consistent formatting. Run:
terraform fmtBefore applying, initialize the working directory so Terraform can download and install providers:
terraform initValidate your configuration:
terraform validateTerraform applies changes in two steps: create an execution plan, then apply it.
Terraform creates an execution plan for the changes it will make. Review this plan to ensure that Terraform will make the changes you expect.
Once you approve the execution plan, Terraform applies those changes using your workspace's providers.
This workflow ensures that you can detect and resolve any unexpected problems with your configuration before Terraform makes changes to your infrastructure.
cd deployment/envs/gcp/prod
terraform plan -out tf.plan
terraform apply tf.planTerraform configurations for deploying Mega to GCP are under:
deployment/envs/gcp/prod
Each environment directory contains:
main.tfvariables.tfproviders.tfversions.tfterraform.tfvars.example
- Install
gcloudand authenticate to the target project. - Ensure you have permissions to create: VPC, Cloud Run, Cloud SQL, Memorystore (Redis), Filestore, GCS, IAM, Cloud Logging/Monitoring.
Recommended API enablement:
gcloud services enable \
run.googleapis.com \
artifactregistry.googleapis.com \
sqladmin.googleapis.com \
servicenetworking.googleapis.com \
redis.googleapis.com \
file.googleapis.com \
logging.googleapis.com \
monitoring.googleapis.comCopy the example file and edit values:
cd deployment/envs/gcp/prod
cp terraform.tfvars.example terraform.tfvarsSensitive values should be provided via environment variables when using CI/CD:
export TF_VAR_db_username="mega_user"
export TF_VAR_db_password="your-db-password"
export TF_VAR_rails_master_key="your-rails-master-key"terraform init
terraform plan
terraform applyExample outputs after deployment:
app_cloud_run_url– Cloud Run backend (mono) URLui_cloud_run_url– Cloud Run UI (Next.js SSR) URLcloud_sql_connection_name– Cloud SQL connection name for the application
- backend (mono):
public.ecr.aws/m8q5m4u3/mega/mono-engine:latest - UI (Next.js):
public.ecr.aws/m8q5m4u3/mega/mega-ui:<env>-latest(e.g.staging,demo,openatom,gitmono)
Notes:
- Pulling images from ECR Public does not require additional Terraform resources; just set
app_image/ui_imageinterraform.tfvars. - If you need more stable pulls and better in-region performance, you can mirror images into GCP Artifact Registry and switch
app_image/ui_imageto Artifact Registry URLs.
Cloud Run stdout/stderr is exported to Cloud Logging by default. You can verify in Cloud Console:
- Logging: Logs Explorer (resource type
cloud_run_revision) - Monitoring: Cloud Run dashboards
terraform destroyTerraform stores infrastructure state in terraform.tfstate.
List tracked resources:
terraform state listPrint the full state:
terraform show