From 0c9efde885187819889e4238218fce9f75be18f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Guilherme=20Vanz?= Date: Mon, 19 Apr 2021 18:11:33 -0300 Subject: [PATCH] Initial Terraform scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the first version of the Terraform script to deploy the infrastructure to run the Querido Diário workloads. Signed-off-by: José Guilherme Vanz --- .gitignore | 4 ++ terraform/database.tf | 9 +++ terraform/kubernetes.tf | 12 ++++ terraform/load_balancer.tf | 19 +++++++ terraform/provider.tf | 5 ++ terraform/registry.tf | 4 ++ terraform/spaces.tf | 5 ++ terraform/tag.tf | 3 + terraform/variables.tf | 114 +++++++++++++++++++++++++++++++++++++ terraform/versions.tf | 8 +++ 10 files changed, 183 insertions(+) create mode 100644 .gitignore create mode 100644 terraform/database.tf create mode 100644 terraform/kubernetes.tf create mode 100644 terraform/load_balancer.tf create mode 100644 terraform/provider.tf create mode 100644 terraform/registry.tf create mode 100644 terraform/spaces.tf create mode 100644 terraform/tag.tf create mode 100644 terraform/variables.tf create mode 100644 terraform/versions.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..052238c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +terraform.tfstate +terraform.tfstate.backup +terraform.tfvars +.terraform diff --git a/terraform/database.tf b/terraform/database.tf new file mode 100644 index 0000000..3c587a1 --- /dev/null +++ b/terraform/database.tf @@ -0,0 +1,9 @@ +resource "digitalocean_database_cluster" "postgres-example" { + name = var.postgres.name + engine = "pg" + version = var.postgres.version + size = var.postgres.size + region = var.region + node_count = var.postgres.node_count + tags = [var.default_tag] +} diff --git a/terraform/kubernetes.tf b/terraform/kubernetes.tf new file mode 100644 index 0000000..5cde077 --- /dev/null +++ b/terraform/kubernetes.tf @@ -0,0 +1,12 @@ +resource "digitalocean_kubernetes_cluster" "querido-diario" { + name = var.cluster_name + region = var.region + version = "1.20.2-do.0" + + node_pool { + name = "worker-pool" + node_count = var.node_count + size = var.node_size + tags = [var.default_tag] + } +} diff --git a/terraform/load_balancer.tf b/terraform/load_balancer.tf new file mode 100644 index 0000000..abb2b12 --- /dev/null +++ b/terraform/load_balancer.tf @@ -0,0 +1,19 @@ +resource "digitalocean_loadbalancer" "public" { + name = var.load_balancer.name + region = var.region + + forwarding_rule { + entry_port = var.load_balancer.forwarding_rule.entry_port + entry_protocol = var.load_balancer.forwarding_rule.entry_protocol + + target_port = var.load_balancer.forwarding_rule.target_port + target_protocol = var.load_balancer.forwarding_rule.target_protocol + } + + healthcheck { + port = var.load_balancer.healthcheck.port + protocol = var.load_balancer.healthcheck.protocol + } + + droplet_tag = var.default_tag +} diff --git a/terraform/provider.tf b/terraform/provider.tf new file mode 100644 index 0000000..06ac412 --- /dev/null +++ b/terraform/provider.tf @@ -0,0 +1,5 @@ +provider "digitalocean" { + token = var.do_token + spaces_access_id = var.do_spaces_access_key + spaces_secret_key = var.do_spaces_secret +} diff --git a/terraform/registry.tf b/terraform/registry.tf new file mode 100644 index 0000000..0cd110e --- /dev/null +++ b/terraform/registry.tf @@ -0,0 +1,4 @@ +resource "digitalocean_container_registry" "querido-diario-registry" { + name = var.registry.name + subscription_tier_slug = var.registry.subscription_tier_slug +} diff --git a/terraform/spaces.tf b/terraform/spaces.tf new file mode 100644 index 0000000..151d9f0 --- /dev/null +++ b/terraform/spaces.tf @@ -0,0 +1,5 @@ +resource "digitalocean_spaces_bucket" "querido-diario-spaces" { + name = var.spaces.name + region = var.region + acl = var.spaces.acl +} diff --git a/terraform/tag.tf b/terraform/tag.tf new file mode 100644 index 0000000..4899156 --- /dev/null +++ b/terraform/tag.tf @@ -0,0 +1,3 @@ +resource "digitalocean_tag" "foobar" { + name = var.default_tag +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..13bea8f --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,114 @@ +variable "do_token"{ + default = null + type = string +} + +variable "do_spaces_secret" { + default = null + type = string +} +variable "do_spaces_access_key"{ + default = null + type = string +} + +variable "cluster_name" { + default = "querido-diario" + type = string +} + +variable "node_size" { + default = "s-1vcpu-2gb" + type = string +} + +variable "node_count" { + default = 2 +} + +variable "region" { + default = "nyc3" +} + +variable "postgres" { + type = object({ + name = string + version = string + size = string + region = string + node_count = number + database = string + user = string + password = string + }) + default = { + name = "example-postgres-cluster" + version = "11" + size = "db-s-1vcpu-1gb" + region = "nyc3" + node_count = 1 + database = "querido-diario" + user = "querido-diario" + password = "querido-diario" + } +} + +variable "default_tag" { + default = "querido-diario" + type = string +} + +variable "registry" { + type = object({ + name = string + subscription_tier_slug = string + }) + default = { + name = "querido-diario" + subscription_tier_slug = "starter" + } +} + +variable "load_balancer" { + type = object({ + name = string + redirect_http_to_https = bool + forwarding_rule =object({ + entry_port = number + entry_protocol = string + target_port = number + target_protocol = string + }) + healthcheck = object({ + port = number + protocol = string + }) + }) + default = { + name = "querido-diario-load-balancer" + redirect_http_to_https = true + forwarding_rule = { + entry_port = 80 + entry_protocol = "http" + target_port = 80 + target_protocol = "http" + } + healthcheck = { + port = 22 + protocol = "tcp" + } + + } +} + +variable "spaces" { + type = object({ + name = string + acl = string + }) + default = { + name = "querido-diario" + acl = "public-read" + } +} + diff --git a/terraform/versions.tf b/terraform/versions.tf new file mode 100644 index 0000000..965ffdb --- /dev/null +++ b/terraform/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + digitalocean = { + version = "~> 2.7" + } + } + required_version = ">= 0.12" +}