Deploy A Sample Web Application Using Terraform

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.

This tutorial deploys real resources into your account. These resources will incur charges the longer you keep them running, so destroy the resources at the end of the tutorial to avoid excess billing.
The sample web application architecture.

Follow This Tutorial Using the In-Browser Terminal

You can choose to follow this tutorial using your own terminal or click the Launch Terminal button in any of the code blocks 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 load and initialize the interactive environment.


Before starting this tutorial, you need four things:

Step 1: Download the DigitalOcean Sample Terraform Repo

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.

To download the repository to your system:

 git clone 

Once you have downloaded the repo, open the following directory:

cd terraform-sample-digitalocean-architectures/01-minimal-web-db-stack

This directory is where you execute Terraform commands from.

Step 2: Configure Terraform Environment Variables

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.

To assign your API token as an environment variable, run the following command, entering your API token and the path to your private SSH when prompted:

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

Step 3: Assign Values to Input Variables

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 using input variables. Input variables give you the ability to dynamically define values for resource attributes before and during deployment.

In the 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 assign the resource’s region attribute value during deployment through one of four methods and in the following order:

  1. Command-line flags
  2. Values stored in a .tfvars file
  3. Values set in the Terraform environment variables
  4. User input on the command line during the deployment

If 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.

Assign Values

To edit the input variables, open the nyc3.tfvars file in a text editor:

nano nyc3.tfvars 

Then change the values of the 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 = ""
subdomain = "minimal-vpc"
domain_name = ""


You can change the values of other variables to better suit your needs.

Save the changes to the file and then close it.

Step 4: Initialize, Review Plan, and Execute Terraform

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.

To initialize the working directory, run the following command from the 01-minimal-web-db-stack directory:

terraform init

To review your Terraform plan, run:

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.

To apply the plan and begin deployment, run:

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.

Step 5: View Your Infrastructure

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.

To view the website being hosted by the Droplets, open a web browser and enter the following address, substituting “” for the domain you used in the deployment: minimal-vpc.<>

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.

Log in to the DigitalOcean Control Panel to view your resources:

Step 6: Destroy Resources (Optional)

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.

To destroy all of the resources you created during this tutorial, run:

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:

  • Create an API token.
  • Download Terraform
  • Set up environmental variables

To deploy additional architectures using the same configuration, run the following commands from the Terraform directory:

  1. Run terraform init to initialize the directory.
  2. Run terraform plan -var-file=nyc3.tfvars -out=infra.out to create and review your Terraform plan.
  3. Review your Terraform plan by opening infra.out file in a text editor.
  4. Run terraform apply "infra.out" to deploy resources.

What’s Next?

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.