Skip to main content

Command Palette

Search for a command to run...

Day 61- Terraform Components

Published
3 min read
Day 61- Terraform Components

What are the components in Terraform?

  • Terraform CLI (Core): This is the command-line interface tool that users interact with to execute commands, generate execution plans, and apply changes. The Core is responsible for reading the configuration files, building a resource dependency graph, and determining what actions need to be taken to match the desired state.

  • Providers: Providers are plugins that allow Terraform to interact with APIs of external services and platforms, such as AWS, Azure, Google Cloud, Kubernetes, and GitHub. Each provider contains the necessary code to authenticate and manage resources within its specific platform.

  • Configuration Files: Written in the HashiCorp Configuration Language (HCL) or JSON, these files declare the desired infrastructure resources and their configurations (e.g., virtual machines, networks, databases). They are declarative, meaning users describe the end state, and Terraform figures out the how.

  • State Data: Terraform tracks the real-world infrastructure's current status and metadata in a state file (e.g., terraform.tfstate). This file acts as a source of truth, enabling Terraform to compare the desired configuration with the actual infrastructure and determine the changes required. For team collaboration and security, this is typically stored remotely (e.g., in HCP Terraform or an S3 bucket with locking).

  • Modules: Modules are reusable, encapsulated containers for multiple resources that are commonly used together, promoting consistency and best practices. They allow users to create complex infrastructure without starting from scratch and can be sourced from the public Terraform Registry or private registries.

What Does a Terraform Configuration File Look Like?

Task 1:

find purpose of basic Terraform commands which you'll use often

Core Workflow Commands

These commands represent the standard lifecycle for managing infrastructure:

  • terraform init: Initializes the working directory, downloads necessary provider plugins, and sets up the backend configuration. This is the first command you should run in a new or cloned configuration directory.

  • terraform plan: Creates an execution plan that shows what actions Terraform will take to achieve the desired state defined in your configuration files. This command allows you to review changes before they are applied.

  • terraform apply: Executes the operations defined in the plan to create, update, or delete infrastructure resources in the cloud provider. You will be prompted to approve the plan unless the -auto-approve flag is used.

  • terraform destroy: Removes all the infrastructure resources managed by the current Terraform configuration. Like apply, it requires confirmation unless -auto-approve is specified.

Other Important Commands

Terraform includes many other commands for managing state, inspecting output, and maintaining the configuration:

  • terraform validate: Checks whether the configuration is syntactically valid and internally consistent, regardless of any already-deployed infrastructure.

  • terraform fmt: Rewrites configuration files in the standard, canonical format and style.

  • terraform show: Provides human-readable output from a state file or a saved plan file to inspect the current state or planned operations.

  • terraform output: Displays the values of output variables defined in your configuration.

  • terraform import: Imports existing infrastructure resources into the Terraform state, allowing you to bring existing resources under Terraform management.

  • terraform state: A set of subcommands for advanced state management, such as listing resources (list), moving resources (mv), or removing resources (rm) within the state file.

  • terraform version: Shows the currently installed Terraform version.