Understanding Terraform Provisioners with Real-Time EC2 Demo

 

terrafform provisioners file - remote exec tutorial

Hello friends, this is our 9th post in our Terraform tutorial series.

We’ve already covered all the important topics from the basics, step by step, in the previous topics. In this post, I’ll explain about Terraform provisioners, including:

1. What are Terraform provisioners?
2. Types of provisioners
3. What is file provisioner?
4. What is remote-exec provisioner?

You can also Watch this tutorial demo on our YouTube Channel

What are Terraform Provisioners?

Provisioners are like post-setup scripts. They help you perform additional tasks after Terraform has applied the configuration.

For example:
  • Copying files
  • Running installation scripts
  • Configuring services
  • Executing any custom commands

We have a few types of provisioners:
  • file
  • local-exec
  • remote-exec

In this post, we’ll primarily focus on file and remote-exec type provisioners, because when using Terraform for infrastructure provisioning, we typically need to set up everything on the remote server after the resource is created.

What is File Provisioner?

As the name suggests, the file provisioner copies files from your local machine to a remote server.

  • The file must exist locally.
  • Terraform will copy it to the remote machine using a connection.
  • The connection type must be defined — either SSH for Linux or WinRM for Windows.
provisioner "file" {
  source = "index.html"
  destination = "/home/ubuntu/index.html"
}

This provisioner is used only to copy files or directories, nothing more. So it will have source and destination parameters to define the file to be copied from source and destination location.

What is Remote-Exec Provisioner?

This provisioner helps you run commands on the remote server. It’s useful for post-setup tasks like:
  • Installing packages
  • Configuring services
  • Running shell scripts
provisioner "remote-exec" {
  inline = [
    "sudo apt-get update",
    "sudo apt-get -y install apache2",
    "sudo cp -rf /home/ubuntu/index.html /var/www/html/index.html",
    "sudo systemctl start apache2"
   ]
}

For both types, we need connection block to establish a connection to the target resources.

connection {
  type = "ssh"
  user = "ubuntu"
  private_key = file("demov2.pem")
  host = aws_instance.server1.public_ip
}    

Provisioners and connection blocks must be inside your resource block, not outside it.

Steps:

  1. Define the connection block
  2. Add the file provisioner to copy the index.html
  3. Add the remote-exec provisioner to install Apache and move the file

main.tf


provider "aws" {
  region     = "us-east-1"
}

resource "aws_instance" "server1" {
  ami                         = "ami-0e86e20dae9224db8"
  instance_type               = "t2.micro"
  subnet_id                   = "subnet-0105b1aef1e7755cd"
  key_name                    = "demov2"
  ebs_block_device {
    device_name = "/dev/sda1"
    volume_size = 10
  }
  associate_public_ip_address = "true"
  vpc_security_group_ids      = ["sg-04dd813e22c5a0b2f"]
  tags = {
    Name = "server1"
  }

connection {
  type = "ssh"
  user = "ubuntu"
  private_key = file("demov2.pem")
  host = aws_instance.server1.public_ip
}

provisioner "file" {
  source = "index.html"
  destination = "/home/ubuntu/index.html"
}

provisioner "remote-exec" {
  inline = [
    "sudo apt-get update",
    "sudo apt-get -y install apache2",
    "sudo cp -rf /home/ubuntu/index.html /var/www/html/index.html",
    "sudo systemctl start apache2"
   ]
}

}


Above file will deploy an ec2 instance on aws and copy index.html to target location.
Then it will setup a apache2 webserver for us as post setup scripts.

Create index.html in your current directory and keep your SSH key for making connection.

Once things are ready, you can apply the terraform configuration.
terraform init
terraform apply --auto-approve
They’re useful for lightweight configurations, especially during resource creation. For long-term, robust service management, consider integrating with config management tools.




Post a Comment

0 Comments