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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

What is Amazon Redshift?

🟥 The Complete Guide to AWS Redshift – Cloud Data Warehousing at Scale As the digital economy expands, the amount of data generated by businesses is growing…

Read More

What is Amazon Redshift and use cases of Amazon Redshift?

What is Amazon Redshift? If you’re in the world of big data, you’ve probably heard of Amazon Redshift. But what exactly is it? Simply put, Amazon Redshift…

Read More

What is Amazon SimpleDB?

Amazon one of the big name, offering its customers computing infrastructure through Amazon Web Services since 2006. Aims to use its own infrastructure to provide the building…

Read More

AWS Tutorials: FinOps – AWS Certificate Manager (ACM) & Private CA cost optimisation strategies

Pricing FAQ For AWS Private Certificate Authority Pricing for AWS Certificate Manager You are not subject to an additional charge for SSL/TLS certificates that you manage with…

Read More

AWS Tutorials: How to Shutdown (Stop / Start) Relational Database Service?

To shutdown (stop) and start a Relational Database Service (RDS) instance in AWS, you can follow these steps: Stopping an RDS Instance Starting an RDS Instance AWS…

Read More

What is AWS App Mesh and use cases of AWS App Mesh?

What is AWS App Mesh? AWS App Mesh is a service mesh offering from Amazon Web Services that facilitates communication and management of your microservices across multiple…

Read More