How to create infrastructure in different environments using Terraform

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.







image







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, , .

.









Terraform, .







β€” 1



terraform.tfvars. , .







. , . outputs.tf, providers.tf, variables.tf . . terraform : init, plan, apply



.







image

( 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"
      
      





image

( 2)







:







  • ;
  • , .


:







  • ;
  • , .




Terraform default. , , , . , , default.

terraform workspace



. . , terraform workspace new



.









β€” , . Terraform β€” root-. Root- , .tf .







, . , . , .







. , .







image







Terragrunt



Terragrunt β€” , DRY , Terraform .







.









  • Terraform β€” - IaC, CLI .
  • , , , .
  • Terraform.
  • β€” . .
  • Terraform default. , , , .
  • β€” , . , .
  • Terragrunt β€” , DRY , Terraform .


, Terraform. . .








All Articles