DEV Community

Aleix Morgadas
Aleix Morgadas

Posted on

Storing Terraform State in Digital Ocean Space

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
  • 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

1.1. Space Configuration

  • 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

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

Alt Text

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

  • At least Terraform v0.12.X
  • Digital Ocean Space ✅
  • Digital Ocean API Personal Token/Keys
  • An SSH Key configured

2. Digital Ocean API Personal Token/Keys

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

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

Alt Text

  • At least Terraform v0.12.X
  • Digital Ocean Space ✅
  • Digital Ocean API Personal Token/Keys ✅
  • An SSH Key configured

3. An SSH Key configured

Alt Text

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

We will use the Fingerprint value later.

  • At least Terraform v0.12.X
  • Digital Ocean Space ✅
  • Digital Ocean API Personal Token/Keys ✅
  • An SSH Key configured ✅

4. Download and Configure the template

$> git clone git@github.com:aleixmorgadas/template-terraform-digitalocean-space.git
Enter fullscreen mode Exit fullscreen mode

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

4.1. Replacing the placeholders

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.

$> terraform init --backend-config=./backend_config.hcl
Enter fullscreen mode Exit fullscreen mode

Then, you should see something like:

Alt Text

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

$> terraform apply
Enter fullscreen mode Exit fullscreen mode

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

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 !(•̀ᴗ•́)و ̑̑ ⚡️

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Top comments (1)

Collapse
 
asherlab profile image
Asher

Thanks