LibraryUnderstanding Provisioners

Understanding Provisioners

Learn about Understanding Provisioners as part of Terraform Infrastructure as Code Mastery

Understanding Terraform Provisioners

Terraform provisioners are a powerful, albeit sometimes complex, feature that allows you to execute scripts or commands on a newly created resource or a resource that is being destroyed. They are primarily used for tasks that Terraform itself cannot manage directly, such as installing software, configuring services, or running initial setup scripts.

What are Terraform Provisioners?

Provisioners are blocks within a Terraform resource that define actions to be taken on the resource after it has been created or before it is destroyed. They act as a bridge between Terraform's declarative infrastructure management and imperative configuration tasks.

Provisioners execute scripts on managed resources.

Terraform provisioners allow you to run scripts on your infrastructure resources after they are created or before they are destroyed. This is useful for tasks like software installation or initial configuration.

Terraform provisioners are a mechanism to execute scripts on remote or local machines. They are typically used to perform tasks that are outside the scope of Terraform's declarative model, such as installing packages, starting services, or running custom setup commands. There are two main types: remote-exec and file.

Types of Provisioners

Terraform offers two primary types of provisioners, each serving a distinct purpose:

Provisioner TypeDescriptionUse Case Example
fileCopies files from the local machine to the remote resource.Uploading a configuration file or an application binary.
remote-execExecutes commands on the remote resource.Running apt-get update and apt-get install nginx on a new EC2 instance.

How Provisioners Work

When Terraform creates a resource, it first provisions the infrastructure itself. Once the resource is successfully created and accessible, Terraform then executes any configured provisioners. For

code
remote-exec
, this typically involves establishing an SSH connection to the resource. For
code
file
, it involves transferring files via SCP.

What is the primary purpose of a Terraform provisioner?

To execute scripts or commands on a resource after it's created or before it's destroyed, for tasks Terraform can't manage declaratively.

It's important to note that provisioners run after the resource has been created by Terraform. This means they rely on the resource being in a state where remote execution or file transfer is possible (e.g., the instance is running and SSH is available).

Provisioner Configuration

Provisioners are defined within the resource block they are associated with. They accept arguments like

code
connection
to specify how to connect to the resource (e.g., SSH details) and
code
command
or
code
source
/
code
destination
for the scripts or files.

Consider a simple aws_instance resource. A remote-exec provisioner can be added to install and start an Nginx web server. The connection block specifies the SSH host, user, and private key. The inline argument within remote-exec lists the commands to be executed sequentially on the remote instance.

📚

Text-based content

Library pages focus on text content

The

code
connection
block is crucial for
code
remote-exec
and
code
file
provisioners. It defines how Terraform will communicate with the target resource. Common connection methods include SSH, WinRM, and local execution.

When to Use Provisioners (and When Not To)

Provisioners are best suited for initial setup or one-time configuration tasks. However, they can introduce complexity and make your Terraform configurations less declarative and more imperative. For ongoing configuration management, tools like Ansible, Chef, Puppet, or cloud-native configuration management services are generally preferred.

Think of provisioners as the 'bootstrapping' mechanism for your infrastructure, not as a full-fledged configuration management system.

It's also important to consider the order of operations. Terraform executes provisioners in the order they appear within the resource block. You can also use

code
when = destroy
to specify provisioners that run only during resource destruction.

What is a potential drawback of using Terraform provisioners extensively?

They can make Terraform configurations less declarative and more imperative, and for ongoing management, dedicated configuration management tools are often better.

Best Practices and Considerations

When using provisioners, keep the scripts idempotent (meaning running them multiple times has the same effect as running them once). This helps prevent unexpected behavior if Terraform needs to re-apply changes. Also, be mindful of security, especially when handling sensitive information in scripts or connection details.

Consider using

code
triggers
within provisioners to force re-execution when certain conditions change, but use this sparingly. For complex workflows, it's often better to have your provisioner script call out to a more robust configuration management tool.

Learning Resources

Terraform Provisioners Documentation(documentation)

The official HashiCorp documentation on Terraform provisioners, covering their purpose, types, and configuration.

Terraform Provisioner: File and Remote-Exec(video)

A video tutorial explaining the file and remote-exec provisioners with practical examples.

Terraform Provisioners: When and How to Use Them(blog)

A blog post discussing the use cases, best practices, and potential pitfalls of Terraform provisioners.

Terraform Provisioner Connection Block(documentation)

Detailed documentation on configuring the connection block for provisioners, including authentication methods.

Terraform Provisioner Example: Installing Nginx(documentation)

An official Terraform example demonstrating the use of remote-exec provisioner to install Nginx on an EC2 instance.

Terraform Provisioner: Best Practices(blog)

A HashiCorp blog post offering insights and best practices for using provisioners effectively in Terraform.

Understanding Terraform Provisioners(tutorial)

A community tutorial that walks through the concepts and practical application of Terraform provisioners.

Terraform Provisioner: File Provisioner Example(video)

A video demonstration focusing specifically on the file provisioner for transferring files.

Terraform Provisioner vs. Configuration Management(blog)

An article comparing Terraform provisioners with dedicated configuration management tools and when to use each.

Terraform Provisioner: Destroy Provisioners(documentation)

Documentation explaining how to configure provisioners that execute during resource destruction.