Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Loops with the string directive

Sumit Rawal answered on May 14, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • looping through strings

  • Loops with the string directive

    0

    Earlier in this blog post series, you learned about string interpolations, which allow you to reference Terraform code within strings:

    "Hello, ${var.name}"

    String directives allow you to use control statements (e.g., for-loops and if-statements) within strings using a syntax similar to string interpolations, but instead of a dollar sign and curly braces (${…}), you use a percent sign and curly braces (%{…}).

    Terraform supports two types of string directives: for-loops and conditionals. In this section, we’ll go over for-loops; we’ll come back to conditionals later in the blog post. The for string directive uses the following syntax:

    %{ for in }%{ endfor }

    where COLLECTION is a list or map to loop over, ITEM is the local variable name to assign to each item in COLLECTION, and BODY is what to render each iteration (which can reference ITEM). Here’s an example:

    variable "names" {

    description = "Names to render"

    type = list(string)

    default = ["neo", "trinity", "morpheus"]

    }

    output "for_directive" {

    value = "%{ for name in var.names }${name}, %{ endfor }"

    }

    When you run terraform apply, you get the following output:

    $ terraform apply

    (...)

    Outputs:

    for_directive = "neo, trinity, morpheus, "  

    Popularity 1/10 Helpfulness 1/10 Language whatever
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 14 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.