TrademarkTrademark
Features
Documentation

Get Started With Scalr & Terraform CLI on Azure

This tutorial will walk you through the basics of using Scalr for Terraform with Microsoft Azure.
Ryan FeeJune 11, 2021
Get Started With Scalr & Terraform CLI on Azure
Key takeaways
  • This tutorial walks through deploying an Azure virtual machine using Scalr as a remote backend for the Terraform CLI.
  • You connect Azure by creating a provider configuration in Scalr with service principal credentials (Client ID, Client Secret, Tenant ID, Subscription ID), or alternatively OIDC workload identity.
  • The Scalr API token goes in your Terraform CLI configuration file (~/.terraformrc on Unix), and the environment name is used as the organization value in the remote backend block.
  • Scalr can deploy any resource Terraform supports and also works via VCS integration, the Scalr API, and the Scalr UI.

This tutorial will walk you through the basics of using Scalr as a remote backend for Terraform. Scalr is a Terraform collaboration tool that provides 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 will have deployed a virtual machine using 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 Azure account. The easiest is to create a service principal that will be used to provision infrastructure. 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, …

From the Scalr UI, navigate to the account-level view (see the Scalr documentation for the current navigation).

Navigating to the Scalr account level

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 Azure. Enter a name and then provide your service principal credentials: Client ID, Client Secret, Tenant ID, and Subscription ID. (As an alternative to a long-lived client secret, you can configure OIDC workload identity so Scalr authenticates to Azure with short-lived federated tokens.) Click save.

Creating an Azure Cloud provider configuration in Scalr

Get your token from Scalr

Open your user profile in the Scalr UI, find the API access / tokens section, generate an API token, and save it somewhere safe (see the Scalr documentation for the current navigation).

Generating a Scalr API access token

Get your environment name from Scalr

Go back to the environment scope and copy your environment name from the Scalr UI. You'll use this as the organization value in the Terraform remote backend block (Scalr's hierarchy is Account → Environment → Workspace; the Terraform organization field carries the environment name).

Run the Get Started Script or use your own Terraform files

For this step you can use your own Terraform code, or the starter main.tf file 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-azure/main/scalr-get-started-azure.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 Azure console.

Destroy the virtual machine

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

Conclusion

Congratulations! You have successfully deployed an instance on Azure using Scalr and the Terraform CLI. Note that you can also provision resources through VCS integration (GitHub, Gitlab, Bitbucket), the Scalr API and the Scalr UI.

From here, invite members of your team and connect a VCS provider.

Using another cloud provider? Check out Get Started With Scalr on AWS or Get Started With Scalr on GCP.

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.