Part 5 - Apstra Resources in Terraform

Corporate Logo

Create resources in Terraform.

Lets get started in this section by working on resources in the Terraform Apstra Provider.

You may want to refer to the following page in the step by step GUI based lab guide for reference: Lab Guide 1 - Juniper

What is a resource in Apstra?

Resources are user-definable pools of objects such as IP addresses, Autonomous System Numbers used in BGP and Virtual Network Identifiers or VNIs. We can create and manage resources used for allocation in Apstra Blueprints. Resources include the five types:

  • ASN Pools

  • IPv4 Address Pools

  • Ipv6 Address Pools

  • VNI Pools

  • Integer Pools (in Freeform )

Let’s now create some resource pools for us to use in our design.

  1. Create a file called resources.tf in your working directory.

  2. Review what resources will be created We will be creating two resources. One is an IPv4 pool and the second is an ASN pool. You can copy the below code snippet exactly.

    ## Create an IPv4 resource pool according to the instructions in the lab guide.
    resource "apstra_ipv4_pool" "lab_guide" {
      name = "apstra-pool"
      subnets = [
        { network = "4.0.0.0/24" },
        { network = "4.0.1.0/24" },
      ]
    }
    
    # Create an ASN resource pool according to the instructions in the lab guide.
    resource "apstra_asn_pool" "lab_guide" {
      name = "vpod-evpn-asn-pool"
      ranges = [
        {
          first = 100
          last  = 1000
        }
      ]
    }
  3. Run terraform apply

    Lets now apply the configuration and examine the results Its usually best practice to check what terraform will do before you run the apply by using the terraform plan command first. Lets take a look at what terraform would like to do with our resources. Here is what the terraform plan output looks like.

    Terraform can be VERY VERBOSE! Included here is the complete unmodified output for your reference, but it may not be necessary for you to read every line in detail, as you will quickly see that Terraform will show you what it plans to do in great detail at every step. We include the output here in case you need it.
    (venv2) bwester@bwester-mbp playground % terraform plan
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # apstra_asn_pool.lab_guide will be created
      + resource "apstra_asn_pool" "lab_guide" {
          + id              = (known after apply)
          + name            = "vpod-evpn-asn-pool"
          + ranges          = [
              + {
                  + first           = 100
                  + last            = 1000
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
            ]
          + status          = (known after apply)
          + total           = (known after apply)
          + used            = (known after apply)
          + used_percentage = (known after apply)
        }
    
      # apstra_ipv4_pool.lab_guide will be created
      + resource "apstra_ipv4_pool" "lab_guide" {
          + id              = (known after apply)
          + name            = "apstra-pool"
          + status          = (known after apply)
          + subnets         = [
              + {
                  + network         = "4.0.0.0/24"
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
              + {
                  + network         = "4.0.1.0/24"
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
            ]
          + total           = (known after apply)
          + used            = (known after apply)
          + used_percentage = (known after apply)
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

    You can see how Terraform tells us exactly what it plans to do and what it plans to create. Lets go forward with the terraform apply command. You will note after executing the terraform apply command that you must say yes to tell Terraform that you really intend for it to make the changes.

    (venv2) bwester@bwester-mbp playground % terraform apply
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # apstra_asn_pool.lab_guide will be created
      + resource "apstra_asn_pool" "lab_guide" {
          + id              = (known after apply)
          + name            = "vpod-evpn-asn-pool"
          + ranges          = [
              + {
                  + first           = 100
                  + last            = 1000
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
            ]
          + status          = (known after apply)
          + total           = (known after apply)
          + used            = (known after apply)
          + used_percentage = (known after apply)
        }
    
      # apstra_ipv4_pool.lab_guide will be created
      + resource "apstra_ipv4_pool" "lab_guide" {
          + id              = (known after apply)
          + name            = "apstra-pool"
          + status          = (known after apply)
          + subnets         = [
              + {
                  + network         = "4.0.0.0/24"
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
              + {
                  + network         = "4.0.1.0/24"
                  + status          = (known after apply)
                  + total           = (known after apply)
                  + used            = (known after apply)
                  + used_percentage = (known after apply)
                },
            ]
          + total           = (known after apply)
          + used            = (known after apply)
          + used_percentage = (known after apply)
        }
    
    Plan: 2 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
    
    apstra_asn_pool.lab_guide: Creating...
    apstra_ipv4_pool.lab_guide: Creating...
    apstra_ipv4_pool.lab_guide: Creation complete after 1s [id=3f6d603d-a58c-4e74-a68a-1898c4a78590]
    apstra_asn_pool.lab_guide: Creation complete after 1s [id=8c80ab22-2fdf-4d2d-acc1-e743dd5958cf]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    (venv2) bwester@bwester-mbp playground %

At this point it might be interesting to make some changes to the configured resources, either in the Apstra web UI, or in the resources.tf file. Rename them, change subnets, delete from the web UI, etc…​ Each time you make a change and then re-run terraform apply, you’ll find that Terraform ensures that the state in the Apstra server is made to exactly match the contents of the resources.tf file.