What is Terraform?
Terraform
is aninfrastructure as code (IaC) tool
that allows you tocreate
,manage
, andupdate infrastructure resources
such asvirtual machines
,networks
, andstorage in a repeatable
,scalable
, andautomated way
.Terraform is an
open-source
tool created byHashiCorp
and is written in theGo programming language
. It is available for Windows, Linux, and macOS
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
Initializes
a new or existing Terraform configuration in the current directory. This command downloads the necessary providerplugins
and sets up theworking directory
.What it does: Prepares your project for Terraform.
- Command: init -upgrade
terraform init -upgrade
Upgrades
the installed Terraform plugins to the latest version, if available. This command is useful whenupgrading Terraform versions
or when there are new versions of the plugins that need to be used.What it does: It Upgrades the installed Terraform to its latest version.
- Command:
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.What it does: Shows what Terraform will do without making changes.
- 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.What it does: Make changes to your infrastructure based on the plan.
- Command: validate
terraform validate
Checks the syntax
andconfiguration of your Terraform files
for errors without actually creating or modifying resources.What it does: Check your configuration for errors.
- 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.what it does: It helps ensure that your code is easy to read, maintain, and collaborate on by enforcing a consistent code structure.
- Command: Destroy
terraform destroy
Destroys are all
resources
defined in yourconfiguration,
effectively tearing down your infrastructure. It also prompts for confirmation before proceeding.What it does: Removes all resources created by Terraform.
- Command: show
terraform show
Inspects the
current state
of theinfrastructure
andoutputs
theresource
information.What it does: Shows the current state of your infrastructure.
- 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.What it does: Generates a visual graph of resource dependencies.
- Command:
terraform import
Imports existing
infrastructure
into your Terraformstate
. This allows you to take resources you've created by some other means and bring it under Terraform management.What it does: Imports existing infrastructure into Terraform.
- 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.What it does: Reads an output from a state file.
- Command: Provider
terraform providers
Prints a tree of the
providers
used in theconfiguration
. This is useful for finding out the providers required for a configuration.What it does: Lists the providers (e.g., AWS, Azure) used in your configuration.
- 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.What it does: Manages workspaces (e.g., dev, staging, prod).
- Command: Refresh
terraform refresh
Updates the
state
ofresources
in a Terraformstate
file. This is useful when thestate
resource is changed by some other means.What it does: Updates the state file.
- Command: version
terraform version
Prints the
Terraform version
. This is useful for checking which version of Terraform is running.What it does: Print the Terraform version.
Who are Terraform's main competitors?
AWS CloudFormation: AWS CloudFormation is a service provided by Amazon Web Services (AWS) for defining and deploying infrastructure as code on the AWS platform. It's a direct competitor for those who are heavily invested in AWS.
Ansible: Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It's a direct competitor for those who are heavily invested in Red Hat.
Chef and Puppet: Chef and Puppet are traditional configuration management tools but can also be used for infrastructure automation. While they differ in their approach from Terraform, they can be considered competitors in the broader infrastructure automation landscape.
Packer: Packer is an open-source tool for creating identical machine images for multiple platforms from a single source configuration. It's a direct competitor for those who are heavily invested in HashiCorp.
Kubernetes: While not a direct competitor, Kubernetes, the container orchestration platform, plays a significant role in managing application deployments and infrastructure at scale. It complements tools like Terraform, and there's some overlap in use cases.
Cloud Foundry: Cloud Foundry is an open-source, multi-cloud application platform as a service (PaaS) governed by the Cloud Foundry Foundation, a 501(c)(6) organization. It's a direct competitor for those who are heavily invested in Cloud Foundry.
Happy Learning :)