OCI Terraform Part 5 - Understanding Inline and Dynamic Blocks in Terraform

Understanding Inline and Dynamic Blocks in Terraform

In this blog, we will see more details around inline and dynamic blocks in Terraform. Managing configurations efficiently is crucial for large-scale projects. Inline blocks and dynamic blocks allow you to simplify and optimize your Terraform code, improving readability and reducing repetition.

What are Inline Blocks in Terraform?

Inline blocks are nested blocks defined within a resource or module. They are used to configure multiple related settings in a single resource without duplicating the resource itself. This makes code more concise and easier to manage.

Benefits of using inline blocks:

  • Simplifies configuration by avoiding the need to create multiple resources.
  • Improves code readability.
  • Helps manage logically related configurations (e.g., multiple security rules, tags, etc.) within a single resource.

What are Dynamic Blocks in Terraform?

Dynamic blocks allow you to dynamically generate multiple nested blocks based on external data like lists or maps. This feature helps avoid repetitive code when defining multiple similar configurations.

Benefits of using dynamic blocks:

  • Reduces redundancy by allowing loops within resource definitions.
  • Useful for scenarios where the number of blocks depends on dynamic inputs, such as multiple route rules in a route table.
  • Makes Terraform code flexible and scalable for complex infrastructure.
  • When to Use Inline and Dynamic Blocks
  • Inline blocks should be used when there are multiple related configurations (like security rules or tags) that are logically grouped under a single resource.
  • Dynamic blocks are ideal when the number or type of nested blocks depends on variables or dynamic input data (e.g., dynamically creating route rules or firewall rules based on external data).

Example 1: Inline Blocks for Multiple Security Rules in a Security List

In Oracle Cloud Infrastructure (OCI), managing security lists involves adding multiple security rules. Instead of creating multiple resources, you can use inline blocks to define security rules directly within a single oci_core_security_list resource.

resource "oci_core_security_list" "cms_security_list" {

    compartment_id = var.compartment_id
    vcn_id         = var.vcn_id
    display_name   = "cms-security-list"

    # Ingress security rules
    ingress_security_rules {
        protocol = "6"
        source   = "0.0.0.0/0"
        tcp_options {
            max = 22
            min = 22
        }
        stateless = false
    }

    ingress_security_rules {
        protocol = "6"
        source   = "0.0.0.0/0"
        tcp_options {
            max = 80
            min = 80
        }
        stateless = false
    }

    # Egress security rule
    egress_security_rules {
        protocol  = "all"
        destination = "0.0.0.0/0"
        stateless = false
    }
}

In this example, two ingress rules are defined directly inside the oci_core_security_list resource as inline blocks, making the configuration concise.


Example 2: Inline Block for Defining Multiple Subnets in a VCN

Here’s another example where you can use inline blocks to define multiple subnets within a single VCN (Virtual Cloud Network):

resource "oci_core_vcn" "cms_vcn" {

    compartment_id = var.compartment_id
    cidr_block     = "10.0.0.0/16"
    display_name   = "cms-vcn"

    # Inline block to define multiple subnets
    subnet {
        cidr_block     = "10.0.1.0/24"
        display_name   = "subnet-1"
        availability_domain = "1"
    }

    subnet {
        cidr_block     = "10.0.2.0/24"
        display_name   = "subnet-2"
        availability_domain = "2"
    }
}

In this case, two subnets are defined within the same VCN resource.

Example 3: Dynamic Block for Route Rules in a Route Table

When defining route rules for an OCI route table, it’s common to have multiple route rules with similar configurations. Using a dynamic block, you can loop over a list of route details and create route rules dynamically.

variable "route_rules" {

    type = list(object({
        cidr_block   = string
        destination  = string
        route_target  = string
    }))
}

resource "oci_core_route_table" "cms_route_table" {
    compartment_id = var.compartment_id
    vcn_id        = var.vcn_id
    display_name   = "example-route-table"

    dynamic "route_rules" {
        for_each = var.route_rules
        content {
            cidr_block         = route_rules.value.cidr_block
            destination        = route_rules.value.destination
            network_entity_id  = route_rules.value.route_target
        }
    }
}

Here, the dynamic block loops over the route_rules variable to generate multiple route rules based on a list of objects, making the route table configuration much more flexible and scalable.

Example 4: Dynamic Block for Creating Multiple Compute Instances

Let’s say you want to create multiple compute instances based on a list of instance details. Dynamic blocks can help generate these instances dynamically without hardcoding each one.

variable "instances" {

    type = list(object({
        display_name = string
        shape        = string
        image_id     = string
    }))
}

resource "oci_core_instance" "cms_instance" {
    for_each = var.instances

    compartment_id      = var.compartment_id
    availability_domain  = var.availability_domain
    display_name         = each.value.display_name
    shape                = each.value.shape

    source_details {
        source_type = "image"
        image_id    = each.value.image_id
    }
}

In this example, the dynamic block automatically generates multiple compute instances based on a list of instance configurations, avoiding the need to write individual resource blocks.

Conclusion

Both inline blocks and dynamic blocks in Terraform offer flexibility and scalability in managing infrastructure. Inline blocks are great for grouping logically related configurations, while dynamic blocks are perfect for reducing redundancy when dealing with multiple similar resources. By incorporating these features into your Terraform scripts, you can write more efficient and scalable infrastructure code, making it easier to manage even the most complex OCI environments.

No comments:

Post a Comment