Storing Terraform State in Digital Ocean Space
Tags: digitalocean, terraform
Published: 2020-02-09 16:41:19

Minimal tutorial about how to Store the Terraform State in Digital Ocean Spaces.

In this tutorial, I assume you are already familiar with Terraform and DigitalOcean. `(/^▽^)/`

In order to run the tutorial we need:

- At least [Terraform v0.12.X](https://www.terraform.io/downloads.html) ✅
- Digital Ocean Space 
- Digital Ocean API Personal Token/Keys
- An SSH Key configured

ℹ️ I use Linux/Debian for this tutorial, you might need to adapt some commands to your Operating System.

# 1. Creating a DigialOcean Space

Create a Space for your Terraform State at https://cloud.digitalocean.com/spaces

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1v433i58atwk1e9zgqnl.png)

## 1.1. Space Configuration <a name="space-config"></a>

- Datacenter Region: _the region you prefer_
- CDN (Content Delivery Network): Disabled
- Allow file listing?: Restring File Listing
- Choose a unique name: _some name you prefer_

## 1.2. Space Access Keys <a name="space-access-keys"></a>

Create the Space Access Keys at https://cloud.digitalocean.com/account/api/tokens

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gqzjqpril4ydbbdf4aay.png)

Then choose a meaningful name for your keys, I use _terraform_ so next time I check the keys I know which one is used for.

Then store securely the Keys, we will use them later.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g5qwg9c77dv73yg77x3z.png)

- At least [Terraform v0.12.X](https://www.terraform.io/downloads.html) ✅
- Digital Ocean Space ✅
- Digital Ocean API Personal Token/Keys
- An SSH Key configured

# 2. Digital Ocean API Personal Token/Keys <a name="personal-token"></a>

At the same page as we configured the Space Access Keys, https://cloud.digitalocean.com/account/api/tokens, we create the API Personal Token

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/btp3kp6iqqwhqs51tbxm.png)

with read and write permissions. Store the token securely, we will use it later.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bqcdh45hjjtzrjv1cvpr.png)

- At least [Terraform v0.12.X](https://www.terraform.io/downloads.html) ✅
- Digital Ocean Space ✅
- Digital Ocean API Personal Token/Keys ✅
- An SSH Key configured

# 3. An SSH Key configured <a name="ssh-key"></a>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4ootoqaztxp3q4cindyt.png)

In case you haven't configured an SSH Key already, take a look into the next official Digital Ocean tutorial https://www.digitalocean.com/docs/droplets/how-to/add-ssh-keys/

After you configured one, you should see something similar to:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t89mna46fsetyrxesske.png)

We will use the _Fingerprint_ value later.

- At least [Terraform v0.12.X](https://www.terraform.io/downloads.html) ✅
- Digital Ocean Space ✅
- Digital Ocean API Personal Token/Keys ✅
- An SSH Key configured ✅

# 4. Download and Configure the template

```bash
$> git clone git@github.com:aleixmorgadas/template-terraform-digitalocean-space.git
```

Repo: https://github.com/aleixmorgadas/template-terraform-digitalocean-space

## 4.1. Replacing the placeholders

- `backend_config.hcl`: Replace the variables with your space `Access Key` and `Secret Key` from step [1.2 Space Access Keys from the Tutorial](#space-access-keys)
- `terraform.tfvars`: Replace the variables with your info from steps [2. Digital Ocean API Personal Token/Keys](#personal-token) and [3. An SSH Key configured](#ssh-key) fingerprint, and add the path to your SSH keys.
- `backend.tf`. Replace the `bucket` name with the space name you choose in step [1.1. Space Configuration](#space-config). The tutorial uses the `fra1` Digital Ocean Region as default, you need to change the `endpoint` in case you have choosen another region.

## 4.2. Add `backend_config.hcl` and `terraform.tfvars` to `.gitignore`

Now, those two files contain sensitive information. Better to remove those from your Git repository as soon as possible.

## 4.3. Initializing Terraform and creating a Droplet

Using `terraform init` we will pass the backend configuration from the `backend_config.hcl` file.

```bash
$> terraform init --backend-config=./backend_config.hcl
```

Then, you should see something like:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hom5b3phwte373fy28m5.png)

Now, let's create a droplet to check that everything looks fine with:

```bash
$> terraform apply
```

The Droplet is configured to start an Nginx server as an example. When the command finishes, it will show the Droplet IP. You can perform `curl http://<droplet_ip>` to see it working.


# 5. Checking that the Terraform State is stored in the Digital Ocean Space

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a96ayqqmljk063doh0xf.png)

If you see this, congrats!!! You have configured it correctly!! 🎉🎉🎉

Now you can collaborate with other team members of Continuous Integration systems sharing the infrastructure state via a Digital Ocean Space 😁😁

 ⚡️ Feedback welcomed `!(•̀ᴗ•́)و ̑̑` ⚡️

<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.