Terraform
What is Terraform?
Terraform, developed by HashiCorp, is an Infrastructure as Code (IaC) tool that allows developers and operations teams to define and provide datacenter infrastructure using a declarative configuration language.
With Terraform, you describe your complete infrastructure as code, even as it spans multiple service providers. Your servers may come from AWS, your DNS may come from CloudFlare, and your database might come from Heroku. Terraform will build all these resources across all these providers in parallel.
Why is Terraform Important?
Infrastructure as Code
By managing your infrastructure as code, your infrastructure is versioned and tracked in your version control system, just like your application code. This provides the benefits of code review, iteration, and versioning.
Idempotent
Terraform's operations are idempotent, meaning running them multiple times will not result in different outcomes. This makes Terraform safe for automation and orchestration.
Provider Agnostic
Terraform is agnostic to the underlying platforms by supporting providers. This allows a unified workflow for provisioning across a multitude of platforms like AWS, Google Cloud, Azure, and many others.
Multi-tier Applications
Terraform supports complex multi-tier application environments. It has the ability to handle dependencies between resources, ensuring they are created in the appropriate order.
Terraform Best Practices
Keep your configurations DRY
Do not Repeat Yourself (DRY). Make use of modules to group reusable resources and use them across your infrastructure. 1 4
Smaller is Better
Split your configuration into small, manageable chunks. This allows changes to be made without impacting unrelated parts of your infrastructure. 4
Remote State Storage
Always configure remote backends to store your state files. This will allow your state to be easily shared and locked between your team members. 1
Sensitive Data
Never hard-code sensitive data. Always use variables for sensitive data and provide them securely. 1 2
Version Pinning
Always pin your provider to a specific version. This ensures that your infrastructure does not break due to new versions. 4
Plan and Review
Always run terraform plan and review it before applying any changes. This helps catch
any unintended modifications before they are applied. 1
Use .gitignore
Add .terraform directory and .tfstate* files in .gitignore. You should never
commit the state files as they may contain sensitive data. 1 3
Use Workspaces
Use workspaces if you need to manage multiple environments like staging, production etc. 1
-
Terraform Best Practices by HashiCorp, the creators of Terraform. ↩↩↩↩↩↩
-
AWS Best Practices for Terraform: A useful guide if you are using AWS with Terraform. ↩
-
Terraform Best Practices: An open-source repository on GitHub that contains a set of guided labs based on best practices. ↩