Steps to Configure Terraform for DigitalOcean

Configure Terraform for DigitalOcean
This tutorial explains the steps to install Terraform and configure a provider file to use Terraform with DigitalOcean. 

 

DigitalOcean is a unique and straightforward cloud hosting provider. DigitalOcean offers cloud computing services to customers to deploy and develop applications that need to be run across multiple cloud servers. DigitalOcean infrastructure is one of the leading cloud service providers based in the USA. Even though the headquarters of DigitalOcean is located in New York City, their data centers are prevalent in every corner of the world to offer seamless cloud services across the globe. In addition, DigitalOcean provides a simple interface and set-up along with a very affordable price.

Terraform is a tool for managing and building infrastructure in an organized way. Users can use Terraform to manage DigitalOcean Droplets, DNS entries, and even Load Balancers, in addition to a wide variety of services offered by other providers. Terraform uses a command-line interface and can run from a desktop or remote server. In this tutorial, we are going to learn how to install and configure Terraform for DigitalOcean. 

 

Install Terraform 

Terraform is a command-line tool that the user can run on their desktop or a remote server. Users can install the latest version of Terraform on the below-mentioned Operating Systems from the command line using various package managers.

 

macOS

To install Terraform on macOS using Homebrew, follow the below steps:

1. First, open a terminal and execute the following command to install Terraform.

 

brew install terraform

 

2. After the installation, execute the following command to verify the Terraform’s installation.

 

terraform -v

 

3. The above command should return the Terraform’s version details, such as: 

 

Terraform v2.5.0

 

Windows

To install Terraform on Windows Operating System, follow the below steps: 

1. First, open the command prompt and execute the following command to install Terraform on Windows using Chocolatey.

 

choco install terraform

 

2. After the installation, execute the following command to verify the Terraform’s installation.

 

terraform -v

 

3. The above command should return the Terraform’s version details, such as: 

 

Terraform v2.5.0

 

CentOS

To install Terraform on CentOS, follow the below steps: 

1. First, open a terminal and execute the following command to install yum-config-manager to manage the repositories. 

 

sudo yum install -y yum-utils

 

2. Next, execute the following command to add the official HashiCorp Linux Repository.

 

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

 

3. After installing the HashiCorp Linux Repository, execute the following command to install Terraform.

 

sudo yum -y install terraform

 

4. After the installation, execute the following command to verify the Terraform’s installation.

 

 Terraform -v

 

5. The above command should return the Terraform’s version details, such as:

 

Terraform v2.5.0

 

 

Ubuntu 

To install Terraform on Ubuntu, follow the below steps: 

1. First, open a terminal and execute the following command to add the HashiCorp GPG key to the system.

 

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

 

2. Next, add the official HashiCorp Linux Repository by executing the following command.

 

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

 

3. After installing the HashiCorp Linux Repository, execute the following commands to update apt and install Terraform.

 

sudo apt-get update
sudo apt-get install terraform

 

4. After the installation, execute the following command to verify the Terraform’s installation.

 

Terraform -v

 

 

5.The above command should return the Terraform’s version details, such as:

 

Terraform v2.5.0

 

 

Configure Terraform 

To use the DigitalOcean provider with Terraform, we have to configure the plugin using a provider file. This file tells Terraform which provider the user uses, such as DigitalOcean, and where to find the necessary credentials, such as the Private SSH key and DigitalOcean API token.

1. First, create and switch into a directory from which we will configure and deploy the infrastructure by executing the following command. Replace the example-terraform-directory value with the actual directory name. 

 

mkdir ~/example-terraform-directory
cd ~/example-terraform-directory

 

2. Next, create a new file in the working directory named provider.tf and open it with the preferred text editor.

 

 vim provider.tf

 

3. Copy and paste the following lines to the file, replace the_name_of_the_public_SSH_key value in the below content with the name of the public SSH key uploaded to the DigitalOcean account:

 

terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "1.22.2"
    }
  }
}

variable "do_token" {}
variable "pvt_key" {}

provider "digitalocean" {
  token = var.do_token
}

data "digitalocean_ssh_key" "terraform" {
  name = "the_name_of_the_public_SSH_key"
}

 

4. Finally, save and exit the file.

 

Create Terraform Configuration Files

Once you have configured Terraform to access the DigitalOcean account, we can begin developing Terraform files describing and declaring the DigitalOcean resources the user wants to deploy into their account. Terraform configuration files are text files with .tf extensions. 

Terraform loads all .tf files during deployment and creates a manifest of resources to deploy called a “plan”. Users can divide resource configurations between as many or as few .tf files as they want.

A sample for Terraform file describing a Droplet with an Nginx web server running on it is mentioned below. Users can copy and paste this file into their working directory as a new .tf file and deploy it using the steps mentioned in the execute Terraform section.

 

 vim www-1.tf 

 

resource "digitalocean_droplet" "www-1" {
  image = "ubuntu-18-04-x64"
  name = "www-1"
  region = "nyc2"
  size = "s-1vcpu-1gb"
  private_networking = true
  ssh_keys = [
    data.digitalocean_ssh_key.terraform.id
  ]
  connection {
    host = self.ipv4_address
    user = "root"
    type = "ssh"
    private_key = file(var.pvt_key)
    timeout = "2m"
  }
  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      # install nginx
      "sudo apt-get update",
      "sudo apt-get -y install nginx"
    ]
  }
}

 

 

Execute Terraform

After configuring the Terraform files, users can deploy all of the configured resources from the command line. Three steps are involved for deployment in Terraform: initializing the directory, reviewing an execution plan, and applying (executing) the Terraform plan.

1. To initialize the working directory, execute the following command:

 

terraform init

 

 

2. If it were successful, the user would receive the below message: 

 

 Terraform has been successfully initialized!.

 

3. Next, create and view the Terraform plan. Execute the following command to create the Terraform plan.

 

terraform plan -out=infra.out

 

4. Terraform returns a manifest of resources that it will deploy when the user applies the plan. The above command also creates an infra.out file with the manifest inside of it. Terraform uses the infra.out file to deploy the resources into the account.

5. After reviewing the plan, run the following command to execute the plan:

 

terraform apply "infra.out”

 

6. Terraform will start executing the plan and will prompt for two security variables:

 

 – var.pvt_key: Mention the path to the private SSH key on the machine, for example, ~/.ssh/id_rsa.

 – var.do_token: Mention the DigitalOcean API token.

Once the variables are provisioned, Terraform will deploy the resources into the DigitalOcean account. Users can open the DigitalOcean Control Panel to view their creation.

 

Conclusion 

This tutorial presents the steps to install and configure Terraform for DigitalOcean. Hope this tutorial was helpful, and do reach out to us if you have any query or suggestions. 

Share this post

Services to Explore

Stay up to date!

Stay up to date with the Web Hosting, Cloud and Server Management Industry News and Tutorials!

We will send you only the relevant emails, and we respect your privacy. Please review our privacy policy for more info.

Managed DigitalOcean Services

Focus on your business, and let us take care of your DigitalOcean Servers!
From what you are reading, it seems you are interested in DigitalOcean and related technologies. If you have a moment to spare, please take a look at our Managed DigitalOcean plan, which might interest you even more!
Managed DigitalOcean

Value-Added Services

We have services that can help you run a successful business. With us, you don't have to worry about these areas because our experts will take care of it for you.

ServerHealers uses cookies.