Categories: Uncategorized

Terraform and OCI DHCP Options

Hello blog! Sorry I’ve been lacking with my posts but let’s kick it off again and with a bang!

I’ve had few cases previously where I’m using Terraform modules and the blocks need to support one to many different values. This means that sometimes the values exist and sometimes they don’t.

For this I thought creating OCI DHCP Options for your subnet would a great simple example. Check out this video to see it in use live!

Summarizing above, one way to create dynamic modules is to use blocks and define the values null from a lookup in a map when you use Terraform 0.12. Like this:

variable "vcn_id" {}
variable "compartment_ocid" {}
variable "dhcp_options" {}
variable "dhcp_display_name" {}


resource "oci_core_dhcp_options" "CreateDHCPOptions" {
   
    compartment_id = var.compartment_ocid
    display_name = var.dhcp_display_name
    vcn_id = var.vcn_id 
    
    dynamic "options" {
        iterator = dhcpoptions
        for_each = [for y in var.dhcp_options : {
        type = y.dhcp_type 
        server_type = lookup(y, "dhcp_server_type", null) 
        custom_dns_servers = lookup(y, "custom_dns_servers", null) 
        search_domain_names = lookup(y, "search_domain_names", null)
   }]
      content {
          type = dhcpoptions.value.type
          server_type = dhcpoptions.value.server_type
          custom_dns_servers = dhcpoptions.value.custom_dns_servers
          search_domain_names = dhcpoptions.value.search_domain_names
    
      }
    }
}

As I talk in the video sometimes you don’t pass all values in the options block. If we look example from Terraform documentation to create DHCP Options resource it looks like this:

resource "oci_core_dhcp_options" "test_dhcp_options" {
    #Required
    compartment_id = "${var.compartment_id}"
    options {
        type = "DomainNameServer"
        server_type = "CustomDnsServer"
        custom_dns_servers = [ "192.168.0.2", "192.168.0.11", "192.168.0.19" ]
    }

    options {
        type = "SearchDomain"
        search_domain_names = [ "test.com" ]
    }

    vcn_id = "${oci_core_vcn.test_vcn.id}"

    #Optional
    defined_tags = {"Operations.CostCenter"= "42"}
    display_name = "${var.dhcp_options_display_name}"
    freeform_tags = {"Department"= "Finance"}
}

You see there are two options blocks now, one for DNS and other one for SearchDomain. Iterating over map with for_each is a handy way of doing it in my opinion. If we look variable what I have for dhcp_options you can see how the options are build up.

variable "vcn_dns_label" { default = "demo" }

variable "dhcp_display_name" { default = "MyCustomDHCP" }
variable "custom_dhcp_options" { default = {
    option1 =  {
     dhcp_type = "DomainNameServer"
     dhcp_server_type = "CustomDnsServer"
     custom_dns_servers = [ "10.0.0.4", "10.0.0.5"]
      }, 
option2 = {
dhcp_type = "SearchDomain"
search_domain_names = [ "thatfinnishguy.blog" ]
}}}

You can label them as you like (I use option1, option2 etc in this case) and then define the necessary variables inside the map. When you use lookup in the module it will not apply them to that options block where they don’t exist.

I recommend you to try it, once you get hold on modules it’s very easy to use same for all you do with Terraform!

All code I used in my example is available from my Git repo.

Simo

View Comments

  • Your demonstrations are great! Thank you for sharing. I have a question regarding the custom DNS servers. Do you have to have the DNS server already created before creating the module or when you execute the module, it creates it automatically for you and works as a dhcp server? I'm really not sure how the custom DNS servers work on OCI? Any information is appreciated

Recent Posts

Connecting to Autonomous Database Running on Google Cloud

Last time I showed how to provision Autonomous Database Serverless (ADB-S) on Google Cloud. This…

1 month ago

Can you believe it? Provisioning Autonomous Database in GCP!

I bet few years back folks didn't expect that by 2024 we would be able…

2 months ago

IP Address Insights with CLI

My previous post on IP Address Insights I mentioned it wasn't yet available with CLI…

6 months ago

Thoughts on Oracle Database@Azure

This will NOT be a technical walkthrough on Oracle Database@Azure but rather my opinions and…

6 months ago

OCI Vulnerability Scanning Setup

Many times when you work for someone, they already have their own vulnerability scanning throughout…

6 months ago

OCI IP Address Insights

Recently OCI announced small but VERY useful service, IP Address Insights. Why this matters? I've…

6 months ago