Saturday, 29 May 2021

Create Instances in OCI using Terraform


Create the below environment file before running the terraform commands


env.sh
compute.tf
provider.tf
variable.tf

 

$ cat provider.tf

provider "oci" {
  tenancy_ocid = var.tenancy_ocid
  region = var.region
  disable_auto_retries = true

}

$cat compute.tf

resource "oci_core_instance" "test_instance" {

          availability_domain = var.instance_availability_domain

          compartment_id = var.compartment_id

          shape = var.instance_shape

          agent_config {

                 is_management_disabled = var.instance_agent_config_is_management_disabled

                 is_monitoring_disabled = var.instance_agent_config_is_monitoring_disabled

                   }

                availability_config {

                   recovery_action = var.instance_availability_config_recovery_action

                                    }

                metadata = {

                   ssh_authorized_keys = file("/home/a_prashant/.ssh/id_rsa.pub")

                           }

                display_name = var.instance_display_name

                source_details {

                      #Required

                        source_id = var.instance_image_id

                        source_type = "image"

                        }

 

                create_vnic_details {

                        assign_public_ip = true

                        subnet_id = "               "

                        }

                }

 

 

$ cat env.sh

export TF_VAR_tenancy_ocid=                                                

export TF_VAR_region=                        

export TF_VAR_instance_availability_domain=                        

export TF_VAR_compartment_id=                        

export TF_VAR_compartment_description=                        

export TF_VAR_compartment_name=                        

export TF_VAR_enable_delete=                        

export TF_VAR_vcn_cidr_block=                        

export TF_VAR_vcn_display_name=                        

export TF_VAR_vcn_dns_label=                        

export TF_VAR_instance_agent_config_is_monitoring_disabled=                        

export TF_VAR_instance_availability_config_recovery_action=                        

export TF_VAR_instance_image_id=                        

export TF_VAR_instance_display_name="TF_TERRAFORM_NEW"

 

 

$ cat variable.tf

variable "tenancy_ocid" {}

variable "user_ocid" {}

variable "fingerprint" {}

variable "private_key_path" {}

variable "private_key_password" {}

variable "region" {}

variable "compartment_id" {}

variable "instance_display_name" {}

#For VCN

variable "vcn_cidr_block" {}

variable "vcn_display_name" {}

variable "vcn_dns_label" {}

#For Instance

variable "instance_availability_domain" {}

variable "instance_shape" {}

variable "instance_agent_config_is_management_disabled" {}

variable "instance_agent_config_is_monitoring_disabled" {}

variable "instance_availability_config_recovery_action" {}

variable "instance_image_id" {}

  

$ terraform init

 

Initializing the backend...

 

Initializing provider plugins...

- Using previously-installed hashicorp/oci v4.13.0

 

The following providers do not have any version constraints in configuration,

so the latest version was installed.

 

To prevent automatic upgrades to new major versions that may contain breaking

changes, we recommend adding version constraints in a required_providers block

in your configuration, with the constraint strings suggested below.

 

* hashicorp/oci: version = "~> 4.13.0"

 

Terraform has been successfully initialized!

 

You may now begin working with Terraform. Try running "terraform plan" to see

any changes that are required for your infrastructure. All Terraform commands

should now work.

 

If you ever set or change modules or backend configuration for Terraform,

rerun this command to reinitialize your working directory. If you forget, other

commands will detect it and remind you to do so if necessary.

 

$ terraform plan

var.fingerprint

  Enter a value:                        

 

var.private_key_password

  Enter a value:                        

 

var.private_key_path

  Enter a value:                                

 

var.user_ocid

  Enter a value:                         

 

Refreshing Terraform state in-memory prior to plan...

The refreshed state will be used to calculate this plan, but will not be

persisted to local or remote state storage.

 ------------------------------------------------------------------------

 

An execution plan has been generated and is shown below.

Resource actions are indicated with the following symbols:

  + create

 

Terraform will perform the following actions:

 

  # oci_core_instance.test_instance will be created

  + resource "oci_core_instance" "test_instance" {

      + availability_domain                 = "RJkC:AP-MUMBAI-1-AD-1"

      + boot_volume_id                      = (known after apply)

      + compartment_id                      = "                        "

      + dedicated_vm_host_id                = (known after apply)

      + defined_tags                        = (known after apply)

      + display_name                        = "TF_TERRAFORM_NEW"

      + fault_domain                        = (known after apply)

      + freeform_tags                       = (known after apply)

      + hostname_label                      = (known after apply)

      + id                                  = (known after apply)

      + image                               = (known after apply)

      + ipxe_script                         = (known after apply)

      + is_pv_encryption_in_transit_enabled = (known after apply)

      + launch_mode                         = (known after apply)

      + metadata                            = {

          + "ssh_authorized_keys" = <<~EOT

                ssh-rsa                         

            EOT

        }

      + private_ip                          = (known after apply)

      + public_ip                           = (known after apply)

      + region                              = (known after apply)

      + shape                               = "VM.Standard.E2.1.Micro"

      + state                               = (known after apply)

      + subnet_id                           = (known after apply)

      + system_tags                         = (known after apply)

      + time_created                        = (known after apply)

      + time_maintenance_reboot_due         = (known after apply)

 

      + agent_config {

          + are_all_plugins_disabled = (known after apply)

          + is_management_disabled   = false

          + is_monitoring_disabled   = false

 

          + plugins_config {

              + desired_state = (known after apply)

              + name          = (known after apply)

            }

        }

 

      + availability_config {

          + recovery_action = "RESTORE_INSTANCE"

        }

 

      + create_vnic_details {

          + assign_public_ip       = "true"

          + defined_tags           = (known after apply)

          + display_name           = (known after apply)

          + freeform_tags          = (known after apply)

          + hostname_label         = (known after apply)

          + private_ip             = (known after apply)

          + skip_source_dest_check = (known after apply)

          + subnet_id              = "                        "

          + vlan_id                = (known after apply)

        }

 

      + instance_options {

          + are_legacy_imds_endpoints_disabled = (known after apply)

        }

 

      + launch_options {

          + boot_volume_type                    = (known after apply)

          + firmware                            = (known after apply)

          + is_consistent_volume_naming_enabled = (known after apply)

          + is_pv_encryption_in_transit_enabled = (known after apply)

          + network_type                        = (known after apply)

          + remote_data_volume_type             = (known after apply)

        }

 

      + shape_config {

          + gpu_description               = (known after apply)

          + gpus                          = (known after apply)

          + local_disk_description        = (known after apply)

          + local_disks                   = (known after apply)

          + local_disks_total_size_in_gbs = (known after apply)

          + max_vnic_attachments          = (known after apply)

          + memory_in_gbs                 = (known after apply)

          + networking_bandwidth_in_gbps  = (known after apply)

          + ocpus                         = (known after apply)

          + processor_description         = (known after apply)

        }

 

      + source_details {

          + boot_volume_size_in_gbs = (known after apply)

          + kms_key_id              = (known after apply)

          + source_id               = "                        "

          + source_type             = "image"

        }

    }

 

Plan: 1 to add, 0 to change, 0 to destroy.

 

------------------------------------------------------------------------

 

Note: You didn't specify an "-out" parameter to save this plan, so Terraform

can't guarantee that exactly these actions will be performed if

"terraform apply" is subsequently run.

 

$ terraform apply

var.fingerprint

  Enter a value:                                                     

 

var.private_key_password

  Enter a value:                         

 

var.private_key_path

  Enter a value:                         

 

var.user_ocid

  Enter a value:                         

 

 

An execution plan has been generated and is shown below.

Resource actions are indicated with the following symbols:

  + create

 

Terraform will perform the following actions:

 

  # oci_core_instance.test_instance will be created

  + resource "oci_core_instance" "test_instance" {

      + availability_domain                 = "RJkC:AP-MUMBAI-1-AD-1"

      + boot_volume_id                      = (known after apply)

      + compartment_id                      = "                        "

      + dedicated_vm_host_id                = (known after apply)

      + defined_tags                        = (known after apply)

      + display_name                        = "TF_TERRAFORM_NEW"

      + fault_domain                        = (known after apply)

      + freeform_tags                       = (known after apply)

      + hostname_label                      = (known after apply)

      + id                                  = (known after apply)

      + image                               = (known after apply)

      + ipxe_script                         = (known after apply)

      + is_pv_encryption_in_transit_enabled = (known after apply)

      + launch_mode                         = (known after apply)

      + metadata                            = {

          + "ssh_authorized_keys" = <<~EOT

                ssh-rsa                         

            EOT

        }

      + private_ip                          = (known after apply)

      + public_ip                           = (known after apply)

      + region                              = (known after apply)

      + shape                               = "VM.Standard.E2.1.Micro"

      + state                               = (known after apply)

      + subnet_id                           = (known after apply)

      + system_tags                         = (known after apply)

      + time_created                        = (known after apply)

      + time_maintenance_reboot_due         = (known after apply)

 

      + agent_config {

          + are_all_plugins_disabled = (known after apply)

          + is_management_disabled   = false

          + is_monitoring_disabled   = false

 

          + plugins_config {

              + desired_state = (known after apply)

              + name          = (known after apply)

            }

        }

 

      + availability_config {

          + recovery_action = "RESTORE_INSTANCE"

        }

 

      + create_vnic_details {

          + assign_public_ip       = "true"

          + defined_tags           = (known after apply)

          + display_name           = (known after apply)

          + freeform_tags          = (known after apply)

          + hostname_label         = (known after apply)

          + private_ip             = (known after apply)

          + skip_source_dest_check = (known after apply)

          + subnet_id              = "                        "

          + vlan_id                = (known after apply)

        }

 

      + instance_options {

          + are_legacy_imds_endpoints_disabled = (known after apply)

        }

 

      + launch_options {

          + boot_volume_type                    = (known after apply)

          + firmware                            = (known after apply)

          + is_consistent_volume_naming_enabled = (known after apply)

          + is_pv_encryption_in_transit_enabled = (known after apply)

          + network_type                        = (known after apply)

          + remote_data_volume_type             = (known after apply)

        }

 

      + shape_config {

          + gpu_description               = (known after apply)

          + gpus                          = (known after apply)

          + local_disk_description        = (known after apply)

          + local_disks                   = (known after apply)

          + local_disks_total_size_in_gbs = (known after apply)

          + max_vnic_attachments          = (known after apply)

          + memory_in_gbs                 = (known after apply)

          + networking_bandwidth_in_gbps  = (known after apply)

          + ocpus                         = (known after apply)

          + processor_description         = (known after apply)

        }

 

      + source_details {

          + boot_volume_size_in_gbs = (known after apply)

          + kms_key_id              = (known after apply)

          + source_id               = "                        "

          + source_type             = "image"

        }

    }

 

Plan: 1 to add, 0 to change, 0 to destroy.

 

Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.

 

  Enter a value: yes

 

oci_core_instance.test_instance: Creating...

oci_core_instance.test_instance: Still creating... [10s elapsed]

oci_core_instance.test_instance: Still creating... [20s elapsed]

oci_core_instance.test_instance: Still creating... [30s elapsed]

oci_core_instance.test_instance: Still creating... [40s elapsed]

oci_core_instance.test_instance: Still creating... [50s elapsed]

oci_core_instance.test_instance: Still creating... [1m0s elapsed]

oci_core_instance.test_instance: Still creating... [1m10s elapsed]

oci_core_instance.test_instance: Creation complete after 1m18s [id=                        ]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

1 comment: