Get Started With Scalr & Terraform CLI on AWS

Author
JB Beck Oct 23, 2020 • 5 Min Read
Share:

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

Within a couple minutes you will have deployed an EC2 instance using Terraform and Scalr. Please note that while we’re taking the example of an EC2 instance here, Scalr can instruct Terraform to deploy any resource it supports.

Prerequisites

To get started, you need to set your AWS access keys as variables in Scalr so that they can be passed along to Terraform. The easiest is to retrieve the access key for the IAM user 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, …

Click to the switcher in the top right corner and go to the account level. Click on Add Cloud Credentials from the account dashboard.


Dashboard


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 Add Credentials and select the AWS Cloud. Enter a name, the Access Key ID and the Secret Access Key. Click save.

Now that you have registered your credentials within Scalr, click on the Environments tab and link the AWS account you just added to an environment by clicking on the link icon in front of AWS.


Environment


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.


Token


Get your organization ID from Scalr

Go back to the environment scope and copy your Organization ID from the dashboard.


Organization ID


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-aws/main/scalr-get-started-aws.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 an EC2 instance in the us-east-2 region. 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>"
  }

Initialize the backend

Run terraform init in the folder where you ran the script. It will instantiate Scalr as the remote backend where Terraform will run and store your state file. 

Provision the virtual machine

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

Destroy the virtual machine

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

Conclusion

Congratulations! You have successfully deployed an instance on AWS 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.

The next step in your Scalr journey is to invite members of your team and connect a VCS provider.

Using another cloud provider? Check out Get Started With Scalr on Azure or Get Started With Scalr on Google Cloud Platform.

Ready to get started?

Scalr is free for up to 5 users