Terraform is an open-source infrastructure as code software tool created by HashiCorp. To use terraform, you define the resources you want to create using a declarative configuration language created by Hashicorp known as HashiCorp Configuration Language (HCL), or optionally JSON.
Digital ocean is a cloud provider like AWS, GCP or Azure but its relatively smaller and thus simpler to use and much cheaper. Digital ocean can be found in this website here.
In this guide, we will explore using terraform to launch a Virtual Private Server(VPS) in the Digital Ocean cloud.
Related content:
- How to create a Digitalocean Managed Database cluster using terraform
- How to use Terraform to create a vpc network and a Cloud SQL in GCP
- Using terraform to launch Digitaocean kubernetes cluster
- How to Create Aws Lightsail Instance With Terraform
Prerequisites
- Terraform installed and accessible in $PATH
doctl
- the digital ocean command like installed (Optional)- A Digital ocean account with enough funds to create a VPS (Use this link here to create an account and earn $100 introduction credit)
Installing terraform
Terraform is available as a single binary that can be downloaded from the terraform website’s downloads page here. Download the correct file for your OS from that page and install it in your system.
Once terraform is installed, use the following command to check the version:
|
|
I am using terraform v1.0.5
in this guide.
Installing doctl
doctl
is the official command-line interface (CLI) for the DigitalOcean API. Its an executable binary file that once authenticated, can be used to query digital ocean’s API.
The command is written in golang thus compiles to a single binary. To install it, head over to its github releases page here and grab the correct file for your OS.
Remember to add it to $PATH and confirm that its been installed by checking the version using this command:
|
|
Generate token in api -> Generate new token:
Then auth:
|
|
Creating a VPS
To create an instance, we have to know a couple of things:
- Which image to use
- How big the instance will be
- The region to launch that instance
The best and simple way to know such is by using the installed doctl
command to query.
Please note that you will have to authenticate before using the doctl
command. You will need an access token to authenticate that can be obtained from the digital ocean’s UI, Click this link here to access that page once logged in.
Click on the generate new button in the top right corner of the page and you will be welcomed by the generate token page.
Once you enter the name click the Generate Token button. This will generate a token that is shown once the pop up dissapears, you will be advised to copy and keep the token safe since it will dissapears once you reload or navigate away from the page.
Authenticate
Use this command to authenticate, supply the token generated above:
|
|
You should see an output similar to this:
|
|
Getting information using doctl
Get a list of public images that can be used to launch instances:
|
|
Geting the region list:
|
|
Get digital ocean size list:
|
|
Creating terraform code
To create the resources, we need to give terraform instructions using the HashiCorp Configuration Language(HCL). Terraform will look for hcl logic in all files in the current directory with the extention .tf
.
Terraform providers
A Terraform Provider represents an integration that is responsible for understanding API interactions with the underlying infrastructure. In our case its Digital ocean.
This code logic defines digital ocean provider:
|
|
Variables
Terraform Variables are a way to define centrally controlled reusable values. It is essentially inputs that are passed when executing the terraform scripts or to a module.
Lets define variables in our code:
|
|
The variable defined above is for the token that will be used to authenticate with Digital Ocean.
Terraform data
Terraform data block requests that Terraform read from a given data source and export the result under the given local name. In this example, lets use data to read an already created ssh key in digital ocean called my-pub
. This will be exported under the name my_key
so it can be accessed using data.digitalocean_ssh_key.my_key
:
|
|
Digital ocean droplet
Lets now create a droplet pasing in the required inputs such as name, image, region, size and ssh keys to use when creating it. The values such as image, region, and size can be obtained using doctl
command as shown above.
|
|
Terraform outputs
Terraform output values allow you to export structured data about your resources. In our case, we can use outputs to export data about the instance we just created like the IP that it has been assignend, any private IPs, the server id, etc:
|
|
Final code
This is the whole code. It can be saved in a single file in the current directory.
|
|
Executing terraform
To create the defined resources, execute the terraform code with the following commands passing the defined token as a variable. The variable is exported as an env variable with the bash export DO_TOKEN=xxxxx
command:
Running the plan
|
|
To view the current state of your changes, use the following command:
|
|
Applying the changes
|
|
Conclusion
In this guide we went through the steps needed to create a vps in digital ocean.