Note:

Deploy Infrastructure using Terraform

Introduction

In this tutorial we will use pre-configured terraform scripts to deploy VCN, Subnet and Compute Instance. We will then delete all these infrastructure resources.

Estimated time: 30 minutes

Key points

Prerequisites

Task 1: Access OCI Cloud Shell

  1. From the OCI Console, click Cloud Shell to launch a cloud shell session.

    Note: Ensure that the correct Compartment is selected under COMPARTMENT list.

    Cloud Shell icon on OCI Console

  2. Click the Profile icon, then click your tenancy. Click Copy and record your tenancy OCID for later use.

    Tenancy under Profile icon

  3. Click your region, then click Manage Regions. Copy your region identifier and record it for later use.

    Manage Regions

  4. Click Identity & Security in the navigation menu, then click Compartments.

    Compartments under Identity & Security

    Click Copy and record your compartment OCID for later use.

    Copy button

Task 2: Configure Terraform

  1. From the cloud shell use this command to download the Terraform sample code:

    curl https://objectstorage.us-ashburn-1.oraclecloud.com/p/W7d4rKt71E8pOkTyB1ca5lbuerxDcyL0ZjuIMIRQ15-i-JW7KJOC6TMKSnD-_bZO/n/ociobenablement/b/lab/o/terraform_demo.zip -o terraform-demo.zip
    
  2. Unzip the sample file using this command:

    unzip terraform-demo.zip
    
  3. Open the terraform.tfvars file using these commands:

    cd terraform_demo
    
    vi terraform.tfvars
    
  4. Enter i to update the - tenancy_ocid, region, compartment_ocid variables with the values you recorded in the Access the OCI Shell task.

    Edit terraform variable file

  5. Enter :wq! to save the file and exit.

  6. Initialize terraform using this command:

    terraform init
    

    A successful initialization message is returned:

    Successful initialization message

  7. View the deployment plan of what will be configured in OCI using this command:

    terraform plan
    
  8. Apply the plan to create the infrastructure using this command:

    terraform apply
    

    Note: Enter ‘yes’ when prompted.

    The script takes some time to execute. You can switch to the OCI console and observe the creation of the VCN, Compute instance. You will see below messege when apply is completed.

    Apply the plan

  9. When done, destroy the infrastructure that you created using this command:

    terraform destroy
    

    Note: Enter ‘yes’ when prompted.

    You can switch to the OCI console and observe deletion of VCN, Compute instance. You will see below messege when destroy is completed.

    Deletion of all resources

Acknowledgements

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.