This will organize your terraform execution! | Harsh Patel

This will organize your terraform execution!

Harsh Patel
FAUN — Developer Community 🐾
3 min readMay 31, 2023

--

Terraform is not a new kid around the block, it’s a way of codifying your infrastructure management, what you would do manually can be written down as code and can be tracked just like your application code on GitHub.

Why use terraform?

Imaging making a new ec2 instance using AWS Management Console, is easy right? But what if you need to make 100 of them? it’s error-prone and tedious. That’s why you write a terraform code (in Hashi Corp Configuration Language) and deploy it, same thing only less tedious and manageable!

Now, there are lots of terraform commands, I’ll show you an organized and situational execution of those commands!

Phase 1: Initializing and Preparing the Project

terraform init: When starting a new project or working on an existing one, the terraform init command is used to initialize the Terraform working directory. It downloads provider plugins and sets up the backend, making your code actionable.

-backend-config: provide backend configuration values

terraform fmt: To maintain code consistency and standardize the structure for better readability, the terraform fmt command automatically formats the Terraform configuration files.

-list: prints the paths of files that would be affected

Phase 2: Validation and Planning

terraform validate: the terraform validate command checks the syntax and validates the resource settings in your configuration files. It ensures that the configuration adheres to the provider's requirements, avoiding potential errors during deployment.

terraform plan: the terraform plan command generates an execution plan for your infrastructure changes. It analyzes the configuration and presents an overview of the proposed modifications without applying them.

  • -var: provide variables for the execution plan
  • -out: specifies a file path to save the execution plan

Phase 3: Infrastructure Deployment and Management

terraform apply: the terraform apply command is used to deploy your changes, it applies the changes outlined in the execution plan, creating or modifying resources as specified in your Terraform configuration.

-auto-approve: proceeds with execution without prompt

terraform destroy: the terraform destroy command demolishes your infrastructure including all resources managed by Terraform.

  • -target: specifies a resource to destroy instead of destroying all resources
  • -force: skips interactive approval before destroying resources

Additional Commands for infrequent use

terraform refresh: updates the local state file by retrieving the latest state from the provider, ensuring it matches any external changes made.

terraform state: manage and inspect the Terraform state.

terraform output: view the defined output values in your Terraform configuration, used for output of specified values like ARN, DNS, etc.

There are multiple ways of executing terraform commands, but this workflow includes the standard protocol without missing any required steps.

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week

--

--

IT Graduate. Loves to write articles, Practicing and Implementing Cloud and DevOps Technologies. Learning Agile and Adaptive Development.