49. Terraform: How to Create an Instance - Ayushi-srivastav/OCI GitHub Wiki

Steps to create an Instance using terraform

Step 1: Initiate a Resource Plan

Open the code editor in OCI and create a file within a folder named 'terraform'.

image

image

and then create a file named import.tf

image

Write below command in import file.

import { id = "ocid1.instance.oc1.iad.anuwcljtgnxa3myc3d6ixwoyfzihsxkzwf6gghvblcwfb3rvggwyzxsk2n4a" to = oci_core_instance.testinstance }

image

Now, Right click on import.tf file and select open in integrated terminal

image

Run

terraform init

command to initialize Terraform configuration file.

image

As we can see Terraform is initialized now

image

Now, Run command

terraform plan --generate-config-out=generated.tf

to creates an execution plan and it saves the resulting configuration of the plan into a new file called generated.tf.

image

As we can see A new file named generated.tf is created with execution plan

image

Now we place # before the import.tf command in the code editor,

it becomes a comment. This effectively hides the command from being executed.

image

Create a file with a name that matches the instance you want to execute. Here I am created file named testinstance.tf

image

Here testinstance.tf file is created

and now we will copy paste the execution plan in testinstance.tf file from generated.tf file

image

and after copy paste the execution plan, we place # before every line in the generated.tf file it becomes a comment. This effectively hides the command from being executed.

image

Now Run

terraform plan 

command to provides a clear and detailed execution plan and ensures that you have a clear understanding of the changes before they are made, reducing the risk of errors and unintended consequences.

image

Step 2 : Create an Instance

Once we review all the details and make any necessary changes, we proceed by running the command

terraform apply

image

Now, enter Yes to confirm and apply the plan.

image

**Occasionally, ** we encounter errors during this process. Let's address and resolve these errors.

  • Error 1 : shapeconfig.nvmes

image

Solution : Place # before nvmes in the testinstance.tf file

image

  • Error 2 : Hostname

Solution : Place # before hostname in the testinstance.tf file

image

  • Error 3 : Boot volume size

Solution : Increase the size of boot volume from 47 gb to 50 gb in testinstance.tf file

image

  • Error 4 : PV encryption in transit enabled

Solution : Place # before PV encryption in transit enabled in the testinstance.tf file

image

  • Error 5 : Private IP

Solution : Place # before Private IP in the testinstance.tf file

image

Again Run

terraform apply

command and enter "Yes"

image

Instance is created now.

image

We can see in OCI console also, Testinstance is created.

image

Step 3 : Terminate an Instance

Run

terraform destroy

command to terminate the Instance from backend using terraform

image

Enter 'Yes' to initiate

image

Instance is destroyed

image

testinstance is Terminated As we can see in console as well

image