Last active
September 27, 2022 20:52
-
-
Save scross01/21e5e934c5aca5602be712fac5f9eacb to your computer and use it in GitHub Desktop.
Oracle Cloud Infrastructure Instance with Terraform remote-exec Provisioner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "oci_core_instance" "example" { | |
compartment_id = "${var.compartment_ocid}" | |
availability_domain = "${var.availability_domain}" | |
subnet_id = "${var.subnet_ocid}" | |
display_name = "example" | |
image = "${lookup(data.oci_core_images.image-list.images[0], "id")}" | |
shape = "VM.Standard1.1" | |
metadata = { | |
ssh_authorized_keys = "${file(var.ssh_public_key_file)}" | |
} | |
connection { | |
type = "ssh" | |
host = "${self.public_ip}" | |
user = "opc" | |
private_key = "${file(var.ssh_private_key_file)}" | |
} | |
provisioner "remote-exec" { | |
inline = [ | |
"echo 'This instance was provisioned by Terraform.' | sudo tee /etc/motd", | |
] | |
} | |
} |
@kaustubh77 I set my host this way:
provisioner "remote-exec" {
connection {
agent = false
timeout = "30m"
host = oci_core_instance.test_instance[count.index % var.num_instances].public_ip
user = "opc"
private_key = var.ssh_private_key
}
inline = [
"hello example"
]
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @scross01 thanks for this code snippet. I am also doing a similar thing. I am provisioning a db_system and trying to copy and execute a bash script in the instance on creation. Here is my code
The db_system gets created correctly but the remote exec fails with this error - "Error: host for provisioner cannot be empty". Any idea what I maybe missing?
Thank you