-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.tf
More file actions
163 lines (150 loc) · 8.22 KB
/
variables.tf
File metadata and controls
163 lines (150 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Input Variables
variable "name" {
description = "The name of the security group. This name must be unique within the VPC."
type = string
}
variable "description" {
description = "A description for the security group. Defaults to 'Security group for <name>' if empty."
type = string
default = ""
}
variable "vpc_id" {
description = "The ID of the VPC in which to create the security group."
type = string
validation {
condition = can(regex("^vpc-[0-9a-f]+$", var.vpc_id))
error_message = "The vpc_id must be a valid VPC ID (e.g., vpc-xxxxxxxxxxxxxxxxx)."
}
}
variable "tags" {
description = "A map of tags to assign to the security group."
type = map(string)
default = {}
}
# Ingress Rules Inputs
variable "ingress_rules" {
description = <<-EOT
A list of ingress rules to add to the security group. Each item in the list is an object with the following attributes:
- `description`: (Optional|string) Description of the rule.
- `from_port`: (Required|number) Start of port range (or ICMP type number).
- `to_port`: (Required|number) End of port range (or ICMP code number).
- `protocol`: (Required|string) Protocol. If not icmp, tcp, udp, or all use the protocol number. Use '-1' for all protocols.
- `cidr_blocks`: (Optional|list(string)) List of IPv4 CIDR blocks. Cannot be specified with `self` or `security_groups`.
- `ipv6_cidr_blocks`: (Optional|list(string)) List of IPv6 CIDR blocks. Cannot be specified with `self` or `security_groups`.
- `prefix_list_ids`: (Optional|list(string)) List of prefix list IDs. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks`, `self` or `security_groups`.
- `security_groups`: (Optional|list(string)) List of security group IDs. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks` or `prefix_list_ids`.
- `self`: (Optional|bool) If true, traffic is allowed from the security group itself. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks`, `prefix_list_ids` or `security_groups`.
EOT
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
cidr_blocks = optional(list(string))
ipv6_cidr_blocks = optional(list(string))
prefix_list_ids = optional(list(string))
security_groups = optional(list(string))
self = optional(bool)
}))
default = []
validation {
condition = alltrue([
for rule in var.ingress_rules :
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids == null && rule.security_groups == null && rule.self != null) || # self rule
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids == null && rule.security_groups != null && rule.self == null) || # security_groups rule
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids != null && rule.security_groups == null && rule.self == null) || # prefix_list_ids rule
((rule.cidr_blocks != null || rule.ipv6_cidr_blocks != null) && rule.prefix_list_ids == null && rule.security_groups == null && rule.self == null) # cidr_blocks or ipv6_cidr_blocks rule
])
error_message = "Each ingress rule must specify exactly one source type: cidr_blocks/ipv6_cidr_blocks, prefix_list_ids, security_groups, or self."
}
}
# Variable for simple CIDR-based ingress rules
variable "ingress_with_cidr_blocks" {
description = "List of ingress rules, simpler format for CIDR blocks only. Each object expects `from_port`, `to_port`, `protocol`, `cidr_blocks` (string), and optionally `description`."
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
cidr_blocks = string
}))
default = []
}
# Variable for simple source SG-based ingress rules
variable "ingress_with_source_security_group_id" {
description = "List of ingress rules, simpler format for source security group ID only. Each object expects `from_port`, `to_port`, `protocol`, `source_security_group_id` (string), and optionally `description`."
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
source_security_group_id = string
}))
default = []
}
# Egress Rules Inputs
variable "egress_rules" {
description = <<-EOT
A list of egress rules to add to the security group. Each item in the list is an object with the following attributes:
- `description`: (Optional|string) Description of the rule.
- `from_port`: (Required|number) Start of port range (or ICMP type number). Use 0 for all ports.
- `to_port`: (Required|number) End of port range (or ICMP code number). Use 0 for all ports.
- `protocol`: (Required|string) Protocol. If not icmp, tcp, udp, or all use the protocol number. Use '-1' for all protocols.
- `cidr_blocks`: (Optional|list(string)) List of IPv4 CIDR blocks. Cannot be specified with `self` or `security_groups`.
- `ipv6_cidr_blocks`: (Optional|list(string)) List of IPv6 CIDR blocks. Cannot be specified with `self` or `security_groups`.
- `prefix_list_ids`: (Optional|list(string)) List of prefix list IDs. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks`, `self` or `security_groups`.
- `security_groups`: (Optional|list(string)) List of destination security group IDs. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks` or `prefix_list_ids`.
- `self`: (Optional|bool) If true, traffic is allowed to the security group itself. Cannot be specified with `cidr_blocks`, `ipv6_cidr_blocks`, `prefix_list_ids` or `security_groups`.
EOT
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
cidr_blocks = optional(list(string))
ipv6_cidr_blocks = optional(list(string))
prefix_list_ids = optional(list(string))
security_groups = optional(list(string)) # Destination SG for egress
self = optional(bool)
}))
default = []
validation {
condition = alltrue([
for rule in var.egress_rules :
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids == null && rule.security_groups == null && rule.self != null) || # self rule
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids == null && rule.security_groups != null && rule.self == null) || # security_groups rule
(rule.cidr_blocks == null && rule.ipv6_cidr_blocks == null && rule.prefix_list_ids != null && rule.security_groups == null && rule.self == null) || # prefix_list_ids rule
((rule.cidr_blocks != null || rule.ipv6_cidr_blocks != null) && rule.prefix_list_ids == null && rule.security_groups == null && rule.self == null) # cidr_blocks or ipv6_cidr_blocks rule
])
error_message = "Each egress rule must specify exactly one destination type: cidr_blocks/ipv6_cidr_blocks, prefix_list_ids, security_groups, or self."
}
}
# Variable for simple CIDR-based egress rules
variable "egress_with_cidr_blocks" {
description = "List of egress rules, simpler format for CIDR blocks only. Each object expects `from_port`, `to_port`, `protocol`, `cidr_blocks` (string), and optionally `description`."
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
cidr_blocks = string
}))
default = []
}
# Variable for simple destination SG-based egress rules
variable "egress_with_destination_security_group_id" {
description = "List of egress rules, simpler format for destination security group ID only. Each object expects `from_port`, `to_port`, `protocol`, `destination_security_group_id` (string), and optionally `description`."
type = list(object({
description = optional(string)
from_port = number
to_port = number
protocol = string
destination_security_group_id = string
}))
default = []
}
variable "create_default_egress_rule" {
description = "If true and no egress rules are specified, a default rule allowing all outbound traffic will be created."
type = bool
default = true
}