hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.6k stars 9.54k forks source link

Wait for ssh connectivity in local-exec #2137

Closed pastafari closed 9 years ago

pastafari commented 9 years ago

Hi,

We're using chef-solo as our provisioning tool, and I wanted to know the best way to integrate it with a terraform provision block. Here's what I need to do:

  1. Spin up a digital ocean droplet
  2. Bootstrap it (lets say I want to call bootstrap.sh)
  3. Provision it (lets say I want to call provision.sh)

I have local scripts that do 2 and 3 given a remote IP, key and run list. I added both scripts to a local-exec block like so:

provisioner 'local-exec' {
  command = "provision.sh && bootstrap.sh" 
}

Now, what happens is that the digital ocean droplet comes up, but OpenSSH takes some time to start, so when the provisioner gets triggered, my bootstrap script is unable to connect to the droplet.

One way around this would be to add a sleep-until-i-can-connect loop in the bootstrap script. Another way would be to delegate that to terraform, like remote-exec does (correct me if I'm wrong!) via its connection block.

What would you recommend?

stephenchu commented 9 years ago

If you are not inside of a VPC (or bastion host-like environment, i.e. no ssh hopping), you could use a remote-exec (which has retry logic on connection refused) as the first provisioner that does nothing to do it.

provisioner "remote-exec" {
  inline = ["# Connected!"]
}
pastafari commented 9 years ago

@stephenchu that is a great idea. Thanks! :+1:

Essentially, I'm doing a remote-exec via a local script.

I was wondering if this is a common enough use case to warrant it being first class in local-exec.

pastafari commented 9 years ago

Closing this as its not really an issue. Thanks again @stephenchu for the idea.

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.