> **For AI agents:** The documentation index is at [https://docs.digitalocean.com/llms.txt](https://docs.digitalocean.com/llms.txt). Markdown versions of pages use the same URL with `index.html.md` in place of the HTML page (for example, append `index.html.md` to the directory path instead of opening the HTML document). # How to Configure Terraform for DigitalOcean To use Terraform with DigitalOcean, you need to install Terraform and configure a provider file. ## Install Terraform You can install the latest version of Terraform on most operating systems from the command line using various package managers. Click your operating system’s tab below to view instructions on how to install Terraform. ## MacOS To install Terraform on MacOS using Homebrew, run the following command in a terminal: ```shell brew install terraform ``` Once installed, verify Terraform’s installation: ```shell terraform -v ``` The command returns Terraform’s version information: ```shell Terraform v1.5.7 ``` ## Windows To install Terraform on Windows using Chocolatey, run the following command from the command prompt: ```shell choco install terraform ``` Once installed, verify Terraform’s installation: ```shell terraform -v ``` The command returns Terraform’s version information: ```shell Terraform v1.6.4 ``` ## Ubuntu To install Terraform on Ubuntu, add the HashiCorp GPG key to your system: ```shell curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - ``` Next, add the official HashiCorp Terraform Linux repository to `apt`: ```shell sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" ``` Then update `apt` and install Terraform: ```shell sudo apt-get update && sudo apt-get install terraform ``` Once installed, verify the installation: ```shell terraform -v ``` The command returns Terraform’s version information: ```shell Terraform v1.6.4 ``` ## CentOS To install Terraform on CentOS, install the `yum-config-manager` to manage your repositories: ```shell sudo yum install -y yum-utils ``` Use `yum-config-manager` to add the official HashiCorp Linux repository: ```shell sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo ``` Then install Terraform: ```shell sudo yum -y install terraform ``` Once installed, verify Terraform’s installation: ```shell terraform -v ``` The command returns Terraform’s version information: ```shell Terraform v1.6.4 ``` ## Configure Terraform for DigitalOcean To use the DigitalOcean provider with Terraform, you have to configure the plugin using a provider file. This file tells Terraform which provider you’re using (DigitalOcean) and where to find the necessary credentials (your DigitalOcean API token, and your public and private SSH keys). If you do not have any SSH keys set up, you need to [create one and upload the public key to your DigitalOcean account](https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/index.html.md) before continuing. To start, create and move into a directory. This is where you configure and deploy your infrastructure and where you create the provider file. ```shell mkdir ~/example-terraform-directory cd ~/example-terraform-directory ``` Create a new file in the working directory called `provider.tf` and then open it with your preferred text editor. Add the following lines to the file, substituting `the_name_of_your_public_SSH_key` value for the name of the public SSH key uploaded to your DigitalOcean account: `provider.tf` ```text terraform { required_providers { digitalocean = { source = "digitalocean/digitalocean" version = "~> 2.0" } } } variable "do_token" {} variable "pvt_key" {} provider "digitalocean" { token = var.do_token } data "digitalocean_ssh_key" "terraform" { name = "the_name_of_your_public_SSH_key" } ``` Here is a breakdown of what each part of this file defines: The following lines tells Terraform which provider you are using. ``` terraform { required_providers { digitalocean = { source = "digitalocean/digitalocean" version = "~> 2.0" } } } ``` The next lines tell Terraform to seek values for the DigitalOcean security variables upon deployment. - `variable "do_token"`- Tells Terraform to seek your DigitalOcean API token upon deployment so it can access your DigitalOcean account and deploy resources via the API. - `variable "pvt_key"` - Tells Terraform to seek the path to your private SSH key on your local machine upon deployment so it can access the Droplets you deploy. ``` variable "do_token" {} variable "pvt_key" {} ``` The next lines further specify the DigitalOcean provider information by assigning `token` to the `do_token` variable. ``` provider "digitalocean" { token = var.do_token } ``` The last lines define your public SSH key and allows Terraform to automatically add your SSH key to any new Droplets you create. ``` data "digitalocean_ssh_key" "terraform" { name = "the_name_of_your_public_SSH_key" } ``` Once you have pasted the lines into the file and defined the name of your public key in the `data "digitalocean_ssh_key"` section, save the file and then exit. ## Create Terraform Configuration Files Once you have configured Terraform to access your DigitalOcean account, you can begin developing Terraform files that describe and declare the DigitalOcean resources that you want to deploy into your account. Terraform configuration files are text files stored with `.tf` extensions. They are human-readable and they support comments. During deployment, Terraform loads all files with a `.tf` extension and creates a manifest of resources to deploy called a “plan”. You can divide resource configurations between as many or as few `.tf` files as you want. Below is a sample Terraform file describing a Droplet with an nginx web server running on it. The `user_data` Terraform attribute is used to [`provide data`](https://docs.digitalocean.com/products/droplets/how-to/provide-user-data/index.html.md), usually a script, to Droplet(s) to run during creation. The `user_data` in the configuration below sets up an nginx web server. You can copy and paste this file into your working directory as a new `.tf` file and deploy it using the steps in [the next section](https://docs.digitalocean.com/reference/terraform/getting-started/index.html.md#execute-terraform). `www-1.tf` ```text resource "digitalocean_droplet" "www-1" { image = "ubuntu-22-04-x64" name = "www-1" region = "nyc2" size = "s-1vcpu-1gb" 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" } user_data = <