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.61k stars 9.55k forks source link

Assign a resource to an another resource based on a condition #22078

Closed KumarManoj-S closed 3 years ago

KumarManoj-S commented 5 years ago

Hi,

we have different environments such as prod|staging|showcase. I found a way how to create a resource based on the environments using count. Is there any way to assign a resource to an another resource based on a condition?

To be more specific, we have WAF setup. Web ACL has list of rules and I want to attach a rule based on a condition. Below is my acl resource, (I want this whitelisting rule needs to be there for all the three environments)

    resource "aws_wafregional_web_acl" "waf_acl" {
      name        = "${local.aws_resource_naming}-acl"
      metric_name = "MetricName"

      default_action {
         type = "BLOCK"
      }

      rule {
       action {
        type = "ALLOW"
       }

      priority = 1
      rule_id  = "${aws_wafregional_rule.ips_whitelist_waf_rule.id}"
      type     = "REGULAR"
     }
    }

And I want to attach one more rule but based on a condition, below is the rule that I want to add only on showcase and staging.

    rule {
     action {
       type = "ALLOW"
     }
    priority = 2
    rule_id  = "${aws_wafregional_rule.integration_tests_rule.id}"
    type     = "REGULAR"
    }

Is there a way in terraform to achieve this? Can anybody help me how to handle this?

jbardin commented 3 years ago

Hello,

Terraform now has dynamic block and for-expressions for constructing and assigning complex types, so I'm going to close this out. If you have more questions, we use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

ghost commented 3 years ago

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.