Deploy an application to a DigitalOcean droplet
DigitalOcean is a cloud hosting provider for infrastructure and applications. DigitalOcean's cloud platform helps developers develop, manage, and scale their applications by providing simple workflows for complex infrastructure. DigitalOcean provides a lightweight, inexpensive virtual machine called a Droplet that you can deploy applications and services to. The Terraform DigitalOcean provider allows you to deploy and manage your Droplets and other infrastructure as code.
In this tutorial, you will use Terraform to provision a Digital Ocean Droplet and deploy a pre-built Hashicorp-skinned Tetris application to it. You will configure the Droplet with a cloud-init
script and specify an SSH key that can access the Droplet. Then, you will use Terraform to scale the application.
Prerequisites
For this tutorial, you will need:
- A DigitalOcean account
- Terraform v1.0.1+
jq
installed
Note
This tutorial provisions resources that qualify under the DigitalOcean Basic tier. If your account does not qualify under the Basic tier, we are not responsible for any charges that you may incur.
Generate DigitalOcean access token
To manage DigitalOcean resources with Terraform, you must configure the Terraform DigitalOcean provider with a DigitalOcean access token. The provider can access the token set as an environment variable and use it to authenticate with the DigitalOcean API.
To create a token, navigate to your API tokens page in the DigitalOcean console. Select "Generate New Token."
Keep the default scopes for "Read" and "Write" privileges. Enter do-terraform
as your token name and generate your token.
Copy the access token to your clipboard. DigitalOcean will not display this token again.
In your terminal, create an environment variable with your new personal access token. Replace the example value below with the token pasted from your clipboard.
Review the example Terraform configuration
Clone the example repository that contains the Terraform DigitalOcean configuration.
Change into the repository directory.
Open the main.tf
file. This configuration file defines a digitalocean_droplet
resource called terramino
based on an Ubuntu image. Terraform deploys a Droplet configured with a single CPU and 1 gig of RAM as defined by the size
attribute. The region
attribute instructs Terraform to deploy this resource to the nyc1
region.
This is a complete configuration that you can deploy with Terraform. The following sections review each block of this configuration in more detail.
Terraform block
The terraform
block contains Terraform settings, including the providers required by your configuration.
In this example configuration, the required_providers
block installs the provider from the Terraform Registry. The DigitalOcean provider's source is defined as digitalocean/digitalocean
, which is shorthand for registry.terraform.io/digitalocean/digitalocean.
You can also set a version constraint for each provider defined in the required_providers
block. If you do not specify a provider version, Terraform will automatically download the latest version during initialization.
Providers
Providers are plugins that Terraform uses to create and manage your resources. To use a provider, you must define a provider block in your configuration. The provider
block in main.tf
configures the DigitalOcean provider you specified in your terraform
block. You can set your credentials in the provider
block, but it is safer to use the DIGITALOCEAN_ACCESS_TOKEN
environment variable to avoid committing sensitive values into source control. You can also configure other optional, provider-specific settings in this block.
You can use multiple provider blocks in your Terraform configuration to manage resources from different providers. You can even reference data about resources from different providers. For example, you could pass the IP address of your DigitalOcean Droplet to a monitoring resource from DataDog.
Resources
A resource
block defines a component of your infrastructure. A resource might be a physical or virtual component such as a Droplet, or managed services such as the DigitalOcean App platform.
Resource blocks have two strings in the first line of block: the resource type and the resource name. In this example, the resource type is digitalocean_droplet
and the name is terramino
. The prefix of the type maps to the name of the provider. Terraform manages the digitalocean_droplet
resource using the digitalocean
provider. Together, the resource type and resource name form a unique ID for the resource. For example, the ID for your Droplet instance is digitalocean_droplet.terramino
.
Resource blocks contain arguments which you use to configure the resource. Arguments can include things like machine sizes, disk image names, or regions. The provider documentation defines the required and optional arguments for each resource. For your Droplet, the example configuration sets the image ID to an Ubuntu image and the size to s-1vcpu-1gb
, which qualifies for DigitealOcean's Basic tier. It also sets the instance name as terramino
.
This configuration uses the user_data
attribute and the file
function to pass a cloud-init script to your Droplet. Your Droplet will automatically run this cloud-init script and provision itself with your SSH key and the pre-built application.
Outputs
Open the outputs.tf
file.
Terraform displays output values when you apply your configuration. This configuration defines an output value for the IP address of your DigitalOcean Droplet.
You can use Terraform outputs to connect your Terraform projects with other parts of your infrastructure, or with other Terraform projects.
Create SSH key
To SSH into the Droplet and perform operations as the terraform
user, you must create an SSH key. Change the placeholder email address to your email address.
When prompted, press enter to create the SSH key without a passphrase.
Open the terramino_app.yaml
file and paste the contents of tf-digitalocean.pub
into the user data ssh_authorized_keys
section. Save the file.
Deploy your application
Initialize your Terraform directory.
Apply your configuration to deploy your application. Enter yes
to confirm your changes when prompted.
In your web browser, navigate to the IP address in the Terraform output to verify your application deployed successfully. It may take a few moments for your Droplet to start Terramino. Refresh the page after a few minutes if nothing appears.
Modify your deployment
One of the benefits of using infrastructure as code is the ability to modify your resources after you deploy them.
Add a count
block to your digitalocean_droplet
block to scale your deployment.
With the count
attribute in your configuration, you need to update your outputs to capture both IP addresses. Update value
of the output in the outputs.tf
file.
The [*]
syntax is a splat expression that iterates over all of the elements of the list given to its left and returns a list of the given attribute from each.
Apply your configuration changes. Enter yes
when prompted to confirm your changes.
You can use the terraform output
command to get the IP addresses of your Droplets for SSH. Use the output
command with the -json
flag to capture your outputs. Then, pipe that list to jq
and select the first element. Enter yes
when prompted to continue connecting to the instance. Make sure you are in the directory in which you generated your SSH key earlier.
Inspect your Apache access logs to review the connections to your application. The first address is the IP address you used to navigate to the application endpoint.
Type exit
in your terminal to close the SSH connection.
Clean up your infrastructure
To avoid unnecessary charges, destroy your application. In your terminal, destroy the resources provisioned by Terraform. Enter yes
when prompted to confirm your changes.
Next steps
In this tutorial, you used Terraform to provision a DigitalOcean Droplet with cloud-init
. Your user data script provisioned your Droplet with a public-facing application and secure credentials.
For more information about the DigitalOcean provider and the concepts used in this tutorial, visit the following documentation:
- DigitalOcean provider documentation
- DigitalOcean documentation
- Learn more about Terraform Outputs
- Learn more about Resources