Skip to content

Instantly share code, notes, and snippets.

@DanielChalk
Last active April 20, 2021 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielChalk/d68655b1c4a0f3756106fde62912bdde to your computer and use it in GitHub Desktop.
Save DanielChalk/d68655b1c4a0f3756106fde62912bdde to your computer and use it in GitHub Desktop.
Terraform Environment Modules (using modules to ecapsulate configuration)
# Looks like we are deploying one of our environments
cd terraform/environments/dev/us-east-1
# Initialise (sets up backend and downloads modules if any)
terraform init
# Let's see what's going to happen
terraform plan -out terraform.plan
# If you're happy, run the following
terraform apply terraform.plan
<project root>
├── README.md                     # Where your docs should start
└── terraform                     # Terraform code lives here
    ├── environments              # When environment modules live, with out configuration
    │   └── prod                  # Prod environment
    │       └── us-east-1         # Environment region (a production environment can consist of many regions)
    │           └── main.tf       # Where your implementation of _entry-point is
    └── modules
        ├── _entry-point          # Your entry point module, "_main" also works
        │   ├── main.tf
        │   ├── outputs.tf        # Only needed if there is informaiton you want to expose
        │   └── variables.tf
        ├── frontend
        │   ├── main.tf
        │   ├── outputs.tf
        │   └── variables.tf
        ├── backend
        │   ├── main.tf
        │   ├── outputs.tf
        │   └── variables.tf
        └── database
            ├── main.tf
            ├── outputs.tf
            └── variables.tf
// main.tf
provider "aws" {
region = "us-east-1"
}
module "this" {
source = "../../modules/_entry-point"
domain = "dev.example.com"
}
output "this" {
value = module.this
}
// variables.tf
variable "domain" {
type = string
}
// main.tf
// These are something youd implement
module "database" {
source = "./database"
}
module "backend" {
source = "./backend"
domain = "backend.${var.domain}"
database_url = module.database.url
database_username = module.database.username
database_password = module.database.password
}
module "frontend" {
source = "./frontend"
domain = var.domain
backend_endpoint = module.backend.endpoint
}
// outputs.tf
output "database" {
value = module.database
}
output "backend" {
value = module.backend
}
output "frontend" {
value = module.frontend
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment