DEV Community

Randika Madhushan Perera
Randika Madhushan Perera

Posted on

Deploying Apps to AWS with Terraform - IaC

4. Understanding fmt, validating, planning, and applying

These commands are fundamental to anyone working with Terraform, a tool that automates the creation, modification, and deletion of infrastructure.

Key Terraform Commands:

  • Terraform Init: This command is the starting point for all Terraform projects. It initializes the working folder, downloads necessary plugins and providers, and sets up the backend for storing state files. It's crucial to run terraform init before deploying any code, as it prepares your environment for Terraform operations.

  • Terraform Format (fmt): The terraform fmt command is used to format your Terraform code. It ensures that your code adheres to a consistent style and formatting standard, which is especially beneficial when working in teams. This command organizes your code without modifying its functionality and is safe to run at any project stage.

  • Terraform Validate: This command checks your Terraform code for syntax errors and internal consistency. It validates the code for mistakes like typos or misconfigured resources. terraform validate depends on having run terraform init at least once and is a non-destructive way to ensure your code is ready for deployment.

  • Terraform Plan: This is one of the most frequently used commands. terraform plan creates an execution plan, detailing what actions Terraform will perform when applying your code. It checks the current state of resources against the desired state defined in your code and highlights the differences. Saving this plan to a file can be beneficial for review and later use.

  • Terraform Apply: This is the command that enacts changes to your infrastructure based on the execution plan. It prompts for confirmation before applying changes (which can be bypassed with a flag for automation). This command is crucial as it brings your infrastructure to the desired state as per your Terraform code.

Summary:

Terraform Init: Initializes the Terraform environment.
Terraform Format (fmt): Formats Terraform code.
Terraform Validate: Checks for syntax and configuration errors.
Terraform Plan: Creates an action plan for Terraform.
Terraform Apply: Applies the execution plan to the infrastructure.

Top comments (0)