You are working with Terraform on your laptop and have been tasked with spinning up multiple compute instances in Oracle Cloud Infrastructure (OCI) for a project.
In addition, you are also required to collect IP addresses of provisioned instances and write them to a file and save it in your laptop.
Which specific Terraform functionality can help accomplish this task? (Choose the best answer.)
Click on the arrows to vote for the correct answer
A. B. C. D.A.
The best Terraform functionality that can help accomplish the task of spinning up multiple compute instances in Oracle Cloud Infrastructure (OCI) and collecting their IP addresses and writing them to a file on a laptop is Terraform Local-exec.
Terraform local-exec is a built-in provisioner in Terraform that allows for executing a command locally on the machine running Terraform after a resource is created. It can be used to execute scripts, run commands, or interact with the file system on the machine running Terraform.
In this case, Terraform local-exec can be used to execute a command to collect the IP addresses of the provisioned compute instances and write them to a file on the laptop. Here is an example of how to use Terraform local-exec in a Terraform configuration:
bashresource "oci_core_instance" "example_instance" { # Instance configuration here } # Define the local-exec provisioner provisioner "local-exec" { command = "echo ${oci_core_instance.example_instance.public_ip} >> instance_ips.txt" }
In this example, the OCI compute instance resource is defined as oci_core_instance.example_instance
. After the instance is created, the local-exec
provisioner is defined to execute a command locally on the machine running Terraform. The command echo ${oci_core_instance.example_instance.public_ip} >> instance_ips.txt
collects the public IP address of the created instance and writes it to a file named instance_ips.txt
in the local directory. The >>
operator appends the IP address to the file, so if this provisioner is run multiple times, it will add each IP address to the same file.
Therefore, by using Terraform local-exec, it is possible to spin up multiple compute instances in OCI and collect their IP addresses and write them to a file on a laptop.