Skip to content

Instantly share code, notes, and snippets.

@scross01
Last active September 27, 2022 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scross01/21e5e934c5aca5602be712fac5f9eacb to your computer and use it in GitHub Desktop.
Save scross01/21e5e934c5aca5602be712fac5f9eacb to your computer and use it in GitHub Desktop.
Oracle Cloud Infrastructure Instance with Terraform remote-exec Provisioner
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
Copy link

kaustubh77 commented Feb 20, 2021

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

resource "oci_database_db_system" "db_system_2" {
  availability_domain = data.oci_identity_availability_domain.ad.name
  compartment_id = "${var.compartment_id}"
  database_edition = "${var.database_edition}"
  db_home {
    database {
      admin_password = "${var.admin_password}"
      character_set = "${var.character_set}"
      db_name = "test"
      db_workload = "${var.db_workload}"
      freeform_tags = { "Project" = "${var.project_name}"}
      ncharacter_set = "${var.ncharacter_set}"
      pdb_name = "${var.pdb_name}"
    }
    db_version = "21.1.0.0"
  }
  cpu_core_count = "4"
  hostname = "test2"
  shape = "${var.shape}"
  ssh_public_keys = ["${file(var.ssh_public_keys)}"]
  subnet_id = "${var.subnet_id}"
  node_count = "${var.node_count}"
  data_storage_size_in_gb = "${var.data_storage_size_in_gb}"
  display_name = "test"

  connection {
    type        = "ssh"
    host        = "${self.private_ip}"
    user        = "opc"
    private_key = "${file(var.ssh_private_key_file)}"
  }

  provisioner "file" {
    source      = "~/Downloads/test/bash.sh"
    destination = "/home/opc/bash.sh"
  }

  provisioner "remote-exec" {
    inline = [
      "chmod 777 /home/opc/bash.sh",
      "/home/opc/bash.sh",
    ]
  }
}

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

@jasperan
Copy link

jasperan commented Dec 7, 2021

@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