From b437f06c69dbc46a6b4422c7ac867ebbfc82b533 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 7 Oct 2021 21:11:05 +0300
Subject: [PATCH] Terraform structure and an empty example.

---
 terraform/README.md                    | 31 ++++++++++++++++++++++++++
 terraform/foo/dev.tf                   |  1 +
 terraform/foo/main.tf                  | 14 ++++++++++++
 terraform/foo/outputs.tf               |  0
 terraform/foo/providers.tf             |  9 ++++++++
 terraform/foo/staging.tf               |  1 +
 terraform/foo/variables.tf             |  7 ++++++
 terraform/library/README.md            |  3 +++
 terraform/library/aws/asg/README.md    |  1 +
 terraform/library/aws/asg/main.tf      |  0
 terraform/library/aws/asg/outputs.tf   |  0
 terraform/library/aws/asg/variables.tf |  0
 12 files changed, 67 insertions(+)
 create mode 100644 terraform/README.md
 create mode 100644 terraform/foo/dev.tf
 create mode 100644 terraform/foo/main.tf
 create mode 100644 terraform/foo/outputs.tf
 create mode 100644 terraform/foo/providers.tf
 create mode 100644 terraform/foo/staging.tf
 create mode 100644 terraform/foo/variables.tf
 create mode 100644 terraform/library/README.md
 create mode 100644 terraform/library/aws/asg/README.md
 create mode 100644 terraform/library/aws/asg/main.tf
 create mode 100644 terraform/library/aws/asg/outputs.tf
 create mode 100644 terraform/library/aws/asg/variables.tf

diff --git a/terraform/README.md b/terraform/README.md
new file mode 100644
index 0000000..a9cfef5
--- /dev/null
+++ b/terraform/README.md
@@ -0,0 +1,31 @@
+# Terraform modules
+
+This directory contains Terraform modules and a library of reusable modules
+under `library`. Each module deploys a complete service. Although services will
+be interconnected and there will be dependencies between services, the goal is
+to keep the modules whole so that applying the module deploys the entire
+service.
+
+## A word on environments
+
+Each module has a Terraform variables files (`.tfvars`) and a workspace for each
+environment that service is deployed to. This puts the emphasis on keeping the
+services the same across different environments with little changes, mainly for
+scale (eg. 2 instances instead of 5).
+
+## Common workflow
+
+In this example, deploying to the dev environment. A convention is used for the
+default workflow (have it be prod or dev or just not used).
+
+```
+terraform init
+terraform workspace new dev
+# Or if the workspace is already present:
+terraform workspace select dev
+terraform plan -var-file=dev.tfvars -out=tfplan
+# Review the changes to be applied.
+terraform apply tfplan
+```
+
+## Some more information here (like different providers, other conventions)
diff --git a/terraform/foo/dev.tf b/terraform/foo/dev.tf
new file mode 100644
index 0000000..7407b87
--- /dev/null
+++ b/terraform/foo/dev.tf
@@ -0,0 +1 @@
+instance_count = 2
diff --git a/terraform/foo/main.tf b/terraform/foo/main.tf
new file mode 100644
index 0000000..4e39eee
--- /dev/null
+++ b/terraform/foo/main.tf
@@ -0,0 +1,14 @@
+terraform {
+  backend "s3" {
+    bucket         = "tf-states"
+    key            = "foo.tfstate"
+    region         = "us-east-1"
+    encrypt        = true
+    dynamodb_table = "tf-locks"
+  }
+}
+
+locals {
+  env    = terraform.workspace == "default" ? "prod" : terraform.workspace
+  module = basename(abspath(path.root))
+}
diff --git a/terraform/foo/outputs.tf b/terraform/foo/outputs.tf
new file mode 100644
index 0000000..e69de29
diff --git a/terraform/foo/providers.tf b/terraform/foo/providers.tf
new file mode 100644
index 0000000..4a0d4ac
--- /dev/null
+++ b/terraform/foo/providers.tf
@@ -0,0 +1,9 @@
+provider "aws" {
+  default_tags {
+    tags = {
+      Environment = local.env
+      # Easy to track down which module created a resource.
+      Module      = local.module
+    }
+  }
+}
diff --git a/terraform/foo/staging.tf b/terraform/foo/staging.tf
new file mode 100644
index 0000000..3b1241f
--- /dev/null
+++ b/terraform/foo/staging.tf
@@ -0,0 +1 @@
+instance_count = 3
diff --git a/terraform/foo/variables.tf b/terraform/foo/variables.tf
new file mode 100644
index 0000000..9121b15
--- /dev/null
+++ b/terraform/foo/variables.tf
@@ -0,0 +1,7 @@
+variable "instance_count" {
+  type        = number
+  description = "The number of instances to deploy."
+  # If the convention is not to use the default workspace, delete the default
+  # value.
+  default     = 5
+}
diff --git a/terraform/library/README.md b/terraform/library/README.md
new file mode 100644
index 0000000..77ff62b
--- /dev/null
+++ b/terraform/library/README.md
@@ -0,0 +1,3 @@
+# Terraform module library
+
+A library of reusable modules (not to deployed by themselves).
diff --git a/terraform/library/aws/asg/README.md b/terraform/library/aws/asg/README.md
new file mode 100644
index 0000000..1bbab24
--- /dev/null
+++ b/terraform/library/aws/asg/README.md
@@ -0,0 +1 @@
+# Auto scaling group
diff --git a/terraform/library/aws/asg/main.tf b/terraform/library/aws/asg/main.tf
new file mode 100644
index 0000000..e69de29
diff --git a/terraform/library/aws/asg/outputs.tf b/terraform/library/aws/asg/outputs.tf
new file mode 100644
index 0000000..e69de29
diff --git a/terraform/library/aws/asg/variables.tf b/terraform/library/aws/asg/variables.tf
new file mode 100644
index 0000000..e69de29
-- 
GitLab