Export OCI Resource with Terraform
The Terraform OCI provider that is used to manage resources on the Oracle Cloud Infrastructure, has a hidden feature that can export resources and generate configuration files.
This is very handy to reverse engineer a compartment or tenancy, or if you simply want to copy or recreate a resource without having to do too much typing.
The executable needed can be downloaded from the Hashicorp releases. Choose the latest release and place it into an executable directory. I typically create a symbolic link to it for easier reference.
To export the resources of a compartment, create an empty directory to hold the generated configuration files (eg. /tf/test/).
Enter the command to export all the resources for a compartment:
$ terraform-provider-oci -command=export -compartment_name=test -output_path=/tf/test
Note that the output_path must be an absolute path.
After the export is completed, you will see a summary of what was exported…
Found 12 'availability_domain' resources. Generated under '/tf/test/availability_domain.tf'
Found 203 'core' resources. Generated under '/tf/test/core.tf'
Found 0 'database' resources. Generated under '/tf/test/database.tf'
Found 0 'load_balancer' resources. Generated under '/tf/test/load_balancer.tf'
Found 6 'object_storage' resources. Generated under '/tf/test/object_storage.tf'
Found 9 'tagging' resources. Generated under '/tf/test/tagging.tf'
…and the corresponding configuration files:
$ ls
availability_domain.tf
core.tf
database.tf
load_balancer.tf
object_storage.tf
provider.tf
tagging.tf
vars.tf
I found this hand for a use case of where I needed to clone resources. I simply copied and pasted the resource code, made some changes and applied it.
I hope you find this helpful.