From 4aab8d4d2df0bc8fd9210aa1f71e353fe4900ee0 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Tue, 27 Aug 2024 18:11:55 +0900 Subject: [PATCH 1/3] [WIP] From 2c96edfbca0d9f2b6b6a0f135250b4f8e1355d08 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Tue, 27 Aug 2024 19:14:36 +0900 Subject: [PATCH 2/3] feat: execute ecs task in bridge network mode --- alb.tf | 2 +- ec2.tf | 7 +++++++ ecs.tf | 8 ++------ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/alb.tf b/alb.tf index 3b8f908..ce7ae54 100644 --- a/alb.tf +++ b/alb.tf @@ -33,7 +33,7 @@ resource "aws_lb_target_group" "nginx_tg" { port = 80 protocol = "HTTP" vpc_id = aws_vpc.main.id - target_type = "ip" + target_type = "instance" } resource "aws_lb_listener" "nginx_listener" { diff --git a/ec2.tf b/ec2.tf index c505cab..e915fc2 100644 --- a/ec2.tf +++ b/ec2.tf @@ -39,6 +39,13 @@ resource "aws_security_group" "ecs_instance_sg" { name_prefix = "ecs-instance-sg-" vpc_id = aws_vpc.main.id + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + security_groups = [aws_security_group.nginx_sg.id] + } + egress { from_port = 0 to_port = 0 diff --git a/ecs.tf b/ecs.tf index c343f52..9459e7c 100644 --- a/ecs.tf +++ b/ecs.tf @@ -21,7 +21,7 @@ resource "aws_iam_role" "ecs_task_exec" { resource "aws_ecs_task_definition" "nginx_task" { family = "nginx_task" - network_mode = "awsvpc" + network_mode = "bridge" execution_role_arn = aws_iam_role.ecs_task_exec.arn requires_compatibilities = ["EC2"] cpu = "256" @@ -72,12 +72,8 @@ resource "aws_ecs_service" "nginx_service" { name = "nginx_service" cluster = aws_ecs_cluster.main.id task_definition = aws_ecs_task_definition.nginx_task.arn - desired_count = 2 + desired_count = 1 launch_type = "EC2" - network_configuration { - subnets = [aws_subnet.sub.id] - security_groups = [aws_security_group.ecs_http_sg.id] - } depends_on = [aws_lb_target_group.nginx_tg] load_balancer { target_group_arn = aws_lb_target_group.nginx_tg.arn From f40ae975ac79c568cdcee84843b74734834edde0 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Wed, 28 Aug 2024 11:51:46 +0900 Subject: [PATCH 3/3] feat: add network mode bridge --- ec2.tf | 4 ++-- ecs.tf | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ec2.tf b/ec2.tf index e915fc2..d647dcc 100644 --- a/ec2.tf +++ b/ec2.tf @@ -40,8 +40,8 @@ resource "aws_security_group" "ecs_instance_sg" { vpc_id = aws_vpc.main.id ingress { - from_port = 80 - to_port = 80 + from_port = 32768 + to_port = 60999 protocol = "tcp" security_groups = [aws_security_group.nginx_sg.id] } diff --git a/ecs.tf b/ecs.tf index 9459e7c..ecf8b87 100644 --- a/ecs.tf +++ b/ecs.tf @@ -34,7 +34,7 @@ resource "aws_ecs_task_definition" "nginx_task" { portMappings = [ { containerPort = 80 - hostPort = 80 + hostPort = 0 } ] logConfiguration = { @@ -72,9 +72,9 @@ resource "aws_ecs_service" "nginx_service" { name = "nginx_service" cluster = aws_ecs_cluster.main.id task_definition = aws_ecs_task_definition.nginx_task.arn - desired_count = 1 + desired_count = 3 launch_type = "EC2" - depends_on = [aws_lb_target_group.nginx_tg] + depends_on = [aws_lb_target_group.nginx_tg] load_balancer { target_group_arn = aws_lb_target_group.nginx_tg.arn container_name = "nginx"