56_Terraform_Instance Creation - Nirvan-Pandey/OCI_DOC GitHub Wiki
56_1: Introduction:
This Terraform script clones an existing OCI instance by referencing its configuration, such as shape, image, and network settings.
56_2: Terraform Configuration
56_2_1: Create Terraform Directory
Open the code editor in OCI and create a folder named 'terraform'.
56_2_2: Create Terraform File
Create a new file named Import.tf in this folder. .tf is the extension of terrraform file.
56_2_3: Import the Instance into Terraform State
Put the below command in this Import.tf file and will replace with OCI ID of Existing Instance.
import {
id = "ocid.virtual_circuit.oc1.phx.example"
to = oci_core_virtual_circuit.test_virtual_circuit
}
Explanation: id = "ocid1.instance.oc1.phx.example" → This is the OCID of the existing instance you are copying.
to = oci_core_instance.new_instance → This is the Terraform resource that will manage the copied instance.
Copy the OCI ID of the existing Instance and replace it in this command in the value for id= .
Put the oci_core_instance.test_instance in the value for to= .
import {
id = "ocid1.instance.oc1.iad.anuwcljtl6qj5oyczl2zxp4igbiuexaciprf3bhyn5eniybjzvrkw747lq7a"
to = oci_core_instance.test_instance
}
56_2_4: Open Integrated Terminal
Now, Right click on import.tf file and select open in integrated terminal.
56_2_5. Initialize Terraform.
terraform init
Terraform is initialized now.
56_2_6. Generate Terraform Configuration
terraform plan --generate-config-out=generated.tf
This will create a generated.tf file with the current configuration of the imported instance.
A new file named generated.tf is created with execution plan.
56_2_7: Comment Out Import File
By placing a # before the import.tf command in the code editor, it is converted into a comment. This prevents the command from being executed, effectively hiding it from the execution process.
56_2_8: Create New Terraform File
Now we will create a New File in the Terraform for the new instance and name it.
Create a new Terraform file for the new instance and name it Application_Server.tf.
56_2_9: Copy Configuration to New File.
Copy all from generated.tf
Paste into Application_Server.tf
Place # into generated.tf to make it inactive and will be treated as comment. In another way, multi-line comments are enclosed within /* ... */ to make it inactive and to be treated as comment.
Now, generated.tf is comment only and will not be executed.
56_2_10: Run Terraform Plan
Run the Terraform plan command to preview the changes
terraform plan
56_2_11: Apply Terraform Configuration
Review all details and apply the Terraform plan
terraform apply
Now, enter Yes to confirm and apply the plan.
56_2_12: Troubleshoot Errors
Error Correction
Error 1: 400-InvalidParameter, Invalid Nvmes: 1
Solution: Comment out nvmes = 1 in shape_config as VM.Standard.E3.Flex does not support NVMe storage.
Error 2: 400-InvalidParameter, Hostname Application_Server is provided when AssignPrivateDnsRecord is false.
Solution: Remove or comment out hostname_label since private DNS is disabled.
Error 3: 400-InvalidParameter, Requested volume size 47GB is not in the allowed range.
Solution: Update boot_volume_size_in_gbs to at least 50GB
56_2_13: Instance Creation
Now Creating.
Instance is created now.
56_2_14: Terminate the Instance.
terraform destroy
Instance has been destroyed.
56_3: Conclusion
This guide provides a step-by-step approach to cloning an OCI instance using Terraform. It covers Terraform initialization, importing an instance, troubleshooting errors, and deploying an instance. Following these steps ensures a smooth replication of an existing instance with the necessary configurations.