Skip to main content

Getting Started — DigitalOcean

Everything you need before using any module from terraform-do-modules: installation, API token setup, provider config, remote state, and the variable conventions shared by every module.


Prerequisites

ToolMinimum versionInstall
Terraform1.6.0+developer.hashicorp.com/terraform/install
DigitalOcean provider2.0+declared in required_providers
doctl (optional)any recentgithub.com/digitalocean/doctl

Authentication

Create a Personal Access Token

  1. Log in to cloud.digitalocean.com
  2. Go to API → Tokens → Generate New Token
  3. Give it a name and enable Write scope

Export the token as an environment variable — never hard-code it in .tf files:

export TF_VAR_do_token="dop_v1_xxxxxxxxxxxxxxxxxxxx"

Provider configuration

# versions.tf
terraform {
required_version = ">= 1.6.0"

required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = ">= 2.0"
}
}
}

variable "do_token" {
type = string
sensitive = true
}

provider "digitalocean" {
token = var.do_token
}

Remote state on Spaces

DigitalOcean Spaces is S3-compatible, so Terraform's s3 backend works with a small configuration adjustment.

1. Create a Spaces bucket and access key

Create a bucket in the DigitalOcean control panel (Spaces → Create Space), then generate a Spaces access key under API → Spaces Keys → Generate New Key.

2. Export the Spaces credentials

export AWS_ACCESS_KEY_ID="<spaces-access-key>"
export AWS_SECRET_ACCESS_KEY="<spaces-secret-key>"

3. Add the backend block

terraform {
backend "s3" {
endpoint = "https://blr1.digitaloceanspaces.com" # your Spaces region endpoint
bucket = "my-tfstate"
key = "prod/terraform.tfstate"
region = "us-east-1" # required placeholder, ignored by Spaces

skip_credentials_validation = true
skip_metadata_api_check = true
skip_region_validation = true
force_path_style = true
}
}

Workflow

# Download providers and initialise the backend
terraform init

# Preview what will change
terraform plan

# Apply changes
terraform apply

# Tear down (use with care in production)
terraform destroy

Common variables

Every module in terraform-do-modules shares these standard variables:

VariableTypeDefaultDescription
namestring""Short resource name, e.g. app, cluster
environmentstring""Environment label — prod, dev, staging
regionstring"blr1"DigitalOcean region slug
enabledbooltrueSet to false to skip resource creation
label_orderlist["name","environment"]Controls label order in resource names
managedbystring"terraform-do-modules"Tag identifying the managing team

Available region slugs

SlugLocation
nyc3New York 3
sfo3San Francisco 3
ams3Amsterdam 3
sgp1Singapore 1
blr1Bangalore 1
lon1London 1
fra1Frankfurt 1
tor1Toronto 1

Pinning module versions

Always pin modules to a release tag in production:

source = "git::https://github.com/terraform-do-modules/terraform-digitalocean-kubernetes.git?ref=v1.0.0"

Next steps