Christoph's 2 Cents

A Backup for My Brain!

CloudDevOpsOracle Cloud InfrastructrureTerraform

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/'
Found 203 'core' resources. Generated under '/tf/test/'
Found 0 'database' resources. Generated under '/tf/test/'
Found 0 'load_balancer' resources. Generated under '/tf/test/'
Found 6 'object_storage' resources. Generated under '/tf/test/'
Found 9 'tagging' resources. Generated under '/tf/test/'

…and the corresponding configuration files:

$ ls

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.