I am using Terraform 0.14 and AWS. I am trying to write a .tf file that will invoke a remote-exec command via Terraform's provisioner.
This code does not work:
resource "aws_instance" "example" {
ami = "ami-1234"
instance_type = "t2.large"
key_name = "foobar"
user_data = local.cloud_config_config
provisioner "remote-exec" {
inline = ["sudo yum -y install coolprogram"]
}
}
The above will create the EC-2 instance. But it won't run the inline command. After I run "terraform apply" with the above content in my .tf file, I see "Error: host for provisioner cannot be empty"
I tried this .tf file variation, but I get an error about the variable:
resource "aws_instance" "example" {
ami = "ami-1234"
instance_type = "t2.large"
key_name = "foobar"
user_data = local.cloud_config_config
provisioner "remote-exec" {
inline = ["sudo yum -y install coolprogram"]
}
connection {
host = "${var.host_name}"
}
}
The hostname will be created dynamically. Terraform's documentation does not mention a hostname. I have tried different variables.tf files.
But I always get something like this:
aws_instance.example (remote-exec): Connecting to remote host via SSH... aws_instance.example (remote-exec): Host: example aws_instance.example (remote-exec): User: root aws_instance.example (remote-exec): Password: false aws_instance.example (remote-exec):
Private key: false aws_instance.example (remote-exec): Certificate: false aws_instance.example (remote-exec): SSH Agent: false aws_instance.example (remote-exec): Checking Host Key: false aws_instance.example (remote-exec): Target Platform: unix
How do I use the remote-exec provisioner on an EC-2 server?