I have recently written about [Packer](https://brandonrozek.com/blog/snapshotswithpacker/) to create system images or snapshots. This post will go over another [HashiCorp](https://www.hashicorp.com/) project named [Terraform](https://www.terraform.io/) that we can use to deploy that image to a VPS. Like before, I am going to go over how to setup this up in DigitalOcean. Check out [this list](https://www.terraform.io/docs/providers/index.html) for documentation on your favorite cloud provider.
## Variables
To protect against committing secrets like API keys, we're going to create a file that only stores variables. For it to be loaded automatically, it needs to be named `terraform.tfvars`. Here is an example configuration:
```
region = "nyc3"
size = "512mb"
domain = "example.com"
subdomain = "temp"
# Secrets
do_token = "DO-TOKEN-HERE"
key_name = "SSH-KEY-NAME-ON-DO"
```
Now to define the variables in HCL, we need to create a separate `variables.tf` file defining their types
```
variable "do_token" {
type = string
}
variable "domain" {
type = string
}
variable "key_name" {
type = string
}
variable "subdomain" {
type = string
}
variable "region" {
type = string
}
variable "size" {
type = string
}
```
## Configuration
Now let's create a file called `do.tf`. We need to start off by stating which provider we are using
```
provider "digitalocean" {
token = var.do_token
}
```
If you want to hook up your SSH key, then we need to query the Digital Ocean API for its ID.
```
data "digitalocean_ssh_key" laptop {
name = var.key_name
}
```
We need to also query the API for the packer snapshot we created. Replace this with any standard image like `"ubuntu-20-04-x64"` if you don't want to use a snapshot.
```
data "digitalocean_droplet_snapshot" "packer_snapshot" {