Skip to content

Instantly share code, notes, and snippets.

@gionn
Last active January 16, 2017 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gionn/787a3fc379eeac0ce01f to your computer and use it in GitHub Desktop.
Save gionn/787a3fc379eeac0ce01f to your computer and use it in GitHub Desktop.
Infrastructure as Code with Terraform
.env
terraform.tfstate*

Infrastructure as Code with Terraform

This is an example terraform configuration folder, look at all the history to get iterations from ground to stars.

variable "cloudflare_domain" {
description = "CloudFlare domain"
}
# Add a record to the domain
resource "cloudflare_record" "frontend" {
domain = "${var.cloudflare_domain}"
name = "coderstug"
value = "${digitalocean_droplet.web.0.ipv4_address}"
type = "A"
ttl = 3600
}
# Add a record to the domain
resource "cloudflare_record" "frontend2" {
domain = "${var.cloudflare_domain}"
name = "coderstug"
value = "${digitalocean_droplet.web.1.ipv4_address}"
type = "A"
ttl = 3600
}
resource "digitalocean_droplet" "web" {
image = "${var.do_image}"
name = "web-${count.index+1}"
region = "${var.do_region}"
size = "512mb"
ssh_keys = ["${digitalocean_ssh_key.root.fingerprint}"]
private_networking = true
count = 2
provisioner "remote-exec" {
inline = [
"curl -s -L https://www.getchef.com/chef/install.sh | bash -s -- -v 12.0.3",
]
connection {
user = "root"
key_file = "/home/gionn/.ssh/id_rsa"
}
}
}
resource "digitalocean_droplet" "db" {
image = "${var.do_image}"
name = "db"
region = "${var.do_region}"
size = "1gb"
ssh_keys = ["${digitalocean_ssh_key.root.fingerprint}"]
private_networking = true
provisioner "remote-exec" {
inline = [
"curl -s -L https://www.getchef.com/chef/install.sh | bash -s -- -v 12.0.3",
]
connection {
user = "root"
key_file = "/home/gionn/.ssh/id_rsa"
}
}
}
# Create a new SSH key
resource "digitalocean_ssh_key" "root" {
name = "Terraform key"
public_key = "${file("/home/gionn/.ssh/id_rsa.pub")}"
}
output "web_address" {
value = "${join(" ", digitalocean_droplet.web.*.ipv4_address)}"
}
output "db_address" {
value = "${digitalocean_droplet.db.ipv4_address}"
}
# Configure the DigitalOcean Provider
provider "digitalocean" {
token = "${var.do_token}"
}
variable "do_token" {
description = "DigitalOcean API Token"
}
# Configure the CloudFlare provider
provider "cloudflare" {
email = "${var.cloudflare_email}"
token = "${var.cloudflare_token}"
}
variable "cloudflare_email" {
description = "CloudFlare email"
}
variable "cloudflare_token" {
description = "CloudFlare api token"
}
variable "do_region" {
description = "Main region where to deploy"
default = "lon1"
}
variable "do_image" {
description = "Image name to deploy"
default = "ubuntu-14-04-x64"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment