Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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)


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)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

AWS CLI Credentials Setup: Configure, Verify, and Secure Access

Learn how to configure AWS credentials with the AWS CLI, verify your identity, and understand where credentials are stored. This updated guide also compares profiles, environment variables, and role-based access.

Read More

Provision an AWS EC2 Instance with Chef (Legacy ChefDK Workflow)

A practical, step-by-step walkthrough for provisioning an AWS EC2 VM using the classic ChefDK and chef-provisioning approach. Includes workflow, commands, recipe examples, and validation checks for legacy environments.

Read More

EC2 Boot from EBS Explained: Architecture, Trade-offs, and an AWS CLI Workflow

Booting Amazon EC2 instances from Amazon EBS was a major shift from ephemeral root disks to persistent root volumes. This guide explains what changed, why it mattered, and how to use the model safely in modern operations.

Read More

Deploying Java EE on Amazon EC2 with EC2Deploy: Workflow, Script, and Modern Lessons

This revised guide breaks down the original EC2Deploy approach for provisioning EC2 instances, deploying Java EE apps, and running JMeter tests. It also maps the same deployment pattern to modern DevOps practices.

Read More

EC2Deploy and the Cloud Tools Maven Plugin: Architecture, Goals, and Deployment Workflow

This revised guide explains what the 2010 EC2Deploy and Cloud Tools release actually introduced, how its components fit together, and how the Maven goals map to deployment operations on EC2. It also covers what modern teams should verify before relying on this legacy toolchain.

Read More

Cloud Tools Adds Amazon EBS Support: Durable EC2 Storage for Database Workloads

This historical update covers when Cloud Tools introduced Amazon EBS support for EC2 applications, including database-backed deployments. It explains the supported storage modes, the backup/restore flow, and practical command-level equivalents.

Read More