DEV Community

Sreya Sharma
Sreya Sharma

Posted on

Diving into Terraform Provisioners

Day 15 was exciting because I learned about Terraform provisioners — a feature that lets you run scripts or commands on your resources during creation or destruction. This added a new dimension to Terraform: it’s not just about creating infrastructure, it can also configure it automatically.

What Are Provisioners?

Provisioners allow Terraform to perform actions on a resource after it’s created or before it’s destroyed.
Think of them as automation hooks for your infrastructure.

There are two main types:

  • remote-exec – runs commands on remote machines (like EC2 instances)
  • local-exec – runs commands on your local machine

Why Provisioners Are Useful

  • Installing software or packages on a server after creation
  • Running initialization scripts
  • Copying files to resources
  • Automating post-deployment configuration

Provisioners help bridge the gap between Terraform and configuration management tools.

Using local-exec

  • This runs a command on your local machine instead of the resource.
  • Useful for notifications, scripts, or automation outside the cloud provider.

remote-exec

  • This installs Nginx immediately after the EC2 instance is created.
  • The connection block tells Terraform how to access the remote machine.

File Provisioners

  • Uploading a configuration or script to a server
  • Copying sensitive files like certificates securely

Key Takeaways

  • Provisioners are optional; Terraform recommends using them sparingly.
  • They are useful for bootstrapping and post-creation configuration.
  • remote-exec for remote machines, local-exec for local commands.
  • Overusing provisioners can lead to imperative code, which goes against Terraform’s declarative nature.

Top comments (0)