hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.37k stars 9.49k forks source link

Unpacking arguments for module calls #34405

Open Satak opened 9 months ago

Satak commented 9 months ago

Terraform Version

1.6

Use Cases

Simplify module calls that have huge amount of input variables. Of course you can refactor the module to just take 1 map as input variable but then you loose the validation and default values from input variable declarations.

This is an example how in Python you can pass multiple arguments by just unpacking a dictionary with ** operator.

def my_func(name, age, is_enabled):
    print(name, age, is_enabled)

config = {
    "name": "John",
    "age": 22,
    "is_enabled": True
}

my_func(**config)

Attempted Solutions

locals {
    config = {
        name = "john"
        age = 30
        is_enabled = true
    }
}

module "test" {
    source    = "./module"
    arguments = **local.config
}

# instead of:

module "test" {
    source     = "./module"
    name       = local.config.name
    age        = local.config.age
    is_enabled = local.config.is_enabled
}

Proposal

module "test" {
    source    = "./module"
    arguments = **local.config
}

Or just have the magical arguments (or similar parameter) that accepts complex object and under the hood unpacks the object and passes the values to matching input variables declared in the module.

module "test" {
    source    = "./module"
    arguments = local.config
}

References

No response

crw commented 9 months ago

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!

chappaPraveen commented 1 week ago

One more use case is while creating dynamic blocks with a list of different map objects.

variable "rules_list"{
type = list(map(string))
default=[
{"action":"ADD_HTTP_REQUEST_HEADER", "header":"custom_h1", "value":"custom_value"},
{ "action" :"CONTROL_ACCESS_USING_HTTP_METHODS", "allowed_methods = ["GET", "POST"] , status_code     ="405"}]
}

resource lbr_rule_set "my_lb_rule_set"{

dynamic items{
for_each = var.rules_list
content = **items.value
}

load_balancer_id = var.lb_id
name = "my_lb_rule_set"
}