Skip to content

Instantly share code, notes, and snippets.

@hrmsk66
Last active March 26, 2022 04:50
Show Gist options
  • Save hrmsk66/af306daccf4d116581505d829ee2f235 to your computer and use it in GitHub Desktop.
Save hrmsk66/af306daccf4d116581505d829ee2f235 to your computer and use it in GitHub Desktop.
Create backend definitions from a list of maps

Create backend definitions from a list of maps

variables.tf

variable "backends" {
    type = list(map(string))
}

terraform.tfvars

backends = [
    {
        "backendname" = "backend1",
        "hostname" = "b1.example.com",
    },
    {
        "backendname" = "backend2",
        "hostname" = "b2.example.com",
    },
]

backend.tftpl

%{ for backend in backends ~}
backend F_${backend.backendname} {
    .between_bytes_timeout = 10s;
    .connect_timeout = 1s;
    .dynamic = true;
    .first_byte_timeout = 15s;
    .host = "${backend.hostname}";
    .max_connections = 200;
    .port = "443";
    .share_key = "5qCtfTZWtnUa31wp3vynpl";
    .ssl = true;
    .ssl_cert_hostname = "${backend.hostname}";
    .ssl_check_cert = always;
    .ssl_sni_hostname = "${backend.hostname}";
    .bypass_local_route_table = true;
    .probe = {
      .dummy = true;
    }
}
%{ endfor ~}

main.tf

terraform {
  required_providers {
    fastly = {
      source  = "fastly/fastly"
      version = ">= 1.0.0"
    }
  }
}

resource "fastly_service_vcl" "myservice" {
  name = "myservice"

  domain {
    name = "test.hkakehas.tokyo"
  }

  snippet {
    name = "backends"
    type = "init"
    content = templatefile("${path.module}/backend.tftpl", { backends = var.backends })
  }

  force_destroy = true
}

terraform apply

terraform apply -auto-approve
fastly_service_vcl.myservice: Refreshing state... [id=5qCtfTZWtnUa31wp3vynpl]

Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # fastly_service_vcl.myservice will be updated in-place
  ~ resource "fastly_service_vcl" "myservice" {
      ~ active_version     = 2 -> (known after apply)
      ~ cloned_version     = 2 -> (known after apply)
        id                 = "5qCtfTZWtnUa31wp3vynpl"
        name               = "myservice"
        # (6 unchanged attributes hidden)

( snip )

      + snippet {
          + content  = <<-EOT
                backend F_backend1 {
                    .between_bytes_timeout = 10s;
                    .connect_timeout = 1s;
                    .dynamic = true;
                    .first_byte_timeout = 15s;
                    .host = "b1.example.com";
                    .max_connections = 200;
                    .port = "443";
                    .share_key = "5qCtfTZWtnUa31wp3vynpl";
                    .ssl = true;
                    .ssl_cert_hostname = "b1.example.com";
                    .ssl_check_cert = always;
                    .ssl_sni_hostname = "b1.example.com";
                    .bypass_local_route_table = true;
                    .probe = {
                      .dummy = true;
                    }
                }
                backend F_backend2 {
                    .between_bytes_timeout = 10s;
                    .connect_timeout = 1s;
                    .dynamic = true;
                    .first_byte_timeout = 15s;
                    .host = "b2.example.com";
                    .max_connections = 200;
                    .port = "443";
                    .share_key = "5qCtfTZWtnUa31wp3vynpl";
                    .ssl = true;
                    .ssl_cert_hostname = "b2.example.com";
                    .ssl_check_cert = always;
                    .ssl_sni_hostname = "b2.example.com";
                    .bypass_local_route_table = true;
                    .probe = {
                      .dummy = true;
                    }
                }
            EOT
          + name     = "backends"
          + priority = 100
          + type     = "init"
        }
        # (1 unchanged block hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
fastly_service_vcl.myservice: Modifying... [id=5qCtfTZWtnUa31wp3vynpl]
fastly_service_vcl.myservice: Still modifying... [id=5qCtfTZWtnUa31wp3vynpl, 10s elapsed]
fastly_service_vcl.myservice: Still modifying... [id=5qCtfTZWtnUa31wp3vynpl, 20s elapsed]
fastly_service_vcl.myservice: Modifications complete after 23s [id=5qCtfTZWtnUa31wp3vynpl]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment