📌 테라폼 ECS 배포 테스트
variable "vpc_id" {
description = "ID of the VPC where ECS and ALB are deployed"
type = string
}
variable "public_subnet_ids" {
description = "List of public subnet IDs for ECS tasks"
type = list(string)
}
variable "ecs_cluster_name" {
description = "Name of the existing ECS cluster"
type = string
}
variable "alb_arn" {
description = "ARN of the existing Application Load Balancer"
type = string
}
variable "target_group_arn" {
description = "ARN of the target group for Nginx testing"
type = string
}
variable "ecs_security_group_id" {
description = "Security group ID for ECS tasks"
type = string
}
variable "alb_security_group_id" {
description = "Security group ID for ALB"
type = string
}
vpc_id = "vpc-ID" # AWS 콘솔에서 VPC ID 확인
public_subnet_ids = ["subnet-1", "subnet-2", "subnet-3"] # 서브넷 ID 3개
ecs_cluster_name = "airduck-POC" # 기존 ECS 클러스터 이름
alb_arn = "ALB ARN" # ALB ARN
target_group_arn = "타겟 그룹ARN" # 타겟 그룹 ARN
ecs_security_group_id = "ECS 태스크용 보안 그룹 ID" # ECS 태스크용 보안 그룹 ID
alb_security_group_id = "ALB용 보안 그룹 ID" # ALB용 보안 그룹 ID
region = "ap-northeast-2" # 배포 리전 (필요 시 변경)
#resource "aws_cloudwatch_log_group" "nginx_log_group" {
# name = "/ecs/airduck-nginx-task" # 태스크에서 사용하는 로그 그룹 이름
# retention_in_days = 7 # 로그 보존 기간 (예: 7일, 필요 시 조정)
#}
resource "aws_ecs_task_definition" "nginx_task" {
family = "airduck-nginx" # 태스크 정의 이름
network_mode = "awsvpc" # 퍼블릭 서브넷에서 실행되므로 awsvpc 사용
requires_compatibilities = ["FARGATE"] # Fargate 사용
cpu = "256" # 0.25 vCPU
memory = "512" # 0.5 GB
execution_role_arn = "arn:ecsTaskExecutionRole" # 역할 ARN
runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = "X86_64"
}
container_definitions = jsonencode([
{
name = "nginx-container-test" # 컨테이너 이름
image = ".dkr.ecr.ap-northeast-2.amazonaws.com/nginx" # ECR Nginx 이미지
essential = true # 필수 컨테이너로 설정
portMappings = [
{
containerPort = 80 # Nginx 기본 포트 (443으로 변경 가능)
hostPort = 80 # awsvpc에서는 동일하게 설정
protocol = "tcp"
}
]
environment = [
{
name = "ECS_ENABLE_CONTAINER_METADATA" # 메타데이터 활성화
value = "true"
}
]
logConfiguration = {
logDriver = "awslogs" # CloudWatch 로그 설정
options = {
"awslogs-group" = "/ecs/airduck-nginx" # 로그 그룹
"awslogs-region" = var.region # 변수에서 리전 참조
"awslogs-stream-prefix" = "nginx" # 로그 스트림 접두사
}
}
}
])
# depends_on = [aws_cloudwatch_log_group.nginx_log_group] # 로그 그룹이 먼저 생성되도록
}
resource "aws_ecs_service" "nginx_service" {
name = "airduck-nginx-service" # 서비스 이름
cluster = var.ecs_cluster_name # 클러스터 이름 변수 참조
task_definition = aws_ecs_task_definition.nginx_task.arn # 태스크 정의 참조
desired_count = 3 # 3개 서브넷에 1개씩 태스크 배포
launch_type = "FARGATE" # Fargate 사용
network_configuration {
subnets = var.public_subnet_ids # 퍼블릭 서브넷 3개
security_groups = [var.ecs_security_group_id] # ECS 보안 그룹
assign_public_ip = true # 퍼블릭 IP 할당
}
load_balancer {
target_group_arn = var.target_group_arn # 타겟 그룹 ARN
container_name = "nginx-container-test" # 컨테이너 이름
container_port = 80 # Nginx 포트
}
depends_on = [aws_ecs_task_definition.nginx_task] # 태스크 정의가 먼저 생성되도록
# 가용 영역 리밸런싱 활성화
deployment_controller {
type = "ECS" # 기본값, AZ 리밸런싱을 위해 명시
}
scheduling_strategy = "REPLICA" # 기본값 명시 (필요 시)
enable_ecs_managed_tags = true # ECS 관리 태그 활성화
propagate_tags = "SERVICE" # 태그 전파 설정
}