A Beginner's Guide to Using Terraform with Google Cloud Provider and Anyscale Google Cloudfoundation Module
In this guide, we will walk through setting up and using Terraform with the Google Cloud Provider from your local laptop. We will be using the Anyscale Google Cloudfoundation module found in the Terraform Registry. We will create a basic example based on the anyscale-v2-commonname example from the registry.
The anyscale-v2-commonname example builds the following Google Cloud resources:
- Project
- Enabling Cloud APIs on the Project
- Cloud Storage Bucket - Standard
- IAM Roles
- VPC with publicly routed subnets (no internal)
- VPC Firewall
- FileStore
- A Google Cloud account with billing enabled
- Terraform installed on your local laptop (version 1.0.0 or later)
- You can install terraform on a mac with
brew
viabrew install terraform
. Other install options are available.
- You can install terraform on a mac with
- Google Cloud SDK (gcloud) installed on your local laptop
- You can install gcloud with
brew
viabrew install gcloud
. Other install options are available.
- You can install gcloud with
- Git CLI installed on your local laptop.
- Anyscale CLI installed on your local laptop needs to be 0.5.104 or newer. You can install/upgrade your cli with:
pip install anyscale --upgrade
- Basic understanding of Terraform and Infrastructure as Code
To successfully run the Terraform commands in this guide, your Google Cloud user must have appropriate permissions. The user should have the following roles:
- Project owner: The user should be the project owner or have a custom role with equivalent permissions. This role allows the user to create and manage resources in the Google Cloud project.
- Compute Instance Admin: This role allows the user to create and manage Compute Engine instances.
- Service Account User: This role enables the user to run operations as the service account.
- Compute Network Admin: This role is required for managing networking resources such as VPCs, subnets, and firewall rules.
- Billing Account User: This role is required for the common name example as it creates a new project - when a project is created, you must be able to tie the project to a Google Billing Account ID. This particular permission is only required if you are using the common name example.
Note: If you're using a Service Account to run the Terraform commands, make sure it has the required permissions mentioned above. You can follow the same steps to assign roles to the Service Account.
Before using the Google Cloud provider with Terraform, you need to authenticate. Run the following command to authenticate with your Google Cloud account:
gcloud auth application-default login
Clone the Anyscale examples repository to your local laptop to access the example configuration files:
git clone https://github.com/anyscale/terraform-google-anyscale-cloudfoundation-modules
Navigate to the example directory (anyscale-v2-commonname) within the cloned repository:
cd terraform-google-anyscale-cloudfoundation-modules/examples/anyscale-v2-commonname
Before running Terraform commands, you need to initialize the working directory. Run the following command to initialize Terraform with the Google provider:
terraform init
In the main.tf
file, modify the configuration to fit your needs. You may need to update the variables for you're needs. Some common variables to update are listed below. Additionally, customize the resources created by the Anyscale module. An example of this is to change the anyscale_filestore_tier
from STANDARD to ENTERPRISE. You can also update
the region that resources are created in. In the examples, resources are created in US regions, but these regions and AZ's can be changed.
anyscale_vpc_public_subnet_cidr
- This is the Public Subnet CIDR range you wish to create.customer_ingress_cidr_ranges
- This is the CIDR range to lock down public access from. It can be 0.0.0.0/0 - it can also be locked down to the CIDR range you're users will be using to access clusters.anyscale_org_id
- This is the Anyscale Organization ID. It can be found in the Anyscale UI by an Organization Owner by clicking on the Organization menu option under their User Name.
Create a terraform.tfvars
file in the example directory to store your project-specific variables. Update the variables according to your Google Cloud setup. For example:
anyscale_org_id = "<your-anyscale_org_id>"
anyscale_google_region = "us-central1"
anyscale_google_zone = "us-central1-a"
billing_account_id = "<your_billing_account_id>"
root_folder_number = "<your_gcloud_folder_number>"
customer_ingress_cidr_ranges = "0.0.0.0/0"
root_folder_number
- This is the Google Project Folder number to create the resources in.billing_account_id
- This is the Google Billing Account IDanyscale_google_region
- This is the Google Region to use.anyscale_google_zone
- This is the Google Zone to use within the region. This only applies if you create a STANDARD file store.anyscale_org_id
- This is the Anyscale Organization ID. This can be found by an Anyscale Organization Owner by clicking on their name in the Anyscale Console, and then clicking onOrganization
.
Before creating resources, you can validate your configuration using the following command:
terraform validate
Check the planned changes to your infrastructure by running:
terraform plan -var-file="terraform.tfvars"
If everything looks good, apply the changes to create the resources:
terraform apply -var-file="terraform.tfvars"
Type 'yes' when prompted to confirm the resource creation.
Check your Google Cloud Console to verify that the resources have been created successfully.
With the outputs from Terraform, you can use the anyscale cloud register
command example to register an Anyscale Cloud. You will want to make
sure to edit the name of the cloud.
Example Cloud Register command for GCP:
anyscale cloud register --provider gcp \
--name gce-anyscale-tf-test-1 \
--vpc-name anyscale-tf-test-1 \
--subnet-names anyscale-tf-test-1-subnet-uscentral1 \
--filestore-instance-id anyscale-tf-test-1 \
--filestore-location us-central1-a \
--anyscale-service-account-email anyscale-tf-test-1-crossacc@gcp-register-cloud-1.iam.gserviceaccount.com \
--instance-service-account-email anyscale-tf-test-1-cluster@gcp-register-cloud-1.iam.gserviceaccount.com \
--firewall-policy-names anyscale-tf-test-1-fw \
--cloud-storage-bucket-name anyscale-tf-test-1 \
--region us-central1 \
--project-id gcp-register-cloud-dogfood-1 \
--provider-id projects/123456789012/locations/global/workloadIdentityPools/anyscale-tf-test-1/providers/private-cloud
Once you are done, you can destroy the resources created by Terraform:
terraform destroy -var-file="terraform.tfvars"
Type 'yes' when prompted to confirm the resource destruction.
In this guide, we have covered how to set up and use Terraform with the Google Cloud Provider from a local laptop. We used the Anyscale Google Cloudfoundation module to create resources based on the Anyscale-v2-commonname example. Now you can create and manage your infrastructure on Google Cloud using Terraform and the Anyscale module.