Skip to content

Terraform

homepage-banner

Introduction

Terraform is an open-source tool for defining, provisioning, and managing infrastructure as code. It works with various infrastructure providers, including popular cloud providers like AWS, Azure, and Google Cloud, as well as on-premises providers like VMware and OpenStack.

Terraform’s core feature is its ability to create and manage infrastructure resources predictably. By defining infrastructure as code, users can version, review, collaborate on changes, and easily track changes and roll back to a previous state.

Terraform also offers advanced features like remote state management, workspaces, and modules, which organize and reuse code and manage infrastructure state.

Terraform has a large, active community that creates and shares custom modules and providers, making it easy to manage additional resources and services without writing custom code.

terraform-life-cycle

Basic Usage

Configure token

Initialize

terraform init

In the process of initializing the project, Terraform will parse the *.tf files in the directory and load the relevant provider plugins.

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Change log level

export TF_LOG=DEBUG

Plan

terraform plan

Apply

terraform apply
terraform state list

## passing var
terraform apply -var static_ip=false

Destroy

terraform destroy

Terraform functions

terraform console

> max(2,3)
3
> lower("HELLO WORLD")
"hello world"
> index(["apple", "orange", "banana"], "orange")
1
> formatdate("HH:mm",timestamp())
"01:32"

Conclusion

Overall, Terraform is a powerful tool for teams looking to automate and manage their infrastructure consistently and predictably across multiple providers.

Reference

  • https://www.terraform.io
  • Learning DevOps, Second Edition, A comprehensive guide to accelerating DevOps culture adoption with Terraform, Azure DevOps, Kubernetes, and Jenkins, Mikael Krief, 2022
  • https://opentofu.org/
  • https://www.fosstechnix.com/terraform-interview-questions-and-answers/
Leave a message







Disclaimer
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @