00_Introduction
Last updated
Last updated
HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share in a consistent manner.
You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle.
Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.
Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs).
Providers enable Terraform to work with virtually any platform or service with an accessible API.
Terraform registery provides the list of available providers.
The core terraform workflow consists of three stages:
Write: You define resources, which may be across multiple cloud providers and services.
For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.
Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies.
For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.
Mutable infrastructure
Allows in place upgradation of infrastructure. If in case this upgradation fails, then we end up in a flunky state of infrastructure. So, this leads to thinking versioning as a continuous spectrum.
Immutable infrastructure
In this, we think infrastructure as discrete versions. So we dont upgrade in place, rather we create a completely different set of infrastructure with required upgraded/downgraded version of softwares and make a distinct machine.
Trade-off in this type of infrastructure is that, if the application has a state or data written to a source (say a local disk). In this case, we might have to externalize this data. The externalization of data allows the immutable pattern to be applied.
Terraform makes an immutable approach to infrastructure, reducing complexity of upgrading or modifying your services and infrastructure.
Terraform generates a plan and prompts you for your approval before modifying your infrastructure. It also keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment.
Terraform uses the state file to determine the changes to make to your infrastructure so that it will match your configuration.
State
Terraform must store state about your managed infrastructure and configuration. This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures.
This state is stored by default in a local file named "terraform.tfstate", but it can also be stored remotely, which works better in a team environment.
Terraform uses this local state to create plans and make changes to your infrastructure. Prior to any operation, Terraform does a refresh to update the state with the real infrastructure.
To read about the purpose of state visit here.
Terraform configuration files are declarative, meaning that they describe the end state of your infrastructure. You do not need to write step-by-step instructions to create resources because Terraform handles the underlying logic. Terraform builds a resource graph to determine resource dependencies and creates or modifies non-dependent resources in parallel. This allows Terraform to provision resources efficiently.
Terraform supports reusable configuration components called modules that define configurable collections of infrastructure, saving time and encouraging best practices. You can use publicly available modules from the Terraform Registry, or write your own.
Modules
They are containers for multiple resources that are used together. A module consists of a collection of .tf
and/or .tf.json
files kept together in a directory.
Modules are the main way to package and reuse resource configurations with Terraform.
Types of modules are as follows:
Root Module
Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.
Child Modules
A Terraform module (usually the root module of a configuration) can call other modules to include their resources into the configuration.
A module that has been called by another module is often referred to as a child module.
Child modules can be called multiple times within the same configuration, and multiple configurations can use the same child module.
Published Modules
In addition to modules from the local filesystem, Terraform can load modules from a public or private registry. This makes it possible to publish modules for others to use, and to use modules that others have published.
The Terraform Registry hosts a broad collection of publicly available Terraform modules for configuring many kinds of common infrastructure.
These modules are free to use, and Terraform can download them automatically if you specify the appropriate source and version in a module call block.