Terraform

Commands

  • terraform init

    • downloads and installs configured providers to .terraform folder

  • terraform fmt

    • format configuration

  • terraform validate

    • check config is syntactically valid and internally consistent

  • terraform apply

    • data is written to terraform.tfstate

  • terraform show

    • prints out state with all values

  • terraform plan

    • Generates and shows what will change when you run

  • terraform refresh

    • updates the local state file against real resources

    • ensures Terraform has an accurate view of what is in the current environment

  • terraform state list

    • advanced state management

  • terraform destroy

  • terraform graph > graph.dot

  • terraform output

    • output 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 source attribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by default

  • More about provider section 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

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?