Tags: #terraform #environments #hcl #fastly
.
├── modules
│ ├── service-compute
│ │ ├── main.tf
│ │ ├── package-built-locally-via-cli.tar.gz
│ │ ├── provider.tf
│ │ └── variables.tf
│ └── service-vcl
│ ├── main.tf
│ ├── provider.tf
│ ├── variables.tf
│ └── vcl
│ └── main.vcl
├── prod
│ ├── inputs.tfvars
│ ├── main.tf
│ ├── provider.tf
│ └── variables.tf
└── stage
├── inputs.tfvars
├── main.tf
├── provider.tf
└── variables.tf
Within each directory (prod
and stage
) run terraform init
.
Then inside each directory run terraform plan -var-file="inputs.tfvars"
.
NOTE: I don’t show the contents for every file as most are the same as others. For example,
provider.tf
is the same across all directories. The stage and prod directories are all the same with the exception of theinputs.tfvars
(e.g. I use the default variable value in prod, where I provide my own value for the variable in stage) and thevariables.tf
(where I set different default values for each environment).
resource "fastly_service_compute" "service" {
name = "Compute Service"
domain {
name = "${var.subdomain}-compute.example.com"
}
package {
filename = "${path.module}/package-built-locally-via-cli.tar.gz"
source_code_hash = filesha512("${path.module}/package-built-locally-via-cli.tar.gz")
}
backend {
address = "httpbin.org"
name = "httpbin"
}
force_destroy = true
}
variable "subdomain" {
type = string
}
resource "fastly_service_v1" "service" {
name = "Example Service"
domain {
name = "${var.subdomain}.example.com"
}
backend {
address = "httpbin.org"
name = "httpbin"
}
vcl {
content = file("${path.module}/vcl/main.vcl")
main = true
name = "custom_vcl"
}
force_destroy = true
}
subdomain = "staging"
module "www" {
source = "../modules/service-vcl"
subdomain = var.subdomain
}
module "compute" {
source = "../modules/service-compute"
subdomain = var.subdomain
}
terraform {
required_providers {
fastly = {
source = "fastly/fastly"
version = "0.27.0"
}
}
}
variable "subdomain" {
type = string
default = "stage"
}
variable "subdomain" {
type = string
default = "www"
}