Terraform on OCI create load balancer

Mastering Terraform on Oracle Cloud Infrastructure – Deep dive training

Table of Content

Terraform on OCI create load balancer

Download OCI Load Balancer script from Github

Once download is complete extract the zip file and the folder structure should look something as shown below in ( Visual Code my Preferred IDE ) , in this excretes we will be running lb_only

Creating a Federated User

Here we would need 4 files in the folder structure /terraform-oci-tdf-lb-master/examples/lb_only

  1. variables.tf
  2. terraform.tfvars
  3. main.tf
  4. output.tf
  5. provider.tf

variables.tf

You can get these variable values from our previous exercise

variable "tenancy_id" {}
variable "user_id" {}
variable "fingerprint" {}
variable "private_key_path" {}
variable "region" {}
variable "default_compartment_id" {}

terraform.tfvars

  
# Get this from the bottom of the OCI screen (after logging in, after Tenancy ID: heading)
tenancy_id="ocid1.tenancy.oc1..aaaaaaaaXXXsdd6ahdouq"
# Get this from OCI > Identity > Users (for your user account)
user_id="ocid1.user.oc1..aaaaaaaa7yvXXXXjsf3s4mca"

# the fingerprint can be gathered from your user account (OCI > Identity > Users > click your username > API Keys fingerprint (select it, copy it and paste it below))
fingerprint="e6:65:1d:94:XX:9c:9b:45:a1"
# this is the full path on your local system to the private key used for the API key pair
private_key_path="/Users/madhusudhanrao/tf/keys/myopensslkey.pem"

# region (us-phoenix-1, ca-toronto-1, etc)
region="us-ashburn-1"

# Compartment Name : Compartment-15Nov
default_compartment_id="ocid1.compartment.oc1..aaaaaaaahb7XXXXzx5nuauaa" 

main.tf

Create a load balancer by name test_lb of shape 100mpbs, vnc name is temp and subnet name is test

  
module "oci_lb" {
  source                  = "../../"
  
  default_compartment_id  = var.default_compartment_id
  
  lb_options            = {
    display_name        = "test_lb"
    compartment_id      = null
    shape               = "100Mbps"
    subnet_ids          = [oci_core_subnet.this.id]
    private             = true
    nsg_ids             = null
    defined_tags        = null
    freeform_tags       = null
  }
}

resource "oci_core_vcn" "this" {
  dns_label             = "temp"
  cidr_block            = "192.168.0.0/16"
  compartment_id        = var.default_compartment_id
  display_name          = "temp"
}

resource "oci_core_subnet" "this" {
  cidr_block            = "192.168.0.0/24"
  compartment_id        = var.default_compartment_id
  vcn_id                = oci_core_vcn.this.id

  display_name          = "test"
  dns_label             = "test"
  prohibit_public_ip_on_vnic = false
}

 terraform init

- statements ignored -

terraform plan

- statements ignored -

terraform apply

[email protected] lb_only % terraform apply
module.oci_lb.data.oci_identity_availability_domains.this: Refreshing state...
module.oci_lb.data.oci_load_balancer_protocols.this: Refreshing state...
module.oci_lb.data.oci_load_balancer_policies.this: Refreshing state...
module.oci_lb.data.oci_load_balancer_shapes.this: Refreshing state...

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_subnet.this will be created
  + resource "oci_core_subnet" "this" {
      + availability_domain        = (known after apply)
      + cidr_block                 = "192.168.0.0/24"
      + compartment_id             = "ocid1.compartment.oc1..aaaaaaaahb7s4w3laXXXXwbzx5nuauaa"
      + defined_tags               = (known after apply)
      + dhcp_options_id            = (known after apply)
      + display_name               = "test"
      + dns_label                  = "test"
      + freeform_tags              = (known after apply)
      + id                         = (known after apply)
      + ipv6cidr_block             = (known after apply)
      + ipv6public_cidr_block      = (known after apply)
      + ipv6virtual_router_ip      = (known after apply)
      + prohibit_public_ip_on_vnic = false
      + route_table_id             = (known after apply)
      + security_list_ids          = (known after apply)
      + state                      = (known after apply)
      + subnet_domain_name         = (known after apply)
      + time_created               = (known after apply)
      + vcn_id                     = (known after apply)
      + virtual_router_ip          = (known after apply)
      + virtual_router_mac         = (known after apply)
    }

  # oci_core_vcn.this will be created
  + resource "oci_core_vcn" "this" {
      + cidr_block               = "192.168.0.0/16"
      + cidr_blocks              = (known after apply)
      + compartment_id           = "ocid1.compartment.oc1..aaaaaaaXXXXzx5nuauaa"
      + default_dhcp_options_id  = (known after apply)
      + default_route_table_id   = (known after apply)
      + default_security_list_id = (known after apply)
      + defined_tags             = (known after apply)
      + display_name             = "temp"
      + dns_label                = "temp"
      + freeform_tags            = (known after apply)
      + id                       = (known after apply)
      + ipv6cidr_block           = (known after apply)
      + ipv6public_cidr_block    = (known after apply)
      + is_ipv6enabled           = (known after apply)
      + state                    = (known after apply)
      + time_created             = (known after apply)
      + vcn_domain_name          = (known after apply)
    }

  # module.oci_lb.oci_load_balancer_load_balancer.this[0] will be created
  + resource "oci_load_balancer_load_balancer" "this" {
      + compartment_id     = "ocid1.compartment.oc1..aaaaaaaahb7s4wXXXXx5nuauaa"
      + defined_tags       = (known after apply)
      + display_name       = "test_lb"
      + freeform_tags      = (known after apply)
      + id                 = (known after apply)
      + ip_address_details = (known after apply)
      + ip_addresses       = (known after apply)
      + ip_mode            = (known after apply)
      + is_private         = true
      + shape              = "100Mbps"
      + state              = (known after apply)
      + subnet_ids         = (known after apply)
      + system_tags        = (known after apply)
      + time_created       = (known after apply)

      + reserved_ips {
          + id = (known after apply)
        }
    }

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


Warning: Interpolation-only expressions are deprecated

  on provider.tf line 7, in provider "oci":
   7:   tenancy_ocid     = "${var.tenancy_id}"

Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.

Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

(and 4 more similar warnings elsewhere)

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_vcn.this: Creating...
oci_core_vcn.this: Creation complete after 3s [id=ocid1.vcn.oc1.iad.amaaaaaafvl7zmiaXXXXc5hakutcq]
oci_core_subnet.this: Creating...
oci_core_subnet.this: Creation complete after 6s [id=ocid1.subnet.oc1.iad.aaaaaaaanhjghdzxXXXXi6mjlcq]
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Creating...
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Still creating... [10s elapsed]
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Still creating... [20s elapsed]
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Still creating... [30s elapsed]
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Still creating... [40s elapsed]
module.oci_lb.oci_load_balancer_load_balancer.this[0]: Creation complete after 47s [id=ocid1.loadbalancer.oc1.iad.aaaaaaaadsb6XXXXcvrptvyfta]

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

Outputs:

lb = [
  {
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaahb7s4w3larbdXXXXXbzx5nuauaa"
    "defined_tags" = {
      "Oracle-Tags.CreatedBy" = "[email protected]"
      "Oracle-Tags.CreatedOn" = "2020-11-16T06:39:15.868Z"
    }
    "display_name" = "test_lb"
    "freeform_tags" = {}
    "id" = "ocid1.loadbalancer.oc1.iad.aaaaaaaadsb6k4a7m7i3fnlp5jiXXXXXrptvyfta"
    "ip_address_details" = [
      {
        "ip_address" = "192.168.0.4"
        "is_public" = false
        "reserved_ip" = []
      },
    ]
    "ip_addresses" = [
      "192.168.0.4",
    ]
    "ip_mode" = "IPV4"
    "is_private" = true
    "shape" = "100Mbps"
    "state" = "ACTIVE"
    "subnet_ids" = [
      "ocid1.subnet.oc1.iad.aaaaaaaanhjghdzx5tmbkcqq46wvzmsmpqXXXXi6mjlcq",
    ]
    "system_tags" = {}
    "time_created" = "2020-11-16 06:39:16.163 +0000 UTC"
  },
]
[email protected] lb_only % 

Reality Check

login to cloud console https://console.us-ashburn-1.oraclecloud.com/ and you should be able to see new user created.

Access Load Balancer

temp VNC is created

test_lb Load Balancer is created

Load Balancer is active

Cleanup what was created .

[email protected] mytf-vnc % terraform destroy
- ignored -