Create Scripts and State Files with Resource Discovery
Generate Terraform files for existing resources in your compartment.
Use the OCI resource discovery feature to:
Create state files for existing resources in the Console, and then add those resources to a Terraform setup.
Duplicate your existing infrastructure in a new tenancy or region.
Detect state drift for updated resources.
In this tutorial, you create scripts and state files for resources in your account through the resource discovery feature. Then you use Terraform to manage the resources.
Key tasks include how to:
Create a resource through the Console.
Create scripts and a state file for the resource through the resource discovery feature.
Update the resource with Terraform.
Confirm that the resource has been updated in the Console.
Make a note of the OCI Terraform provider command:
Cloud Shell: terraform-provider-oci_<version> from /usr/bin
Compute instance or a local environment: Use the <shorter-alias> that you created in the previous tutorial to use instead of the command, terraform-provider-oci_<version>.
Example: tf-oci
Terraform v1.1.3+:
If you're using Cloud Shell, you don't need to install Terraform. Terraform is already installed.
If you're using a compute instance or a local environment, then follow the steps at Install Terraform.
1. Create a Resource 🔗
Create a bucket in your tenancy through the Console. This bucket doesn't have a Terraform script. Later, use resource discovery to create a Terraform script and a state file for the bucket.
If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:
Copy
allow group <a-group-your-username-belongs-to> to manage compartments in tenancy
With this privilege, you can create a compartment for all the resources in your
tutorial.
If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:
Copy
allow group <a-group-your-username-belongs-to> to manage all-resources in compartment <your-compartment-name>
With this privilege, you can manage all resources in your compartment, giving you administrative rights in that compartment.
Replace <your-compartment-name>, with the name of your compartment.
Sample output:
...
INFO <date> [INFO] ===> Generating resource 'oci_objectstorage_namespace.export_namespace'
INFO <date> [INFO] ===> Generating resource 'oci_objectstorage_bucket.export_<your-bucket-name>'
INFO <date> [INFO] Optional TF attribute 'kms_key_id' not found in source
INFO <date> Found 2 'object_storage' resources. Generated under '/<home-directory>/resource-discovery/object_storage.tf'.
INFO <date> === COMPLETED ===
Note
You might get the following message:
<date> ERROR IN GET: Service error: LifecyclePolicy NotFound. The bucket '<your-bucket-name>' does not define a lifecycle policy.. http status code: 404. Opc request id: iad-1:xxx
Lacking a lifecycle policy is OK for this tutorial. To plan for the service to automatically archive or delete this bucket, see Using Object Lifecycle Management.
Review the generated Terraform scripts.
Copy
cd resource-discovery
Copy
ls
object_storage.tf
provider.tf
vars.tf
View the object_storage.tf script.
Copy
cat object_storage.tf
## This configuration was generated by terraform-provider-oci
data oci_objectstorage_namespace export_namespace {
compartment_id = var.compartment_ocid
}
resource oci_objectstorage_bucket export_<your-bucket-name> {
access_type = "NoPublicAccess"
auto_tiering = "Disabled"
compartment_id = var.compartment_ocid
defined_tags = {
}
freeform_tags = {
}
#kms_key_id = <<Optional value not found in discovery>>
metadata = {
}
name = "<your-bucket-name>"
namespace = data.oci_objectstorage_namespace.export_namespace.namespace
object_events_enabled = "false"
storage_tier = "Standard"
versioning = "Disabled"
}
You have successfully discovered Object Storage resources in your compartment.
[ERROR] output_path does not exist: stat /$HOME/resource-discovery: no such file or directory
The resource discovery command doesn't create a directory for the discovered resources. Create a directory and specify the path in your command.
[ERROR] no output directory specified
The parser stops at the parameter that lacks a dash. Ensure that you add a dash to the beginning of each parameter. For example, if you use services instead of -services, the parser doesn't reach the output directory.
To track your resources, Terraform stores the state of your resources in a state file. Then, every time you update a resource with Terraform, Terraform updates the state file with that change.
Run the resource discovery command with the
-generate_state flag:
...
Terraform has been successfully initialized!
...
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
INFO <date> [INFO] ===> Generating resource 'oci_objectstorage_namespace.export_namespace'
INFO <date> [INFO] ===> Generating resource 'oci_objectstorage_bucket.export_<your-bucket-name>'
INFO <date> [INFO] Optional TF attribute 'kms_key_id' not found in source
INFO <date> Found 2 'object_storage' resources. Generated under '/<home-directory>/resource-discovery/object_storage.tf'.
INFO <date> === COMPLETED ===
Note
Troubleshooting:
Error: Failed to query available provider packages:
If you are on a VPN, check your proxy settings.
View the contents of the resource-discovery
directory.
Copy
ls
Sample output:
object_storage.tf
provider.tf
terraform.tfstate
vars.tf
Note
The resource discovery command overwrites the <resource>.tf files every time you run it. If you don't specify a service, it creates a <resource>.tf file for every resource in the specified compartment.
Update the name of your bucket in the object_storage.tf file and then run your Terraform scripts. Your account authenticates the scripts and then Terraform updates the bucket name. Confirm the new bucket name through the Console.
When you create a state file, resource discovery initializes a working directory that includes Terraform configuration files. Make a habit to run this command every time you update your Terraform scripts.
Check the contents of the resource-discovery directory.
Copy
ls -a
You have a folder called .terraform that includes the plugins for the oci provider.
Confirm that you have Terraform installed.
Copy
terraform -v
Run the init command:
Copy
terraform init
Example output:
Initializing the backend...
Initializing provider plugins...
- Using previously-installed hashicorp/oci vx.x.x
...
Terraform has been successfully initialized!
An execution plan is the list of changes that Terraform plans to apply to your account.
Create an execution plan:
Copy
terraform plan
Review the execution plan.
With the command,terraform plan you check whether the changes shown in the execution plan match your expectations, without changing to the real resources. Example output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# oci_objectstorage_bucket.export_<your-bucket-name>must be replaced
-/+ resource "oci_objectstorage_bucket" "export_<your-bucket-name>" {
...
~ name = "<your-bucket-name>" -> "<your-bucket-name>-2" # forces replacement
namespace = "<your-tenancy>"
...
Plan: 1 to add, 0 to change, 0 to destroy.