TrademarkTrademark
Features
Documentation

Get Started With Scalr & Terraform CLI on GCP

This tutorial goes through the basics of using Scalr as a remote backend for Terraform with Google Cloud Platform.
Ryan FeeJune 11, 2021
Get Started With Scalr & Terraform CLI on GCP
Key takeaways
  • This tutorial shows how to use Scalr as a remote backend for Terraform to deploy a virtual machine on Google Cloud Platform with the Terraform CLI.
  • Setup involves linking GCP credentials to Scalr via a service account JSON key or OIDC workload identity federation, then generating a Scalr API token.
  • The Scalr remote backend uses your Environment ID (starting with env-) in the organization field, and the Scalr token is appended to the Terraform CLI config file.
  • Beyond the CLI, Scalr also supports provisioning through VCS integration with GitHub, GitLab, and Bitbucket, the Scalr API, and the Scalr UI.

This tutorial covers the basics of using Scalr as a remote backend for Terraform. Scalr is a Terraform collaboration tool that gives you a centralized runtime environment, state file storage and locking, policy enforcement with the Open Policy Agent, and role-based access controls.

In a couple of minutes you'll have deployed a virtual machine on Google Compute Engine with Terraform and Scalr. We use a virtual machine as the example here, but Scalr can deploy any resource that Terraform can.

Prerequisites

To get started, you need to give Scalr access to your GCP account. The easiest is to create a Service Account access key within your project and upload the JSON file to Scalr. If you don't know how to do that, click here. Note that you can also use whatever process you have already implemented for Terraform: Hashicorp Vault, environment variables, …

Click on Account in the bottom left corner and go to the account level.

Navigating to the Scalr account level from the bottom left corner

Note: The account level is used for admin tasks - adding cloud credentials, connecting vcs providers, managing users and teams, … The environment level is used to manage workspaces. You can read more on this here.

Click on Provider Configurations from the account dashboard. Click on Create Configuration and select Google Cloud Platform. Enter a name and upload the JSON key file for the GCP service account you created above. (As an alternative to a long-lived JSON key, you can configure OIDC workload identity federation so Scalr authenticates to GCP with short-lived federated tokens.) Click save.

Creating a Google Cloud Platform provider configuration in Scalr

Get your token from Scalr

Click on the profile icon on the top right corner of the screen. Click on API access. Click on Generate API Token and save your token.

Generating a Scalr API access token from the profile menu

Get your environment ID from Scalr

Go back to the environment scope and copy your Environment ID (it starts with env-) from the dashboard. The Scalr remote backend uses this value in the organization field of your Terraform configuration.

Run the Get Started Script or use your own Terraform files

For this step you can decide to use your own Terraform code or use the starter main.tf file that we've built for this tutorial.

If you have a UNIX-based computer and want to use our starter code, open your terminal and run the following script:

bash <(curl https://raw.githubusercontent.com/jeanbaptistebeck/scalr-get-started-gcp/main/scalr-get-started-gcp.sh)>

It will ask for the Scalr account name you chose when creating your account, the Scalr token you retrieved earlier and a workspace name of your choice. This script creates a main.tf file that will deploy a virtual machine. It will also append your Scalr token to your ~/.terraformrc file so that the Terraform CLI can talk to your Scalr account.

If you are using your own Terraform files, append the Scalr token to the CLI configuration file.

  • Windows: terraform.rc in %APPDATA% directory
  • Unix/Linux/Mac : ~/.terraformrc
credentials "my-account.scalr.io" {
  token = "<user-token>"
}

Provision the virtual machine

Run terraform apply to provision the instance. Go to the Scalr UI to check that everything went well. Check that the instance has been deployed by going to your GCP console.

Destroy the virtual machine

Run terraform destroy from the terminal to remove the instance we just launched.

Conclusion

That's it. You've deployed an instance on GCP using Scalr and the Terraform CLI. The CLI is one of several ways in: you can also provision resources through VCS integration (GitHub, Gitlab, Bitbucket), the Scalr API and the Scalr UI.

A good next step is to invite your team and connect a VCS provider.

Using another cloud provider? Check out Get Started With Scalr on AWS or Get Started With Scalr & Terraform CLI on Azure.

About the author
Ryan Feedirector of platform engineering at Scalr
Ryan Fee is the director of platform engineering at Scalr, with over 15 years of experience improving infrastructure experiences at companies large and small.