Terraform is an open source IaC (infrastructure as code) tool that provides a consistent workflow in the CLI for managing hundreds of cloud services. Terraform converts cloud APIs to declarative configuration files.
We usually deploy the infrastructure in several environments that we use for development, staging, testing, and production. It is very important to write a Terraform configuration that is easy to maintain and scale to provision the infrastructure in the specified environments.
In this article, we will consider several ways to prepare infrastructure in different environments. Each of them has its own advantages and disadvantages.
Introduction
. . β . Terraform .
Terraform β IaC, , .
.
β 1
terraform.tfvars. , .
. , . outputs.tf, providers.tf, variables.tf . . terraform : init, plan, apply
.
( 1)
:
- ,
- .
:
- ;
- , .
β 2
, terraform.tfvars. , .
main.tf variables.tf , terraform . , , :
// Dev Environment terraform plan --var-file="tfvars/environment/dev.tfvars" // QA Environment terraform plan --var-file="tfvars/environment/qa.tfvars" // Prod Environment terraform plan --var-file="tfvars/environment/prod.tfvars"
( 2)
:
- ;
- , .
:
- ;
- , .
Terraform default. , , , . , , default.
terraform workspace
. . , terraform workspace new
.
β , . Terraform β root-. Root- , .tf .
, . , . , .
. , .
Terragrunt
Terragrunt β , DRY , Terraform .
- Terraform β - IaC, CLI .
- , , , .
- Terraform.
- β . .
- Terraform default. , , , .
- β , . , .
- Terragrunt β , DRY , Terraform .
, Terraform. . .