Skip to content

Commit 31db0b9

Browse files
Make cluster security group idempotent [GH-49]
[GH-49]: #49
1 parent 94f19dd commit 31db0b9

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Available targets:
129129
| parameter | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another | object | `<list>` | no |
130130
| port | Redis port | number | `6379` | no |
131131
| replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no |
132-
| security_groups | Security Group IDs | list(string) | `<list>` | no |
132+
| security_groups | A list of security group IDs that are allowed ingress to the cluster | list(string) | `<list>` | no |
133133
| snapshot_retention_limit | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. | number | `0` | no |
134134
| snapshot_window | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. | string | `06:30-07:30` | no |
135135
| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no |

docs/terraform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| parameter | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another | object | `<list>` | no |
2727
| port | Redis port | number | `6379` | no |
2828
| replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no |
29-
| security_groups | Security Group IDs | list(string) | `<list>` | no |
29+
| security_groups | A list of security group IDs that are allowed ingress to the cluster | list(string) | `<list>` | no |
3030
| snapshot_retention_limit | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. | number | `0` | no |
3131
| snapshot_window | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. | string | `06:30-07:30` | no |
3232
| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no |

main.tf

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@ resource "aws_security_group" "default" {
1717
vpc_id = var.vpc_id
1818
name = module.label.id
1919

20-
ingress {
21-
from_port = var.port # Redis
22-
to_port = var.port
23-
protocol = "tcp"
24-
security_groups = var.security_groups
25-
}
20+
tags = module.label.tags
21+
}
2622

27-
egress {
28-
from_port = 0
29-
to_port = 0
30-
protocol = "-1"
31-
cidr_blocks = ["0.0.0.0/0"]
32-
}
23+
resource "aws_security_group_rule" "default_ingress" {
24+
for_each = toset(var.security_groups)
25+
description = "default ingress"
26+
type = "ingress"
27+
from_port = var.port # Redis
28+
to_port = var.port
29+
protocol = "tcp"
30+
source_security_group_id = each.value
3331

34-
tags = module.label.tags
32+
security_group_id = aws_security_group.default.*.id
33+
}
34+
35+
resource "aws_security_group_rule" "default_egress" {
36+
description = "default egress"
37+
type = "ingress"
38+
from_port = 0
39+
to_port = 0
40+
protocol = "-1"
41+
source_security_group_id = aws_security_group.default
42+
43+
security_group_id = aws_security_group.default.*.id
3544
}
3645

3746
locals {

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "name" {
2424
variable "security_groups" {
2525
type = list(string)
2626
default = []
27-
description = "Security Group IDs"
27+
description = "A list of security group IDs that are allowed ingress to the cluster"
2828
}
2929

3030
variable "vpc_id" {

0 commit comments

Comments
 (0)