[AWS] EC2로 ECS를 사용할때 알아야할 네트워크 모드

김태훈·2023년 8월 24일
0

성균관대 Skkuding

목록 보기
1/16
post-thumbnail

학교 동아리에서 Fargate 기반의 ECS를 EC2기반으로 바꾸는 중 알게된 사실을 정리하려 한다.
terraform apply를 하였는데 다음과 같은 오류 메시지가 발생했다.

Error: updating ECS Service (~~): 
InvalidParameterException: The provided target group ~~
has target type instance, which is incompatible with the awsvpc network mode 
specified in the task definition.

결국 alb의 타겟그룹을 aws network모드를 지원하지 않는다는 내용인데,

target group이 instance (ec2) 인데, ec2를 사용하게 되면 awsvpc를 사용할 수 없다.
내가 찾아낸 자료는 다음과 같다.

https://stackoverflow.com/questions/60502112/ecs-target-type-ip-is-incompatible-with-the-bridge-network-mode-specified-in-t

위 스택오버플로우 내용에서는 다음과 같이 말한다.

https://docs.aws.amazon.com/ko_kr/AmazonECS/latest/bestpracticesguide/networking-networkmode.html
본격적인 내용에 앞서서 위 자료를 참고하면 좋을 것 같다.
awsvpc의 단점을 이야기한 부분에서 "awsvpc네트워크 모드를 Amazon EC2 인스턴스에서 호스팅하는 작업을 사용하는 경우 EC2 인스턴스에 연결할 수 있는 ENI 개수에는 한도가 있다는 것입니다. 이렇게 하면 각 인스턴스에 배치할 수 있는 작업 수가 제한됩니다."
라는 내용이 존재한다.

awsvpc를 쓰게되면 ec2 인스턴스 하나마다 3개의 콘테이너를 배치할 수 있고, awsvpc는 ENI를 각 태스크마다 제공한다.

we could only place 3 containers per EC2 instance, as awsvpc network mode will attach an ENI (Elastic Network Interface) to each task. You can only have 4 ENIs per EC2 instance (depending on the instance type), so the cluster was over-provisioning instances to place the service tasks.

그리고, ec2 instance와의 매핑을 동적 포트 매핑으로 해결했다고 한다.

그 후, task definition도 수정하자

resource "aws_ecs_task_definition" "client_api" {
  family                   = "Codedang-Client-Api"
  requires_compatibilities = ["EC2"]
  network_mode             = "bridge"
  container_definitions = templatefile("${path.module}/backend/task-definition.tftpl", {
    task_name = "Codedang-Client-Api",
    # aurora-posrgresql
    # database_url      = "postgresql://${var.postgres_username}:${random_password.postgres_password.result}@${aws_rds_cluster.cluster.endpoint}:${var.postgres_port}/skkuding?schema=public",

    # posrgresql (free tier)
    database_url      = "postgresql://${var.postgres_username}:${random_password.postgres_password.result}@${aws_db_instance.db-test.endpoint}/skkuding?schema=public",
    ecr_uri           = var.ecr_client_uri,
    container_port    = 4000,
    cloudwatch_region = var.region,
    redis_host        = aws_elasticache_replication_group.db_cache.configuration_endpoint_address,
    redis_port        = var.redis_port,
    jwt_secret        = random_password.jwt_secret.result,
    nodemailer_from   = "Codedang <noreply@codedang.com>",
    rabbitmq_host     = "${aws_mq_broker.judge_queue.id}.mq.${var.region}.amazonaws.com}",
    rabbitmq_port     = var.rabbitmq_port,
    rabbitmq_username = var.rabbitmq_username,
    rabbitmq_password = random_password.rabbitmq_password.result,
    rabbitmq_vhost    = rabbitmq_vhost.vh.name,
  })

  execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
}
profile
기록하고, 공유합시다

0개의 댓글