To see Terraform working, you can try deploying our sample web application from GitHub. The architecture includes three Droplets attached to a database and a load balancer.
This tutorial guides you through how to use Terraform to deploy this architecture into a VPC network on your DigitalOcean account. By the end of this tutorial, you will be able to view a small website being hosted by the Droplets in your web browser.
You can choose to follow this tutorial using your own terminal or click the Launch Terminal button below to open an in-browser terminal in an Ubuntu 18.04 environment. If you choose to launch the in-browser terminal, all commands in this tutorial become clickable and you can paste them into the command line simply by clicking on them.
When you launch the in-browser terminal, it may take a minute to download and install Terraform.
Before starting this tutorial, you need four things:
You need to create an API token in your DigitalOcean account**. This allows Terraform to access the DigitalOcean API and programmatically deploy resources into your account.
You need to own a domain name and have its DNS records hosted by DigitalOcean. The domain name allows you to access the newly deployed Droplets via a hostname.
You need to have an SSH key and uploaded your public key to your DigitalOcean account. This is the SSH key that Terraform applies to the Droplets being deployed.
You need to install Terraform on the machine you are deploying from. Terraform is available for MacOS, Windows, Linux, and FreeBSD. If you are using the in-browser terminal for this tutorial, Terraform has already been installed.
We have set up a GitHub repository with several Terraform configuration files that compose the web architecture. You will need to download this repo to your to you machine to deploy the resources. You do not need a GitHub account to download the repo.
git clone https://github.com/do-community/terraform-sample-digitalocean-architectures
cd terraform-sample-digitalocean-architectures/01-minimal-web-db-stack
This directory is where you execute Terraform commands from.
Before running Terraform commands, you need to assign your API token as an environment variables. This allows Terraform to access your DigitalOcean account via the API.
Environment variables are stored on your operating system outside of your working directory. This can be useful for storing variable values that are consistent across your DigitalOcean Terraform deployments, such as your API token or datacenter regions.
echo Enter your DigitalOcean API token?; read token; export TF_VAR_do_token=$token
The command returns a blank prompt. You can verify the variable was created by running the following echo
command:
echo $TF_VAR_do_token
Next, you need to assign values to various variables in order to customize the architecture to your needs. This section provides a brief overview of how Terraform variables work. If you already understand how Terraform variables work, you can skip to the next section
The .tf
files in the working directory contain your DigitalOcean resource configurations. 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.
Here's an example of a simple resource configuration in a .tf
file:
resource "digitalocean_droplet" "web" {
image = "ubuntu-18-04-x64"
name = "web-server"
region = "sfo2"
size = "s-1vcpu-1gb"
count = "2"
}
This resource defines two DigitalOcean Droplets named web-server
that will run an Ubuntu 18.04 image in the SFO2 datacenter region.
While you can define all of your resources by hand in .tf
files, Terraform becomes more powerful when you begin input variables. Input variables give you the ability to dynamically define values for resource attributes before and during deployment.
In the variables.tf
file of the sample architecture, we have defined input variables for many resource attributes, including the region
attribute.
variable "region" {
type = string
default = "nyc3"
}
In this variable, we have defined that the value of the region
attribute should be a string, and that if no other value is provided during the deployment, the default "nyc3"
value will be assigned to any region
attribute with this variable assigned to it.
resource "digitalocean_droplet" "web" {
image = "ubuntu-18-04-x64"
name = "web-1"
region = "var.region"
size = "s-1vcpu-1gb"
count = "2"
}
In the Droplet resource configuration above, we have assigned the region variable (var.region
) to the region attribute. This means that Terraform will seek out this resource's region
attribute value during deployment through one of four methods and in the following order:
.tfvars
fileIf Terraform does not receive a value from any of these methods, it will assign the default value. In the example above, if you didn't assign a value to the region variable through any of the aforementioned methods, Terraform would assign the nyc3
value to all resources with a region
attribute that have this input variable assigned to it.
In the sample architecture, we have assigned values for several required variables in the nyc3.tfvars
file. You will need to change some of these values before deployment.
nyc3.tfvars
file in a text editor:nano nyc3.tfvars
ssh_key
and domain_name
fields. For the ssh_key
field, enter the file name of an SSH key you have previously uploaded to your account. In the domain
field, enter a domain name hosted on your DigitalOcean account. The result should look something like this:
region = "nyc3"
droplet_count = 3
ssh_key = "id_rsa.pub"
subdomain = "minimal-vpc"
domain_name = "example.com"
You can change the values of other variables to better suit your needs.
Terraform requires three steps for deployment: initializing the directory, reviewing an execution plan, and applying (executing) the Terraform plan.
Initialization prepares the working directory for use by accounting for any changes in Terraform's backend configuration. The planning step provides you a detailed manifest of the resources for you to review before execution. Lastly, the terraform apply "infra.out"
command executes the deployment of the resources into your account.
01-minimal-web-db-stack
directory:terraform init
terraform plan -var-file=nyc3.tfvars -out=infra.out
Terraform saves the plan to an infra.out
inside the working directory. Open the file in a text editor to review your Terraform plan.
terraform apply "infra.out"
Terraform will create the resources outlined in the Terraform plan in your DigitalOcean account. You can view the resources being created in the control panel.
Once you have deployed the infrastructure, you can view the resources in the DigitalOcean control panel and you can view the small website being hosted by the Droplets.
minimal-vpc.<your-domain.com>
Click your browser's Reload or Refresh button to see how the load balancer is distributing traffic across the Droplets using a round-robin algorithm.
You can delete resources from your account using Terraform with one command. You can destroy specific resources or all the resources deployed using your .tfvars
file.
terraform destroy -var-file=nyc3.tfvars
Terraform will ask you to confirm the deletion.
Once you've deployed resources into your account, you can use this same workflow to deploy similar architectures for other web applications into your account. You only need to complete these steps once:
To deploy additional architectures using the same configuration, run the following commands from the Terraform directory:
terraform init
to initialize the directory.terraform plan -var-file=nyc3.tfvars -out=infra.out
to create and review your Terraform plan.infra.out
file in a text editor.terraform apply "infra.out"
to deploy resources.After deploying the web application infrastructure, you can use your new Droplets and network to host your websites and applications. You can also edit the Terraform files in the working directory using DigitOcean's Terraform reference documentation to make changes to the infrastructure and redeploy them using the instructions in step 4.