Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get remote-exec provisioner to apply after disk attachments?

Tags:

terraform

I have a script that I need to run after my instance has been provisioned and the volumes have been attached:

resource "aws_instance" "controller" {
  ...

  provisioner "remote-exec" {
    connection {
      type     = "ssh"
      user     = "centos"
    }
    inline = [
      "download and run script to verify environment"
    ]
  }
}

resource "aws_ebs_volume" "controller-ebs-sdb" {
  ...
}

resource "aws_volume_attachment" "controller-volume-attachment-sdb" {
  device_name = "/dev/sdb"
  volume_id   = "${aws_ebs_volume.controller-ebs-sdb.id}"
  instance_id = "${aws_instance.controller.id}"
}

Currently the script is failing the environment because when it runs the volume has not been attached.

Is it possible to only run the remote-exec script after the volumes have been attached?

like image 709
Chris Snow Avatar asked Sep 15 '25 18:09

Chris Snow


1 Answers

You can run a provisioner on any resource (consider the null_resource pattern for an extreme version of this) so the best thing here is to run it on the aws_volume_attachment resource:

# ...

resource "aws_volume_attachment" "controller-volume-attachment-sdb" {
  device_name = "/dev/sdb"
  volume_id   = "${aws_ebs_volume.controller-ebs-sdb.id}"
  instance_id = "${aws_instance.controller.id}"

  provisioner "remote-exec" {
    connection {
      host     = "${aws_instance.controller.public_ip}"
      type     = "ssh"
      user     = "centos"
    }
    inline = [
      "download and run script to verify environment"
    ]
  }
}
like image 73
ydaetskcoR Avatar answered Sep 18 '25 10:09

ydaetskcoR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!