Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for unanchored dynamic blocks #21219

Closed
siennathesane opened this issue May 6, 2019 · 2 comments
Closed

Support for unanchored dynamic blocks #21219

siennathesane opened this issue May 6, 2019 · 2 comments

Comments

@siennathesane
Copy link

Current Terraform Version

terraform --version
Terraform v0.12.0-beta1

Use-cases

I'd like to be able to create unanchored dynamic resource blocks.

This allows me to pass a module a pipelined variable that allows me to dynamically define a resource type for highly repetitive tasks. For example, if I have R regional deployments with H hostnames, I then have to provide R*H A record resources. This starts to become untenable in large distributed systems with dozens of hostnames per system. If I were to support both IPv4 and IPv6 records, then I would have R*2H records, thus becoming unmaintainable. By supporting dynamic resource types, it allows me to create a baseline definition for a record, then pass in pipeline data which can be resolved during the planning phase.

Attempted Solutions

None yet, but I'm happy to take a stab at it in my free time if someone can show me where things are.

Proposal

# static_ip_map = {
#     uswest = {
#         host.domain.com = "1.2.3.4"
#         host1.domain.com = "1.2.3.5"
#         host2.domain.com = "1.2.3.6"
#         host3.domain.com = "1.2.3.7"
#         ...
#     }
# }
variable "static_ip_map" {
    type = map(map(object({record = string})))
}

dynamic "resource" {
    for_each = var.static_ip_map
    content {
        resource "google_dns_record_set" "var.a-record.region.record.key" {
            name = "${a-record.region.record.key}"
            managed_zone = "${a-record.region}.${var.base_domain}"
            type = "A"
            ttl  = 300
            rrdatas = ["${a-record.region.record.value}"]
        }
    }
}

References

Haven't seen any yet.

@apparentlymart
Copy link
Member

Hi @mxplusb! Thanks for opening this issue.

I think what you're describing here is the same use-case as #17179, where I left some notes about a slightly different approach we're planning to take by adding a for_each meta-argument that can be used in a similar way to count, leaving the resource blocks at the top level.

The top-level objects in a Terraform configuration cannot be dynamic because Terraform needs to be able to process them before evaluating any expressions in order to build the dependency graph for traversal. However, as we can see with the existing count feature it is possible for Terraform to treat each resource block as a single node during initial processing and then produce multiple instances for that node dynamically, and so this for_each feature builds on that by providing a cleaner syntax and also by allowing the instance identifiers to optionally be string map keys instead of numeric list indices, thus allowing you to represent sets of resources which do not have an inherent order.

I didn't quite follow exactly how you intended var.a-record to be interpreted in your example since that variable wasn't defined, but based on your description of the use-case I think for_each would meet it, if combined with some other expression features to flatten your multi-level map into a single map of an object type.

Since we already have #17179 open to track this use-case, I'm going to close this out just to consolidate the discussion. As you can see over there, we're hoping to finish the for_each feature for resources in a 0.12 minor release, building on the groundwork laid so far for 0.12.0.

@ghost
Copy link

ghost commented Jul 26, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants