Terraform Provisioners

jaffar shaik
2 min readJan 5, 2021

Terraform Provisioners

A Provisioners can be used to model specific actions on the local machine or on a remote machine in order to prepare some tasks.

For example if you are creating a server and after that some software need to installed on top it for example in our case we are installing Httpd server. Terraform wont recommend to use provisioners .

“what if the provisioners Fails in this case “If a creation-time provisioner fails, the resource is marked as tainted. A tainted resource will be planned for destruction and recreation upon the next terraform apply . “Terraform does this because a failed provisioner can leave a resource in a semi-configured state

Types of provisioners:

In day to day activities we use 3 types of provioners.

  1. Local provisioners.
  2. Remote Provisioners
  3. File provisioner
  4. Local provisioners:

The local-exec provisioner invokes a local executable code on our local machine after a resource is created.

The following example shows we create a EC2 instance after that it will give us the attribute private IP address of the server.

provider "aws" {
access_key = ""
secret_key = ""
region = ""
}
resource aws_instance "myec23" {
ami = "ami-0e306788ff2473ccb"
instance_type = "t2.large"
provisioner "local-exec" {
command = "echo ${aws_instance.myec23.private_ip} >> private_ips.txt"
}
}
  1. Remote Provisioners:

Remote provisioners help to execute the scripts on remote servers after they are created.

  • For example if you are creating a server and after that some software need to installed on top it for example in our case we are installing Httpd server. Terraform wont recommend to use provisioners.
  • When we work with remote provisioners we have to include the script to be executed under INLINE BLOCK.
provider "aws" {
access_key = ""
secret_key = ""
region = "ap-south-1"
}
resource "aws_instance" "myec2" {
ami = "ami-026669ec456129a70"
instance_type = "t2.large"
key_name = "yyyy"
tags = {
name = "myec3"
}
provisioner "remote-exec" {
inline = [
"sudo yum install httpd -y",
"sudo service httpd on",
"sudo service httpd start"
]
}
connection {
type = "ssh"
user = "ec2-user"
private_key = file("./newpemkeyfile.pem")
host = self.public_ip
}
}

In the above we can see a Connection block .

  • the default connection type is SSH.
  • we need to specfify the path of private key pem file .In our case we are passing through File fumction.
  1. File Provioners:

The file provisioner is used to copy files or directories from the machine executing Terraform to the newly created resource.

  • They are widely used with Ansible
  • The usecase is if we need to copy some files from Master node to control nodes we can use File provioners.
Example:provisioner "file" {
source = "conf/configs.d"
destination = "/etc"
}
# Copies all files and folders in apps/app1 to D:/IIS/webapp1
provisioner "file" {
source = "apps/app1/"
destination = "D:/helloworld"
}
}

In the above Example Source refers Source refers to where the files are present and destination refers to where we need to copy the files.

That’s all about provisioners hope you might have enjoyed this article.

--

--