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
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
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.
- 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
with read and write permissions. Store the token securely, we will use it later.
- At least Terraform v0.12.X ✅
- Digital Ocean Space ✅
- Digital Ocean API Personal Token/Keys ✅
- An SSH Key configured
3. An SSH Key configured
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:
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
Repo: https://github.com/aleixmorgadas/template-terraform-digitalocean-space
4.1. Replacing the placeholders
-
backend_config.hcl
: Replace the variables with your spaceAccess Key
andSecret Key
from step 1.2 Space Access Keys from the Tutorial -
terraform.tfvars
: Replace the variables with your info from steps 2. Digital Ocean API Personal Token/Keys and 3. An SSH Key configured fingerprint, and add the path to your SSH keys. -
backend.tf
. Replace thebucket
name with the space name you choose in step 1.1. Space Configuration. The tutorial uses thefra1
Digital Ocean Region as default, you need to change theendpoint
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.
$> terraform init --backend-config=./backend_config.hcl
Then, you should see something like:
Now, let's create a droplet to check that everything looks fine with:
$> 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
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 !(•̀ᴗ•́)و ̑̑
⚡️
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Top comments (1)
Thanks