How to use OpenStack Swift as your Terraform Backend


In this quick guide we will go over how to use OpenStack Swift as the backend for Terraform. We will also go into some details about what a Terraform backend is as well as Terraform states.

What is a Terraform backend?

In Terraform, a backend tells how a “state” is loaded and how operations like “plan” and “apply” are executed. A state is the mapping of assets that are created and managed by Terraform, you can think of this as a snapshot of your current cloud infrastructure. By default this state is saved to the local machine that is running Terraform.

While remote backends are completely optional to the operation of Terraform, there are benefits to using one. First it allows for working in a team to be easier. The backend can store the state of a team’s cloud and grants the ability to lock the state to prevent any corruption. It allows you to keep any sensitive information off disk. The Terraform state is retrieved from backend when needed and is stored in memory, the only location this exists is where the backend is storing it. You can also use it for remote operations. When working with large infrastructures, operations like ‘apply’ can take a significant amount of time. Some backend allow for remote operations so that you can apply the change and don’t have to worry about any interruptions that can happen on a local machine.

How to configure Terraform to use a Swift backend

Configuring Swift as your Terraform backend is pretty simple. First, what you have to do is provide access to your OpenStack cloud via the provider section. For information on how to do this, you can see the article here on how to generate and configure a clouds.yaml in OpenStack.

Next you will want to use the backend option with swift. Below is an example of how to do this.

terraform {
  backend "swift" {
    container         = "terraform-state"
    archive_container = "terraform-state-archive"
    cloud = "flex_metal" # Using a clouds.yaml file

After that simply run a terraform init and let it configure the new backend.

root@crees-pc: ~/terraform/openstack # terraform init

Initializing the backend...

Successfully configured the backend "swift"! Terraform will automatically
use this backend unless the backend configuration changes.

Congratulations, you have successfully setup Swift as your Terraform backend!

If you are not running OpenStack yet, a whole new generation of on-demand private clouds have come to the market with InMotion leading the way.

Was this article helpful? Let us know!