이번엔 전 시간에 terraform으로 띄웠던 network위에 ecs backend를 띄워도도록 하겠습니다.
전에 연동시킨 git에 tarraform을 위한 백엔드 프로젝트를 push해야 합니다. 해당 코드를 하나의 폴더에 넣고 push 해줍니다.
# main.tf
#######################
# dev-network outputs 참조를 위한 terraform_remote_state 선언
#######################
data "terraform_remote_state" "dev-network" {
backend = "remote"
config = {
organization = "<본인 terraform organization>"
workspaces = {
name = "<본인 terraform workspace network 이름>"
}
}
}
#######################
# ecs 클러스터 생성
#######################
# ecs 클러스터 생성
resource "aws_ecs_cluster" "test_velog_dev_cluster" {
name = "test-velog-dev-cluster"
}
# 용량 공급자 fargate 생성
resource "aws_ecs_cluster_capacity_providers" "test_velog_dev_cluster_capacity_provider" {
cluster_name = aws_ecs_cluster.test_velog_dev_cluster.name
capacity_providers = ["FARGATE"]
default_capacity_provider_strategy {
base = 1
weight = 100
capacity_provider = "FARGATE"
}
}
#######################
# ecs 서비스 생성 - backend
#######################
resource "aws_ecs_service" "backend" {
name = "backend"
cluster = aws_ecs_cluster.test_velog_dev_cluster.id
task_definition = data.aws_ecs_task_definition.ecs_backend_td.task_definition
desired_count = 1
network_configuration{
subnets = [data.terraform_remote_state.dev-network.outputs.test_velog_private_a_subnet_id,data.terraform_remote_state.dev-network.outputs.test_velog_private_c_subnet_id]
security_groups = [aws_security_group.test_velog_ecs_service_backend_sg.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.test_velog_backend_tg.arn
container_name = "backend"
container_port = 8080
}
}
#######################
# 타겟 그룹 생성 - backend
#######################
resource "aws_lb_target_group" "test_velog_backend_tg" {
name = "test-velog-backend-tg"
port = 8080
protocol = "HTTP"
target_type = "ip"
vpc_id = data.terraform_remote_state.dev-network.outputs.test_velog_seoul_vpc_id
health_check {
path = "/health"
port = 8080
protocol = "HTTP"
healthy_threshold = 3
unhealthy_threshold = 3
}
}
#######################
# alb 리스너 생성
#######################
resource "aws_lb_listener" "ecs_cluster_alb_listener" {
load_balancer_arn = data.terraform_remote_state.dev-network.outputs.test_velog_ecs_cluster_alb_arn
port = "8080"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.test_velog_backend_tg.arn
}
}
#######################
# ecs-service-backend security group 생성
#######################
# security group 생성
resource "aws_security_group" "test_velog_ecs_service_backend_sg" {
name = "test-velog-ecs-service_backend-sg"
description = "allow to access ecs service-backend from alb"
vpc_id = data.terraform_remote_state.dev-network.outputs.test_velog_seoul_vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# security group rule 생성
resource "aws_security_group_rule" "ecs_backend_sg_ingress_rule" {
type = "ingress"
from_port = 8080
to_port = 8080
protocol = "tcp"
source_security_group_id = data.terraform_remote_state.dev-network.outputs.test_velog_ecs_cluster_alb_sg
security_group_id = aws_security_group.test_velog_ecs_service_backend_sg.id
}
# task definition 참조
data "aws_ecs_task_definition" "ecs_backend_td"{
task_definition = var.ecs_task_definition_family
}
여기서 config에 들어갈 organization은 본인의 organization 명을 적고 workspace name은 전 포스팅에서 만든 network를 적습니다.
# var.tf
variable "ecs_task_definition_family"{
type = string
default = "test-velog-ecs-tf" # 본인 작업 정의로 입력
}
var.tf에 적어야 하는 작업 정의는 미리 만들어져 있어야 합니다.
이제 backend를 위한 workspace를 생성합니다.
전 포스팅과 똑같이 환경변수를 추가해 줍니다.
전 포스팅에서 생성한 network에 들어가서 생성한 backend를 다음과 같이 remote-state-sharing 합니다.
다시 backend workspace에 들어가 run apply를 하면 backend ecs에 자동으로 aws에서 동작하는 것을 확인할 수 있습니다.