Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start a remote service using Terraform provisioning?

I want my Terraform config to provision a server and start the service at the end by invoking a command and keep running it. I tried using nohup and screen using remote-exec:

nohup:

provisioner "remote-exec" {
 inline = "nohup sudo command &"
}

screen:

provisioner "remote-exec" {
 inline = "screen -d -m sudo command"
}

I check if the commands are running by logging in manually. But they do not keep a process running. These commands do work if I try them manually and invoking them with ssh also works.

How can I use Terraform provisioning to start a command and keep it running while returning control flow?

like image 945
snorberhuis Avatar asked Mar 24 '16 18:03

snorberhuis


People also ask

How do you run a Terraform remote?

You can initiate Terraform Cloud runs through the manual Start new run action in the workspace actions menu, VCS webhooks, the standard terraform apply command (with the CLI integration configured), and the Runs API (or any tool that uses that API).

What is Terraform provisioning?

Terraform Provisioners are used for executing scripts or shell commands on a local or remote machine as part of resource creation/deletion. They are similar to “EC2 instance user data” scripts that only run once on the creation and if it fails terraform marks it tainted.

What is local-exec and remote-exec in Terraform?

The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource. See the remote-exec provisioner to run commands on the resource.


1 Answers

Try adding a sleep after your nohup. Worked for me. I suspect backgrounding your last remote-exec lets Terraform get away with shutting down the connection before the child process has a chance to start up, despite the nohup.

provisioner "remote-exec" {
    inline = [
        "nohup sudo command &",
        "sleep 1"
    ]
}
like image 148
talarczykco Avatar answered Sep 30 '22 17:09

talarczykco