Remote-Exec Provisioner
The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to run a configuration management tool, bootstrap into a cluster, etc. The remote-exec provisioner supports both ssh and winrm type connections.
# creating instance.
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = aws_key_pair.key-tf.key_name
vpc_security_group_ids = ["${aws_security_group.allow_tls.id}"]
tags = {
Name = "first-tf-instance"
}
user_data = file("${path.module}/script.sh")
connection {
type = "ssh"
user = "ubuntu"
private_key = file("${path.module}/id_rsa")
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
"ifconfig > /tmp/ifconfig.output",
"echo 'hello gaurav'>/tmp/test.txt"
]
}
provisioner "remote-exec" {
script = "./testscript.sh"
}
}
Demo: