In this post we will discuss how to deploy your infrastructure with Terraform using Scalr and GitHub Actions. Scalr will be used as the backend for our Terraform state and GitHub Actions will act as the orchestrator to execute our Terraform runs.
If you're new to Scalr or would like to learn more I would recommend reading the Introduction and Getting Started pages as they provide an good level of detail to get you started with Scalr. For this post it assumes that you have a Scalr Organization and Environment setup and ready to go!
First off we need to setup a few things within Scalr; Service Account, Token, and Workspace. With these three things we will be able to connect up to GitHub and start running some Terraform.
1. From the Scalr Environment dashboard we are going to be clicking on Create workspace.
2. Give the workspace a Name that makes sense for your situation, and ensure you select CLI as the Source as we will be using GitHub Actions as our orchestrator rather than Scalr itself. Finally click on Create workspace.
3. The next step is going to be setting up a Service Account for GitHub to authenticate with. Click on Account.
4. Next click on IAM.
5. From the IAM page click on Service accounts.
6. Click on New service account.
7. Give the service account a meaningful Name, and then click on Grant access.
The final access we are going to be providing is to the workspace that we created, under Roles select admin, for Grant on select Workspace, Environment select your environment (e.g. Non-production), then select the workspace from the Workspace dropdown, finally click Assign.
Now we have all of our pieces put together we will add them into GitHub in the next section!
For our GitHub setup we are simply going to be adding in the Token we just generated within Scalr, this will allow GitHub Actions to perform state storage operations on our workspace.
Our environment is now completely setup and ready to accept our Terraform code as well as the definition of our GitHub Actions workflow. Firstly lets take a quick look at the Terraform code example, it is just going to be provisioning a scratch resource and outputting its value for us. Let's break up the code a little bit.
From the Scalr Workspace dashboard page you can get the Workspace configuration by click on Remote backend configuration.
This will show the follow modal with the details that can be copied and pasted into your editor:
With all of that in place now when we define the following it will store its state into the remote backend we defined earlier.
Now that everything is setup and linked together when we push a change into GitHub a run is triggered. From the Repository root after a change has been pushed you can click on the little orange dot next to the commit.
This brings up a small modal, click on Details.
From here you can see a full summary of what is going on:
Once the Job has finished you can go back to Scalr to see what's happened:
So today we have looked at how easy it is to setup a GitHub repository hosting Terraform code with Scalr as the backend state storage facility using GitHub Actions as the orchestrator. If you're just starting out with Terraform in your organization and are looking for a free, highly-available and solid state storage solution then this is one of the most solid ways to go. As you will soon learn the more you delve into Terraform state is both a blessing and a curse, having it stored somewhere that is extremely solid is absolutely critically important!!