HiveBrain v1.2.0
Get Started
← Back to all entries
snippetterraformMinor

How do I use the remote-exec provisioner with Terraform?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thewithprovisionerhowremoteuseexecterraform

Problem

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?

Solution

here is a complete example:

resource "aws_key_pair" "my_key" {
 key_name   = "my_key"
 public_key = file(pathexpand("~/.ssh/id_rsa.pub"))
}

resource "aws_instance" "example" {
  ami = my_ami
  instance_type = "t2.micro"
  key_name = aws_key_pair.my_key.key_name
  ebs_block_device {
    device_name = "/dev/sda1"
    volume_size = 50
  }
  provisioner "remote-exec" {
   inline = ["my_command"]
  }
  connection {
   host        = coalesce(self.public_ip, self.private_ip)
   agent       = true
   type        = "ssh"
   user        = "My_user_name"
   private_key = file(pathexpand("~/.ssh/id_rsa"))
  }
}


so I assumed that you have your public and private key located in your home directory in .ssh otherwise you need to hard code the path. and you need to make sure that your IP is allowd in the AWS security group.

Code Snippets

resource "aws_key_pair" "my_key" {
 key_name   = "my_key"
 public_key = file(pathexpand("~/.ssh/id_rsa.pub"))
}

resource "aws_instance" "example" {
  ami = my_ami
  instance_type = "t2.micro"
  key_name = aws_key_pair.my_key.key_name
  ebs_block_device {
    device_name = "/dev/sda1"
    volume_size = 50
  }
  provisioner "remote-exec" {
   inline = ["my_command"]
  }
  connection {
   host        = coalesce(self.public_ip, self.private_ip)
   agent       = true
   type        = "ssh"
   user        = "My_user_name"
   private_key = file(pathexpand("~/.ssh/id_rsa"))
  }
}

Context

StackExchange DevOps Q#13477, answer score: 2

Revisions (0)

No revisions yet.