Terraform
Commands
terraform initdownloads and installs configured providers to
.terraformfolder
terraform fmtformat configuration
terraform validatecheck config is syntactically valid and internally consistent
terraform applydata is written to
terraform.tfstate
terraform showprints out state with all values
terraform planGenerates and shows what will change when you run
terraform refreshupdates the local state file against real resources
ensures Terraform has an accurate view of what is in the current environment
terraform state listadvanced state management
terraform destroyterraform graph > graph.dotto check and later visualise dependencies between resources
terraform outputoutput values (only applicable while having an active state
Hashicorp Configuration Language (HCL)
Convention:
main.tf
all resources are declared
outputs.tf
outputs are declared
variables.tf
variables are declared
Variables
through command line
through file
through env variable
tf_
Locals
Local values allow you to assign a name to an expression, locals can make your code more readable
Output
Terraform output values allow you to export structured data about your resources. You can use this data to configure other parts of your infrastructure with automation tools, or as a data source for another Terraform workspace. Outputs are also necessary to share data from a child module to your root module.
terraform{} block
for each provider
sourceattribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by defaultMore about
providersection is HERE
provider{} block
A provider is a plugin that Terraform uses to create and manage your resources
You can use multiple provider blocks in your Terraform configuration to manage resources from different providers. You can even use different providers together. For example, you could pass the IP address of your AWS EC2 instance to a monitoring resource from DataDog.
resource{} block
More on resource documentation is HERE
Modules
Why?:
Organize configuration
Encapsulate configuration
Re-use configuration
Provide consistency and ensure best practices
What is it?
A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf files is a module. When you run Terraform commands directly from such a directory, it is considered the root module. So in this sense, every Terraform configuration is part of a module. You may have a simple set of Terraform configuration files such as:
In this case, when you run terraform commands from within the minimal-module directory, the contents of that directory are considered the root module.
Calling modules
Terraform commands will only directly use the configuration files in one directory, which is usually the current working directory. However, your configuration can use module blocks to call modules in other directories. When Terraform encounters a module block, it loads and processes that module's configuration files.
A module that is called by another configuration is sometimes referred to as a "child module" of that configuration.
Local and remote modules
Modules can either be loaded from the local filesystem, or a remote source. Terraform supports a variety of remote sources, including the Terraform Registry, most version control systems, HTTP URLs, and Terraform Cloud or Terraform Enterprise private module registries.
Example:
Code from tutorial:
Modules also have output values, which are defined within the module with the output keyword. You can access them by referring to module.<MODULE NAME>.<OUTPUT NAME>.
There is no provider block in this configuration. When Terraform processes a module block, it will inherit the provider from the enclosing configuration. Because of this, we recommend that you do not include provider blocks in modules.
How to use a module:
You provide values to variables defined in the module.
Last updated
Was this helpful?