From 28d03043ed41a74f13920bc0f19309980d1230ee Mon Sep 17 00:00:00 2001 From: Jason Ernst Date: Sun, 21 Mar 2021 20:36:49 -0700 Subject: [PATCH] Initial commit to plan / apply / destroy digitalocean compute resources --- .gitignore | 35 +++++++++++++++++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ compute-1.tf | 18 ++++++++++++++++++ provider.tf | 19 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 compute-1.tf create mode 100644 provider.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6d3604 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ + +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Exclude all .tfvars files, which are likely to contain sentitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +# +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc diff --git a/README.md b/README.md new file mode 100644 index 0000000..6673231 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Infrastructure as Code + +Most of this was created from this guide: +https://www.digitalocean.com/community/tutorials/how-to-use-terraform-with-digitalocean#step-4-%E2%80%94-using-terraform-to-create-the-nginx-server + +## Commands: +To plan: +`terraform plan -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"` + +To apply: +`terraform apply -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"` + +To show state: +`terraform show terraform.tfstate` + +To destroy: +`terraform plan -destroy -out=terraform.tfplan \ + -var "do_token=${DO_PAT}" \ + -var "pvt_key=$HOME/.ssh/id_rsa" \` + +to make the destroy plan and `terraform apply terraform.tfplan` diff --git a/compute-1.tf b/compute-1.tf new file mode 100644 index 0000000..7657c8a --- /dev/null +++ b/compute-1.tf @@ -0,0 +1,18 @@ +resource "digitalocean_droplet" "compute-1" { + image = "ubuntu-20-10-x64" + name = "compute-1" + region = "sfo2" + size = "s-1vcpu-1gb" + private_networking = true + ssh_keys = [ + data.digitalocean_ssh_key.terraform.id + ] + + connection { + host = self.ipv4_address + user = "root" + type = "ssh" + private_key = file(var.pvt_key) + timeout = "2m" + } +} diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..823a4da --- /dev/null +++ b/provider.tf @@ -0,0 +1,19 @@ +terraform { + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = "1.22.2" + } + } +} + +variable "do_token" {} +variable "pvt_key" {} + +provider "digitalocean" { + token = var.do_token +} + +data "digitalocean_ssh_key" "terraform" { + name = "terraform" +}