Skip to content

Instantly share code, notes, and snippets.

@CariZa
Last active January 28, 2021 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CariZa/ad3a7f003b0ec64bc3aba601862af929 to your computer and use it in GitHub Desktop.
Save CariZa/ad3a7f003b0ec64bc3aba601862af929 to your computer and use it in GitHub Desktop.
Simple Terraform Digitalocean Configuration
# Set the variable value in *.tfvars file
# or using -var="do_token=..." CLI option
variable "do_token" {}
variable "ssh_key_name" {}
# Configure the DigitalOcean Provider
provider "digitalocean" {
token = "${var.do_token}"
}
# Use existing ssh key stored on Digital Ocean
data "digitalocean_ssh_key" "default" {
name = "${var.ssh_key_name}"
}
# Create a web server
resource "digitalocean_droplet" "web" {
image = "ubuntu-18-04-x64"
name = "web-1"
region = "ams3"
size = "s-1vcpu-1gb"
ssh_keys = ["${data.digitalocean_ssh_key.default.fingerprint}"]
}
output "controller_ip_address" {
value = "${digitalocean_droplet.web.ipv4_address}"
}
# Store the digital ocean token as an environment variable called DOTOKEN
# export DOTOKEN="DOTOKEN"
# test token by running: $ echo $DOTOKEN
# Store the ssh key name as an environment variable called SSHKEYNAME
# export SSHKEYNAME="SSHKEYNAME"
# test token by running: $ echo $SSHKEYNAME
# Run these:
# $ terraform init
# $ terraform plan -var="do_token=$DOTOKEN" -var="ssh_key_name=$SSHKEYNAME"
# - Output will end with: Plan: 1 to add, 0 to change, 0 to destroy.
# $ terraform apply -var="do_token=$DOTOKEN" -var="ssh_key_name=$SSHKEYNAME"
# To destroy:
# $ terraform destroy -var="do_token=$DOTOKEN" -var="ssh_key_name=$SSHKEYNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment