🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terrafrom Tutorials – remote-exec provisioner using AWS & Azure providers

the remote-exec provisioner in Terraform is used to execute commands remotely on a target resource after it’s been created. This is often used for post-deployment configuration or initialization tasks on virtual machines, containers, or other infrastructure resources.

Here’s a general example of how you can use the remote-exec provisioner:


resource "aws_instance" "example" {
  ami           = "ami-12345678"  # Replace with your desired AMI ID
  instance_type = "t2.micro"
}

provisioner "remote-exec" {
  inline = [
    "echo This is a remote command.",
    "echo You can execute multiple commands here.",
    "echo Remember that each command is executed independently.",
  ]
}
Code language: PHP (php)

provider "aws" {
profile = "default"
region = "us-west-2"
}
resource "aws_key_pair" "example" {
key_name = "examplekey"
public_key = file("~/.ssh/terraform.pub")
}
resource "aws_instance" "example" {
key_name = aws_key_pair.example.key_name
ami = "ami-04590e7389a6e577c"
instance_type = "t2.micro"
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/terraform")
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo amazon-linux-extras enable nginx1.12",
"sudo yum -y install nginx",
"sudo systemctl start nginx"
]
}
}

The remote-exec provisioner requires a connection block to specify how Terraform will connect to the remote resource. The connection block can be defined in the same resource block as the remote-exec provisioner, or it can be defined in a separate resource block.

The remote-exec provisioner has the following arguments:

  • inline: A list of commands to be executed. The provisioner uses a default shell unless you specify a shell as the first command (eg., #!/bin/bash).
  • script: A path to a local script that will be copied to the remote resource and then executed.
  • scripts: A list of paths to local scripts that will be copied to the remote resource and then executed, one after the other.
  • timeout: The maximum amount of time to wait for the commands to complete, in seconds.
  • on_failure: The action to take if the commands fail. Valid values are “continue” and “fail”.

resource "aws_instance" "example" {
  ...
}

resource "null_resource" "bootstrap" {
  depends_on = ["aws_instance.example"]

  provisioner "remote-exec" {
    connection {
      instance_id = aws_instance.example.id
      user = "ubuntu"
    }

    inline = ["sudo apt-get update", "sudo apt-get install -y terraform"]
    timeout = 600
    on_failure = "fail"
  }
}Code language: JavaScript (javascript)

Remote Exec in Windows


  provisioner "remote-exec" {
    on_failure = "continue"
    inline = [
      "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command \"Write-Output 'Hello from Terraform'\"",
    ]
  }

  connection {
    type        = "winrm"
    user        = "Administrator"
    password    = "your_password"
    host        = self.public_ip_address
    https       = false
    port        = 5985
    timeout     = "10m"
  }
}Code language: PHP (php)

  connection {
    type     = "winrm"
    user     = "Administrator"
    password = "your_password"
    host     = self.public_ip_address
    https    = false
    port     = 5985
    timeout  = "10m"
  }

  provisioner "remote-exec" {
    on_failure = "continue"
    inline = [
      "cmd /c echo Hello from Terraform"
    ]
  }Code language: PHP (php)

provider "azurerm" {
  features {}
}

resource "azurerm_virtual_machine" "example" {
  # ... other VM configuration ...

  connection {
    type        = "winrm"
    user        = "Administrator"
    password    = "your_password"
    host        = self.public_ip_address
    https       = false
    port        = 5985
    timeout     = "10m"
  }
}

resource "null_resource" "example" {
  triggers = {
    instance_id = azurerm_virtual_machine.example.id
  }

  provisioner "local-exec" {
    command = "echo Remote execution completed"
  }

  provisioner "remote-exec" {
    on_failure = "continue"
    inline = [
      "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command \"Write-Output 'Hello from Terraform'\"",
    ]
  }

  depends_on = [azurerm_virtual_machine.example]
}
Code language: PHP (php)

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.