Vultr DevOps Essentials
WARNING: If you are going to connect terraform to your existing infrastructure, or even if you setup new infrastructure, I am not responsible for any damage done, or any new costs you may incur.
In this post, we will explore the use of Terraform for DevOps with Vultr. Vultr is a cloud platform that provides on-demand infrastructure and services for developers and businesses. Terraform is a popular tools used for infrastructure as code (IaC) and automation.
API Key
First off, you'll need an API key. The API key will allow us to utilize Terraform (or something else like Ansible) to connect to our account and perform some action. Also, sometimes the API can have access controls on the host's end, so certain keys can only perform certain tasks.
Once you have your Vultr account, you'll need to login then click Account
> API
, or just visit this link.
The API key should be about 36 characters long.
Terraform
Terraform is an open-source IaC tool that allows you to define and manage infrastructure resources in a declarative way. With Terraform, you can create, update, and delete infrastructure resources such as virtual machines, networks, and storage.
Sidenote
If using FreeBSD, you can install terraform
using this command:
# pkg install terraform
/Sidenote
So we first need to setup the Vultr provider with the API key. I am first going to create the file providers.tf
, with some tips from the Terraform site for Vultr:
# General
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
verstion = "2.22.1"
}
}
}
# Configure Vultr
provider "vultr" {
api_key = var.VULTR_API_KEY
rate_limit = 100
retry_limit = 3
}
variable "VULTR_API_KEY" {}
And create a variables file, called terraform.tfvars
(yes the filename is important):
VULTR_API_KEY = "your_api_key"
Initialize
Since terraform
attempts to keep track of the state of your infrastructure, we need to initialize things. You can do so with this command:
$ terraform init
This should grab the Vultr provider, create a file called .terraform.lock.hcl
, and create a directory with the state information called .terraform
. Do not mess with this information, unless you know exactly what you are doing. Things can get tricky if your state file is out of sync with the actual state of the infrastructure.
Show
Since things are initialized now, we can ask terraform
for the current state of things:
$ terraform show
No state.
Nothing too exciting at this point, since we haven't imported or created anything.
Add a Resource
Just a quick sidenote, if you would like to see the available schema(s) you can use, this command will show you what your providers allow:
$ terraform providers schema -json | jq
$ terraform providers schema -json | jq -r ".provider_schemas[].resource_schemas|keys[]"
So if you're unsure about a resource, and you can't find the docs online, this is a good reference.
Now we'll create a new file called servers.tf
, and add the beginnings for a new server resource:
# Backend API Server
resource "vultr_instance" "api_server" {
...
}
I haven't added any values yet, because we need to find a couple things:
- Plan name
- OS ID for FreeBSD 14.1
- Region
We can get the information from the docs found here. So the values we'll end up using are:
# Backend API Server
resource "vultr_instance" "api_server" {
plan = "vc2-1c-1gb"
os_id = 2212 # FreeBSD 14 x64
region = "mia" # Miami, FL
label = "API Server"
}
Tidy up
I think the devs behind terraform
were expecting people to have some messy config files, so they provided a way for terraform
itself to format your files. So if you have inconsistent spacing, or too much spacing, or some other problems, you can use the format subcommand at this time:
$ terraform fmt
Plan
Once you have your resources all ready to deploy, it's a good idea to find out the "plan" that terraform
is actually planning to implement. You wouldn't want to accidentally remove a production server or something like this. To see the plan, run this command:
$ terraform plan
...
...
...
Plan: 1 to add, 0 to change, 0 to destroy.
This shows that terraform is planning to add 1 resource, which is the server, and that's it. If your plan is different, make sure that it fits your needs before you deploy.
Apply/Deploy
Once you're comfortable with the plan, go ahead and apply the changes:
$ terraform apply
Fortunately Terraform is very conservative, and asks if you are sure that you want to do this:
Do you want to perform these actions?
This is where you give thanks to your Creator for programs that care for you. Because it's easy to mess things up, especially when managing a lot of resources.
Go ahead and type "yes" and press Enter when you're ready. You should see something like this:
vultr_instance.api_server: Creating...
vultr_instance.api_server: Still creating... [10s elapsed]
...
...
vultr_instance.api_server: Creation complete after 1m2s [id=...]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Now that we've launched our server, if we use the show
command again, we'll see what the current state now holds:
$ terraform show
If you make any changes to your servers.tf
file, or add new files and resources, then you'll want to run the plan
command again, and then apply
when you're satisfied.
Destroy
Now, if you want to clean up everything we just did, and remove your infrastructure, you can use destroy
to "Destroy previously-created infrastructure":
$ terraform destroy
I don't recommend this, unless you are sure you want to remove everything.
Import Existing Infrastructure
If you already have server instances, or firewalls, or other resources, you can import those into your Terraform state. You'll need to find the ID of the existing resource typically. Here is a reference of the terraform syntax to import a vultr_instance
.
Conclusion
A couple quick notes:
- You can dynamically do stuff with the instance IP address, by references the resource.
- You can apply an SSH key to the resource, which may be necessary for automation
We've explored the use of Terraform for DevOps with Vultr, a cloud platform that provides on-demand infrastructure and services. We've walked through the process of setting up the Vultr provider, initializing Terraform, and creating a new server resource. We've also covered how to plan and apply changes, as well as how to import existing infrastructure. With Terraform, you can define and manage infrastructure resources in a declarative way, making it easier to automate and manage your infrastructure. By following the steps outlined in this article, you can get started with Terraform and Vultr to streamline your DevOps workflow.