Creation in the Cloud using Terraform

I created the Debian 8 instance for this website using a powerful tool called Terraform. I’m using Digital Ocean as the hosting provider because they have straightforward pricing and I like their documentation. I was introduced to Digital Ocean through Hoegg Software in Oklahoma City. Terraform provides a scripted way to create, update, and delete infrastructure in the cloud.

I’ll show you now the script that created the virtual machine this website runs on.

The variable digitalocean_token is setup to prompt the user when this script is run to enter the DigitalOcean security token. This token is required to process all API requests and use of terraform.exe (Terraform the application). The provider and resource section make up the important sections of this script. Again, DigitalOcean¬†is the virtual machine host. The VM instances there are called¬†droplets . The resource section is easy to understand. I want a 1GB virtual machine in the New York City 2 datacenter with Debian 8 for 64 bit architecture. The SSH_KEYS field is a little more complicated. The number you see is an identifier that’s created using Terraform and the DigitalOcean API. It starts with your SSH RSA public key file. To obtain the ssh_keys id create an infrastructure folder in isolation of the folder holding the instance creation. My machine creation resides in c:/Infrastructure/digital-ocean and the ssh-key work is located at c:/Infrastructure/digital-ocean-ssh. When Terraform is run, all files are processed. This is why the ssh-key infrastructure work is separated.

This script provides a DigitalOcean SSH key resource for the management of SSH keys for Droplet access. Keys created with this resource can be referenced in your droplet configuration via their ID or fingerprint as needed. Always run a Terraform plan first. Then you would apply if satisfied with the plan. Terraform produces a state file when applied. Here’s the state file (obfuscated of course).

This state file is created when you run terraform so always create a backup file e.g. terraform.tfstate.backup. The “id”: “3256467” is what you need to set up SSH and password-less login to your new machine. Set ssh_keys = [3256467] in your terraform specification file and run:

When the plan is executed, you’ll get a sense of what is going to happen. Verify the results and if you’re satisified, run the apply and create your new machine.

Leave a Reply

Wow, 1,647 people read this.