Table of contents
What is Terraform?
Terraform is an open-source tool that lets you define and automate your infrastructure using code. You write configurations to describe what resources you want (like servers, databases, or networks), and Terraform handles the process of creating and managing those resources across various cloud providers and platforms. It makes infrastructure management more efficient and consistent.
Task 1:
Terraform is an infrastructure-as-code (IAC) tool used for building, changing, and versioning infrastructure efficiently. It has several commands to manage your infrastructure code and the resources it provisions.
Here are some commonly used Terraform commands:
- Command: init
terraform init
terraform init
initializes a Terraform project. It downloads necessary plugins, configures backends, and prepares your project for infrastructure management. It's the first step when working with Terrafor
- Command: init -upgrade
terraform init -upgrade
terraform init -upgrade
updates your provider plugins to their latest versions while initializing your Terraform project. It keeps your project current and takes advantage of the latest features and bug fixes.
- Command: plan
terraform plan
- Generates a
execution plan
description of what Terraform will do toreach the desired state
define your configuration. It shows which resources will be created, updated, or destroyed.
- Command: Apply
terraform apply
Applies the changes
the defined in your Terraform configuration tocreate
orupdate resources
based on the execution plan. It prompts for confirmation before making changes.
- Command: validate
terraform validate
Checks the syntax
andconfiguration of your Terraform files
for errors without actually creating or modifying resources.
- Command: fmt
terraform fmt
Rewrites
Terraform configuration files to acanonical format
and style. This applies standard formatting conventions to the code to make it easier to read and understand.
- Command: Destroy
terraform destroy
- Destroys are all
resources
defined in yourconfiguration,
effectively tearing down your infrastructure. It also prompts for confirmation before proceeding.
- Command: show
terraform show
- Inspects the
current state
of theinfrastructure
andoutputs
theresource
information.
- Command: Graph
terraform graph
- terraform graph: Creates a visual representation of the Terraform
resources
defined in yourconfiguration
. It outputs the generated graph in the DOT format.
- Command: terraform import
terraform import
terraform import
is used to bring existing infrastructure resources under Terraform's control. It maps existing resources to Terraform configurations, enabling you to manage them alongside other infrastructure as code.
- Command: Output
terraform output
- Reads the
state
file andoutputs
thevalue
of aspecified output variable
. This is useful for fetching the value of a specific variable.
- Command: Provider
terraform providers
terraform providers
lists the available provider plugins in your Terraform configuration, showing the configured providers and their versions. It helps confirm the provider setups in your project.
- Command: Workspace
terraform workspace
Used to create, list, and delete Terraform
workspaces
. Workspaces allow you to manage multiple distinct sets of infrastructure resources with the same configuration files.- Command: Refresh
terraform refresh
terraform refresh
updates the Terraform state file to match the current state of your infrastructure resources by querying their actual status, without making any changes to the resources themselves.
- Command: version
terraform version
terraform version
shows the currently installed Terraform version, helping you confirm the version you are working with.