Skip to content

Commit f81e3a2

Browse files
committed
Revert "feat: use security-group module instead of resource (#117)"
This reverts commit 4988650.
1 parent cf2a6b5 commit f81e3a2

File tree

9 files changed

+106
-937
lines changed

9 files changed

+106
-937
lines changed

README.md

Lines changed: 0 additions & 621 deletions
This file was deleted.

README.yaml

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ usage: |2-
124124
ecs_cluster_arn = aws_ecs_cluster.default.arn
125125
launch_type = var.ecs_launch_type
126126
vpc_id = module.vpc.vpc_id
127-
security_groups = [module.vpc.vpc_default_security_group_id]
127+
security_group_ids = [module.vpc.vpc_default_security_group_id]
128128
subnet_ids = module.subnets.public_subnet_ids
129129
tags = var.tags
130130
ignore_changes_task_definition = var.ignore_changes_task_definition
@@ -138,36 +138,6 @@ usage: |2-
138138
desired_count = var.desired_count
139139
task_memory = var.task_memory
140140
task_cpu = var.task_cpu
141-
142-
security_group_rules = [
143-
{
144-
type = "egress"
145-
from_port = 0
146-
to_port = 0
147-
protocol = -1
148-
cidr_blocks = ["0.0.0.0/0"]
149-
source_security_group_id = null
150-
description = "Allow all outbound traffic"
151-
},
152-
{
153-
type = "ingress"
154-
from_port = 8
155-
to_port = 0
156-
protocol = "icmp"
157-
cidr_blocks = ["0.0.0.0/0"]
158-
source_security_group_id = null
159-
description = "Enables ping command from anywhere, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-ping"
160-
},
161-
{
162-
type = "ingress"
163-
from_port = 80
164-
to_port = 80
165-
protocol = "tcp"
166-
cidr_blocks = []
167-
source_security_group_id = module.vpc.vpc_default_security_group_id
168-
description = "Allow inbound traffic to container port"
169-
}
170-
]
171141
}
172142
```
173143
@@ -227,5 +197,3 @@ contributors:
227197
github: nitrocode
228198
- name: Maxim Mironenko
229199
github: maximmi
230-
- name: Vladimir Syromyatnikov
231-
github: SweetOps

docs/terraform.md

Lines changed: 0 additions & 136 deletions
This file was deleted.

examples/complete/main.tf

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ module "container_definition" {
4747

4848
module "ecs_alb_service_task" {
4949
source = "../.."
50+
alb_security_group = module.vpc.vpc_default_security_group_id
5051
container_definition_json = module.container_definition.json_map_encoded_list
5152
ecs_cluster_arn = aws_ecs_cluster.default.arn
5253
launch_type = var.ecs_launch_type
5354
vpc_id = module.vpc.vpc_id
54-
security_groups = [module.vpc.vpc_default_security_group_id]
55+
security_group_ids = [module.vpc.vpc_default_security_group_id]
5556
subnet_ids = module.subnets.public_subnet_ids
5657
ignore_changes_task_definition = var.ignore_changes_task_definition
5758
network_mode = var.network_mode
@@ -64,35 +65,5 @@ module "ecs_alb_service_task" {
6465
task_memory = var.task_memory
6566
task_cpu = var.task_cpu
6667

67-
security_group_rules = [
68-
{
69-
type = "egress"
70-
from_port = 0
71-
to_port = 0
72-
protocol = -1
73-
cidr_blocks = ["0.0.0.0/0"]
74-
source_security_group_id = null
75-
description = "Allow all outbound traffic"
76-
},
77-
{
78-
type = "ingress"
79-
from_port = 8
80-
to_port = 0
81-
protocol = "icmp"
82-
cidr_blocks = ["0.0.0.0/0"]
83-
source_security_group_id = null
84-
description = "Enables ping command from anywhere, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-ping"
85-
},
86-
{
87-
type = "ingress"
88-
from_port = 80
89-
to_port = 80
90-
protocol = "tcp"
91-
cidr_blocks = []
92-
source_security_group_id = module.vpc.vpc_default_security_group_id
93-
description = "Allow inbound traffic to container port"
94-
}
95-
]
96-
9768
context = module.this.context
9869
}

examples/complete/outputs.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,8 @@ output "task_role_id" {
7979
}
8080

8181
output "service_security_group_id" {
82-
value = module.ecs_alb_service_task.security_group_id
8382
description = "Security Group ID of the ECS task"
84-
}
85-
86-
output "service_security_group_arn" {
87-
value = module.ecs_alb_service_task.security_group_arn
88-
description = "Security Group ARN of the ECS task"
89-
}
90-
91-
output "service_security_group_name" {
92-
value = module.ecs_alb_service_task.security_group_name
93-
description = "Security Group name of the ECS task"
83+
value = module.ecs_alb_service_task.service_security_group_id
9484
}
9585

9686
output "task_definition_family" {

main.tf

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
locals {
22
enabled = module.this.enabled
33
enable_ecs_service_role = module.this.enabled && var.network_mode != "awsvpc" && length(var.ecs_load_balancers) <= 1
4-
security_group_enabled = module.this.enabled && var.security_group_enabled && var.network_mode == "awsvpc"
54
}
65

76
module "task_label" {
@@ -259,19 +258,58 @@ resource "aws_iam_role_policy_attachment" "ecs_exec" {
259258

260259
# Service
261260
## Security Groups
262-
module "security_group" {
263-
source = "cloudposse/security-group/aws"
264-
version = "0.3.1"
261+
resource "aws_security_group" "ecs_service" {
262+
count = local.enabled && var.network_mode == "awsvpc" ? 1 : 0
263+
vpc_id = var.vpc_id
264+
name = module.service_label.id
265+
description = "Allow ALL egress from ECS service"
266+
tags = module.service_label.tags
265267

266-
use_name_prefix = var.security_group_use_name_prefix
267-
rules = var.security_group_rules
268-
description = var.security_group_description
269-
vpc_id = var.vpc_id
268+
lifecycle {
269+
create_before_destroy = true
270+
}
271+
}
272+
273+
resource "aws_security_group_rule" "allow_all_egress" {
274+
count = local.enabled && var.enable_all_egress_rule ? 1 : 0
275+
type = "egress"
276+
from_port = 0
277+
to_port = 0
278+
protocol = "-1"
279+
cidr_blocks = ["0.0.0.0/0"]
280+
security_group_id = join("", aws_security_group.ecs_service.*.id)
281+
}
270282

271-
enabled = local.security_group_enabled
272-
context = module.service_label.context
283+
resource "aws_security_group_rule" "allow_icmp_ingress" {
284+
count = local.enabled && var.enable_icmp_rule ? 1 : 0
285+
description = "Enables ping command from anywhere, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-ping"
286+
type = "ingress"
287+
from_port = 8
288+
to_port = 0
289+
protocol = "icmp"
290+
cidr_blocks = ["0.0.0.0/0"]
291+
security_group_id = join("", aws_security_group.ecs_service.*.id)
273292
}
274293

294+
resource "aws_security_group_rule" "alb" {
295+
count = local.enabled && var.use_alb_security_group ? 1 : 0
296+
type = "ingress"
297+
from_port = var.container_port
298+
to_port = var.container_port
299+
protocol = "tcp"
300+
source_security_group_id = var.alb_security_group
301+
security_group_id = join("", aws_security_group.ecs_service.*.id)
302+
}
303+
304+
resource "aws_security_group_rule" "nlb" {
305+
count = local.enabled && var.use_nlb_cidr_blocks ? 1 : 0
306+
type = "ingress"
307+
from_port = var.nlb_container_port
308+
to_port = var.nlb_container_port
309+
protocol = "tcp"
310+
cidr_blocks = var.nlb_cidr_blocks
311+
security_group_id = join("", aws_security_group.ecs_service.*.id)
312+
}
275313

276314
resource "aws_ecs_service" "ignore_changes_task_definition" {
277315
count = local.enabled && var.ignore_changes_task_definition && ! var.ignore_changes_desired_count ? 1 : 0
@@ -347,7 +385,7 @@ resource "aws_ecs_service" "ignore_changes_task_definition" {
347385
dynamic "network_configuration" {
348386
for_each = var.network_mode == "awsvpc" ? ["true"] : []
349387
content {
350-
security_groups = compact(concat(module.security_group.*.id, var.security_groups))
388+
security_groups = compact(concat(var.security_group_ids, aws_security_group.ecs_service.*.id))
351389
subnets = var.subnet_ids
352390
assign_public_ip = var.assign_public_ip
353391
}
@@ -602,7 +640,7 @@ resource "aws_ecs_service" "default" {
602640
dynamic "network_configuration" {
603641
for_each = var.network_mode == "awsvpc" ? ["true"] : []
604642
content {
605-
security_groups = compact(concat(module.security_group.*.id, var.security_groups))
643+
security_groups = compact(concat(var.security_group_ids, aws_security_group.ecs_service.*.id))
606644
subnets = var.subnet_ids
607645
assign_public_ip = var.assign_public_ip
608646
}

outputs.tf

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,9 @@ output "task_role_id" {
4848
value = join("", aws_iam_role.ecs_task.*.unique_id)
4949
}
5050

51-
output "security_group_id" {
52-
value = module.security_group.id
51+
output "service_security_group_id" {
5352
description = "Security Group ID of the ECS task"
54-
}
55-
56-
output "security_group_arn" {
57-
value = module.security_group.arn
58-
description = "Security Group ARN of the ECS task"
59-
}
60-
61-
output "security_group_name" {
62-
value = module.security_group.name
63-
description = "Security Group name of the ECS task"
53+
value = join("", aws_security_group.ecs_service.*.id)
6454
}
6555

6656
output "task_definition_family" {

test/src/examples_complete_test.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,21 @@ package test
22

33
import (
44
"encoding/json"
5-
"math/rand"
6-
"strconv"
7-
"testing"
8-
"time"
9-
105
"github.com/gruntwork-io/terratest/modules/terraform"
116
"github.com/stretchr/testify/assert"
7+
"testing"
128
)
139

1410
// Test the Terraform module in examples/complete using Terratest.
1511
func TestExamplesComplete(t *testing.T) {
1612
t.Parallel()
1713

18-
rand.Seed(time.Now().UnixNano())
19-
20-
randId := strconv.Itoa(rand.Intn(100000))
21-
attributes := []string{randId}
22-
2314
terraformOptions := &terraform.Options{
2415
// The path to where our Terraform code is located
2516
TerraformDir: "../../examples/complete",
2617
Upgrade: true,
2718
// Variables to pass to our Terraform code using -var-file options
2819
VarFiles: []string{"fixtures.us-east-2.tfvars"},
29-
Vars: map[string]interface{}{
30-
"attributes": attributes,
31-
},
3220
}
3321

3422
// At the end of the test, run `terraform destroy` to clean up any resources that were created
@@ -69,61 +57,45 @@ func TestExamplesComplete(t *testing.T) {
6957
// Run `terraform output` to get the value of an output variable
7058
ecsClusterId := terraform.Output(t, terraformOptions, "ecs_cluster_id")
7159
// Verify we're getting back the outputs we expect
72-
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-alb-service-task-"+randId, ecsClusterId)
60+
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-alb-service-task", ecsClusterId)
7361

7462
// Run `terraform output` to get the value of an output variable
7563
ecsClusterArn := terraform.Output(t, terraformOptions, "ecs_cluster_arn")
7664
// Verify we're getting back the outputs we expect
77-
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-alb-service-task-"+randId, ecsClusterArn)
65+
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-alb-service-task", ecsClusterArn)
7866

7967
// Run `terraform output` to get the value of an output variable
8068
ecsExecRolePolicyName := terraform.Output(t, terraformOptions, "ecs_exec_role_policy_name")
8169
// Verify we're getting back the outputs we expect
82-
assert.Equal(t, "eg-test-ecs-alb-service-task-"+randId+"-exec", ecsExecRolePolicyName)
70+
assert.Equal(t, "eg-test-ecs-alb-service-task-exec", ecsExecRolePolicyName)
8371

8472
// Run `terraform output` to get the value of an output variable
8573
serviceName := terraform.Output(t, terraformOptions, "service_name")
8674
// Verify we're getting back the outputs we expect
87-
assert.Equal(t, "eg-test-ecs-alb-service-task-"+randId, serviceName)
75+
assert.Equal(t, "eg-test-ecs-alb-service-task", serviceName)
8876

8977
// Run `terraform output` to get the value of an output variable
9078
taskDefinitionFamily := terraform.Output(t, terraformOptions, "task_definition_family")
9179
// Verify we're getting back the outputs we expect
92-
assert.Equal(t, "eg-test-ecs-alb-service-task-"+randId, taskDefinitionFamily)
80+
assert.Equal(t, "eg-test-ecs-alb-service-task", taskDefinitionFamily)
9381

9482
// Run `terraform output` to get the value of an output variable
9583
taskExecRoleName := terraform.Output(t, terraformOptions, "task_exec_role_name")
9684
// Verify we're getting back the outputs we expect
97-
assert.Equal(t, "eg-test-ecs-alb-service-task-"+randId+"-exec", taskExecRoleName)
85+
assert.Equal(t, "eg-test-ecs-alb-service-task-exec", taskExecRoleName)
9886

9987
// Run `terraform output` to get the value of an output variable
10088
taskExecRoleArn := terraform.Output(t, terraformOptions, "task_exec_role_arn")
10189
// Verify we're getting back the outputs we expect
102-
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-alb-service-task-"+randId+"-exec", taskExecRoleArn)
90+
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-alb-service-task-exec", taskExecRoleArn)
10391

10492
// Run `terraform output` to get the value of an output variable
10593
taskRoleName := terraform.Output(t, terraformOptions, "task_role_name")
10694
// Verify we're getting back the outputs we expect
107-
assert.Equal(t, "eg-test-ecs-alb-service-task-"+randId+"-task", taskRoleName)
95+
assert.Equal(t, "eg-test-ecs-alb-service-task-task", taskRoleName)
10896

10997
// Run `terraform output` to get the value of an output variable
11098
taskRoleArn := terraform.Output(t, terraformOptions, "task_role_arn")
11199
// Verify we're getting back the outputs we expect
112-
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-alb-service-task-"+randId+"-task", taskRoleArn)
113-
114-
// Run `terraform output` to get the value of an output variable
115-
securityGroupName := terraform.Output(t, terraformOptions, "service_security_group_name")
116-
expectedSecurityGroupName := "eg-test-ecs-alb-service-task-" + randId + "-service"
117-
// Verify we're getting back the outputs we expect
118-
assert.Equal(t, expectedSecurityGroupName, securityGroupName)
119-
120-
// Run `terraform output` to get the value of an output variable
121-
securityGroupID := terraform.Output(t, terraformOptions, "service_security_group_id")
122-
// Verify we're getting back the outputs we expect
123-
assert.Contains(t, securityGroupID, "sg-", "SG ID should contains substring 'sg-'")
124-
125-
// Run `terraform output` to get the value of an output variable
126-
securityGroupARN := terraform.Output(t, terraformOptions, "service_security_group_arn")
127-
// Verify we're getting back the outputs we expect
128-
assert.Contains(t, securityGroupARN, "arn:aws:ec2", "SG ID should contains substring 'arn:aws:ec2'")
100+
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-alb-service-task-task", taskRoleArn)
129101
}

0 commit comments

Comments
 (0)