panhandlefamily.com

Provisioning VMs on DigitalOcean Using Terraform: A Guide

Written on

Getting Started with Your Cloud Journey

Terraform infrastructure management on DigitalOcean

Photo by ThisisEngineering RAEng on Unsplash

Terraform has emerged as the leading tool for systematically building and managing infrastructure. While it might be tempting to dive straight into the DigitalOcean interface to create resources, this method can lead to inefficiencies.

Why Choose DigitalOcean?

DigitalOcean offers a straightforward platform, making it easier to use than many larger cloud providers, yet it still provides all the necessary features to launch your project. Utilizing Terraform enhances the maintainability of your project and can also reduce costs, allowing you to delete and recreate resources with ease.

In this guide, we will explore how to provision virtual machines (VMs) on DigitalOcean using Terraform scripts.

Provisioning the Droplet with Terraform

We'll begin with the foundational files required for any Terraform project:

  • backend.tf: Specifies where the Terraform state will be stored.
  • versions.tf: Lists the versions of the providers in use.
  • main.tf: Contains the primary Terraform configuration.
  • variables.tf: Defines the variables necessary for your setup.
  • env.tfvars: Contains the environment-specific variable values.

Let’s break down each of these files one by one.

Backend Configuration

For my backend, I will utilize Terraform Cloud since it’s a free option that features an excellent user interface for managing Terraform plans. Below is the definition for the remote backend:

terraform {

backend "remote" {

hostname = "app.terraform.io"

organization = "nokwebspace"

workspaces {

name = "infra-bootstrap-tools-digitalocean"

}

}

}

Creating a workspace is straightforward; all you need is a git repository and a few clicks in the UI.

Next, log into Terraform Cloud using your API key:

terraform login

Here's how to locate your token:

Provider Versions

The versions.tf file indicates which version of the provider you intend to use. This configuration is derived from the provider’s documentation. You'll need to generate an API key to allow Terraform to operate on your account. Instructions can be found in the provider’s documentation:

terraform {

required_providers {

digitalocean = {

source = "digitalocean/digitalocean"

version = "2.17.1"

}

}

}

provider "digitalocean" {

# Configuration options

# Use the environment variable DIGITALOCEAN_ACCESS_TOKEN

}

Make sure to add your DigitalOcean token to Terraform Cloud. If you opt not to use Terraform Cloud, don't forget to export the token in your console:

export DIGITALOCEAN_ACCESS_TOKEN=

You can add the variable in Terraform Cloud by using a variable set, which is beneficial for reusability across multiple workspaces.

Adding DigitalOcean token to Terraform Cloud

Main Configuration

Now it’s time to start writing Terraform code. Your objective is to create one or more droplets for your project. Additionally, you’ll need to reference an SSH key that’s registered in DigitalOcean.

SSH key configuration for DigitalOcean droplets

To maintain organization, consider creating a project and including every droplet within it.

Variable Management

We utilize variables to enhance the configurability of our Terraform scripts. For example, we can define how many instances we need:

variable "worker_count" {

type = number

default = 1

}

By establishing a default value, there’s no need to provide variable files for this particular example. You're all set!

Applying the Configuration

To preview your changes, run:

terraform plan

If everything appears correct, proceed with applying the configuration:

terraform apply

What Comes Next?

Creating a VM is just the first step in cloud engineering, opening up numerous possibilities. This tutorial is part of a Q&A series designed to assist you in developing your next cloud project. Here are some questions I've already addressed:

  • How to create SSH keys with Terraform?
  • How to configure GitHub Environments with Terraform?
  • How to create Ansible Inventory with Terraform?
  • How to run an Ansible playbook using GitHub Action?

Explore more code and insights in the dedicated GitHub repository:

GitHub - xNok/infra-bootstrap-tools: A collection of scripts for setting up a host with Docker Swarm and Caddy…

This repository offers Ansible playbooks for establishing a minimal infrastructure for a simple self-hosted application…

github.com

References

  • Deploy an Application to a DigitalOcean Droplet | Terraform - HashiCorp Learn

DigitalOcean is a cloud hosting provider for infrastructure and applications. Their cloud platform simplifies deployment…

learn.hashicorp.com

Terraform deployment process on DigitalOcean

If you found this post helpful, please show your support by clicking the clap button below! 🚀 Developers: Continue to learn and grow by staying updated with what matters—JOIN FAUN.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Elon Musk's Perspective on Taxes and the Future of Governance

Elon Musk discusses his views on taxation, government efficiency, and the evolution of societal structures in the information age.

New Insights into Weight Loss: A Personal Journey

Discover how one man's weight loss journey led to insights on nutrition and gut health, resulting in a lifestyle transformation.

Enjoying a Sober Summer: A Guide to Thriving Alcohol-Free

Embrace your first sober summer with joy and confidence. Discover tips to thrive without alcohol while making the most of your summer days.