šŸš€ Unveiling Terraformā€™s Local Exec Provisioner and Remote Exec Provisioner: A Comprehensive Guide

Ayushmaan Srivastav
2 min readApr 13, 2024

--

Introduction to Terraform Provisioners āœØ

In the realm of Infrastructure as Code (IaC), Terraform stands tall as a pioneering tool. It enables users to define and provision infrastructure resources declaratively. Among its arsenal are provisioners, powerful tools that execute scripts or commands on local or remote machines during resource creation or destruction.

Understanding Local Exec Provisioner šŸ› ļø

The Local Exec Provisioner empowers Terraform to execute scripts or commands locally on the machine where Terraform is running. It is ideal for tasks such as initializing configurations or setting up dependencies.

Practical Example:

resource "aws_instance" "example" {
# Resource configuration

provisioner "local-exec" {
command = "echo 'Instance created'"
}
}

Delving into Remote Exec Provisioner šŸŒ

On the other hand, the Remote Exec Provisioner allows Terraform to execute scripts or commands on the provisioned resources themselves. Itā€™s useful for tasks like software installation or configuration management.

Practical Example:

resource "aws_instance" "example" {
# Resource configuration

provisioner "remote-exec" {
inline = [
"sudo apt update",
"sudo apt install nginx -y",
]
}
}

Workspace: A Multi-environment Savior šŸŒŸ

Workspaces in Terraform provide a means to manage multiple states of the same configuration. They are particularly handy when you need to deploy the same infrastructure in different environments, like development, staging, or production.

Practical Example:

terraform {
workspace "development"
}

Harnessing the Power of Lookup Function šŸ”

The Lookup function in Terraform enables dynamic retrieval of values from maps or lists. Itā€™s incredibly useful when you need to reference variable values based on dynamic conditions.

Practical Example:

variable "instance_types" {
type = map
default = {
"development" = "t2.micro"
"production" = "t2.large"
}
}

resource "aws_instance" "example" {
# Resource configuration

instance_type = lookup(var.instance_types, terraform.workspace)
}

Conclusion šŸŽ‰

With Terraformā€™s local and remote exec provisioners, workspaces, and the lookup function, youā€™re equipped to streamline your infrastructure provisioning process. Whether youā€™re initiating local tasks, executing commands remotely, managing multiple environments, or dynamically fetching values, Terraform empowers you to orchestrate infrastructure with finesse.

--

--

No responses yet