Local and Remote Exec Provisioners
Implement `local-exec` and `remote-exec` provisioners to run commands on the machine running Terraform or on the created resources respectively.
Local and Remote Exec Provisioners is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet Terraform Provisioners
What if you need to run a script or command after Terraform creates a resource? That's where provisioners come in!
Provisioners allow you to execute actions on the local machine or on a remote resource as part of its lifecycle. They bridge the gap between infrastructure creation and initial software setup.
Think of them as hooks that let you customize your resources right after they're born.
`local-exec`: Run Locally
The local-exec provisioner runs commands on the machine where Terraform is being executed.
This is useful for tasks like:
- Generating configuration files locally.
- Triggering local scripts or tools.
- Performing cleanup operations after resource destruction.
It's perfect for actions that don't need to happen on the cloud resource itself.
`local-exec` Basic Example
Let's see local-exec create a simple file on your local machine after a null resource is created.
A null_resource is a placeholder that doesn't create anything in the cloud, but it's handy for triggering provisioners.
resource "null_resource" "local_file_creator" {
provisioner "local-exec" {
command = "echo 'Local file created by Terraform!' > local_output.txt"
}
triggers = {
always_run = timestamp()
}
}When to Use `local-exec`
local-exec is great for:
- Post-deployment notifications: Send an email or Slack message.
- Local file generation: Create inventory files for Ansible or other tools.
- Pre-deployment checks: Run local validation scripts.
- Triggering CI/CD: Kick off a pipeline after infrastructure changes.
Remember, it operates from where you run terraform apply.
`remote-exec`: Run on Resource
Unlike local-exec, the remote-exec provisioner runs commands on the remote resource itself.
This is vital for initial setup tasks on a newly created server, such as:
- Installing software packages (e.g., web server, database).
- Configuring operating system settings.
- Deploying application code.
It requires network connectivity (like SSH for Linux or WinRM for Windows) to the target resource.
Connecting for `remote-exec`
For remote-exec to work, Terraform needs to connect to your resource.
Common connection types include:
- SSH: For Linux instances, requiring a private key.
- WinRM: For Windows instances, requiring credentials.
You'll typically configure connection details within the provisioner block or inherited from the resource.
`remote-exec` on an EC2
Here's how to use remote-exec to install Apache on a new AWS EC2 instance.
Note: This requires an EC2 instance to be provisioned and accessible via SSH. For simplicity, we assume an existing key pair and security group.
resource "aws_instance" "web_server" {
ami = "ami-0abcdef1234567890" # Replace with valid AMI ID
instance_type = "t2.micro"
key_name = "my-ssh-key" # Replace with your SSH key name
vpc_security_group_ids = ["sg-0123456789abcdef0"] # Replace with your SG ID
provisioner "remote-exec" {
inline = [
"sudo yum update -y",
"sudo yum install -y httpd",
"sudo systemctl start httpd",
"sudo systemctl enable httpd"
]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/my-ssh-key.pem")
host = self.public_ip
}
}
}When to Use `remote-exec`
remote-exec is ideal for:
- Bootstrapping servers: Install web servers, databases, or Docker.
- Application deployment: Copy application code and configure it.
- Configuration management: Run Ansible playbooks or Chef recipes directly.
- Post-creation validation: Verify services are running on the new instance.
It's your go-to for setting up a server's initial state.
Provisioners on Destroy
Provisioners can also run when a resource is being destroyed. This is useful for cleanup tasks.
You can specify when = destroy within a provisioner block. For example, to deregister an instance from a load balancer before it's terminated.
on_failure behavior can also be configured, determining what happens if a provisioner command fails. By default, it errors out.
Provisioner Quiz
Which provisioner would you use to install a web server (like Nginx) on a newly created virtual machine instance?
Provisioners: Local vs. Remote
We've explored Terraform provisioners, powerful tools for executing commands during the resource lifecycle.
local-execruns commands on the machine where Terraform is executed, useful for local scripting or notifications.remote-execruns commands directly on the provisioned resource, perfect for bootstrapping servers with software or configurations.
Provisioners help bridge the gap between infrastructure deployment and initial application setup. Next, we'll dive into advanced state management techniques!
Frequently asked questions
Is the “Local and Remote Exec Provisioners” lesson free?
Yes — the full text of “Local and Remote Exec Provisioners” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Local and Remote Exec Provisioners”?
Implement `local-exec` and `remote-exec` provisioners to run commands on the machine running Terraform or on the created resources respectively. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Local and Remote Exec Provisioners” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Querying Data with Data Sources
- Local and Remote Exec Provisioners
- Null Resources and Triggers
- Using External Data and Templates