Monday, 22 February 2021

Terraform AWS SG | Undeclared Input Variable Error

Issue Description

 I got the below error with terraform-aws-security-group (v3.18.0) with terraform version 0.14.7

Error: Reference to undeclared input variable

  on ..\..\modules\terraform-aws-security-group-3.18.0\main.tf line 390, in resource "aws_security_group_rule" "ingress_with_self":

 390:     var.rules[lookup(var.ingress_with_self[count.index], "rule", "_")][2],

An input variable with the name "rules" has not been declared. This variable

can be declared with a variable "rules" {} block.

Error: Reference to undeclared input variable

  on ..\..\modules\terraform-aws-security-group-3.18.0\main.tf line 412, in resource "aws_security_group_rule" "computed_ingress_with_self":

 412:     var.rules[lookup(var.computed_ingress_with_self[count.index], "rule", "_")][0],

An input variable with the name "rules" has not been declared. This variable

can be declared with a variable "rules" {} block.

 

Reason for the above error and solution

*  I couldn't defined a rules file and that's why encountered the issue.

* Created a rules.tf file with below contents and this fixed the issue

$ cat modules/terraform-aws-security-group-3.18.0/rules.tf

variable "rules" {

  description = "Map of known security group rules (define as 'name' = ['from port', 'to port', 'protocol', 'description'])"

  type        = map(list(any))

  # Protocols (tcp, udp, icmp, all - are allowed keywords) or numbers (from https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml):

  # All = -1, IPV4-ICMP = 1, TCP = 6, UDP = 17, IPV6-ICMP = 58

  default = {

    # HTTP

    http-80-tcp   = [80, 80, "tcp", "HTTP"]

    http-8080-tcp = [8080, 8080, "tcp", "HTTP"]

    # HTTPS

    https-443-tcp  = [443, 443, "tcp", "HTTPS"]

    https-8443-tcp = [8443, 8443, "tcp", "HTTPS"]

    # This is a fallback rule to pass to lookup() as default. It does not open anything, because it should never be used.

    _ = ["", "", ""]

  }

}

variable "auto_groups" {

  description = "Map of groups of security group rules to use to generate modules (see update_groups.sh)"

  type        = map(map(list(string)))

  # Valid keys - ingress_rules, egress_rules, ingress_with_self, egress_with_self

  default = {

    activemq = {

      ingress_rules     = ["activemq-5671-tcp", "activemq-8883-tcp", "activemq-61614-tcp", "activemq-61617-tcp", "activemq-61619-tcp"]

      ingress_with_self = ["all-all"]

      egress_rules      = ["all-all"]

    }

    https-443 = {

      ingress_rules     = ["https-443-tcp"]

      ingress_with_self = ["all-all"]

      egress_rules      = ["all-all"]

    }

    https-8443 = {

      ingress_rules     = ["https-8443-tcp"]

      ingress_with_self = ["all-all"]

      egress_rules      = ["all-all"]

    }

  }

}

 

No comments:

Post a Comment