Skip to content
Merged
6 changes: 3 additions & 3 deletions component/ecr/lifecycle.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
resource "aws_ecr_lifecycle_policy" "app" {
repository = aws_ecr_repository.app.name
resource "aws_ecr_lifecycle_policy" "shared_app_retention" {
repository = aws_ecr_repository.shared_app.name

policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep last 10 images"
description = "Keep last 3 images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
Expand Down
2 changes: 1 addition & 1 deletion component/ecr/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "aws_ecr_repository" "app" {
resource "aws_ecr_repository" "shared_app" {
name = "${var.environment}-app"
image_tag_mutability = "MUTABLE"

Expand Down
4 changes: 2 additions & 2 deletions component/ecr/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
output "repository_url" {
description = "ECR repository URL"
value = aws_ecr_repository.app.repository_url
value = aws_ecr_repository.shared_app.repository_url
}

output "repository_name" {
value = aws_ecr_repository.app.name
value = aws_ecr_repository.shared_app.name
}
8 changes: 4 additions & 4 deletions component/ecs-ec2/autoscaling_group.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_autoscaling_group" "ecs" {
name = "${var.environment}-ecs-asg"
resource "aws_autoscaling_group" "cluster_ec2_shared_asg" {
name = "${var.environment}-cluster-ec2-shared-asg"
vpc_zone_identifier = var.public_subnet_ids

min_size = 1
Expand All @@ -10,13 +10,13 @@ resource "aws_autoscaling_group" "ecs" {
health_check_grace_period = 240

launch_template {
id = aws_launch_template.ecs-launch-template.id
id = aws_launch_template.cluster_ec2_shared_launch_template.id
version = "$Latest"
}

tag {
key = "Name"
value = "${var.environment}-ecs-instance"
value = "${var.environment}-cluster-ec2-shared-instance"
propagate_at_launch = true
}

Expand Down
14 changes: 7 additions & 7 deletions component/ecs-ec2/capacity_provider.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
resource "aws_ecs_capacity_provider" "ecs" {
name = "${var.environment}-cp"
resource "aws_ecs_capacity_provider" "cluster_ec2_shared" {
name = "${var.environment}-cluster-ec2-shared-cp"

auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.ecs.arn
auto_scaling_group_arn = aws_autoscaling_group.cluster_ec2_shared_asg.arn

managed_scaling {
status = "DISABLED" # sandbox: 1대 고정 (나중에 필요하면 ENABLED로)
Expand All @@ -15,12 +15,12 @@ resource "aws_ecs_capacity_provider" "ecs" {
}
}

resource "aws_ecs_cluster_capacity_providers" "this" {
cluster_name = aws_ecs_cluster.this.name
capacity_providers = [aws_ecs_capacity_provider.ecs.name]
resource "aws_ecs_cluster_capacity_providers" "cluster_default_ec2" {
cluster_name = aws_ecs_cluster.cluster_shared.name
capacity_providers = [aws_ecs_capacity_provider.cluster_ec2_shared.name]

default_capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.ecs.name
capacity_provider = aws_ecs_capacity_provider.cluster_ec2_shared.name
weight = 1
base = 1
}
Expand Down
2 changes: 1 addition & 1 deletion component/ecs-ec2/cluster.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resource "aws_ecs_cluster" "this" {
resource "aws_ecs_cluster" "cluster_shared" {
name = "${var.environment}-ecs-cluster"
}
9 changes: 4 additions & 5 deletions component/ecs-ec2/launch_template.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# ECS Optimized AMI (Amazon Linux 2) 최신 ID
data "aws_ssm_parameter" "ecs_ami" {
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}

resource "aws_launch_template" "ecs-launch-template" {
name_prefix = "${var.environment}-ecs-launch-template"
resource "aws_launch_template" "cluster_ec2_shared_launch_template" {
name_prefix = "${var.environment}-cluster-ec2-shared-launch-template"
image_id = data.aws_ssm_parameter.ecs_ami.value
instance_type = var.instance_type

Expand All @@ -15,13 +14,13 @@ resource "aws_launch_template" "ecs-launch-template" {
vpc_security_group_ids = [aws_security_group.ecs_instance_sg.id]

user_data = base64encode(templatefile("${path.module}/userdata.sh.tftpl", {
cluster_name = aws_ecs_cluster.this.name
cluster_name = aws_ecs_cluster.cluster_shared.name
}))

tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.environment}-ecs-instance"
Name = "${var.environment}-cluster-ec2-shared-instance"
Environment = var.environment
}
}
Expand Down
10 changes: 5 additions & 5 deletions component/ecs-ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
output "ecs_cluster_name" {
value = aws_ecs_cluster.this.name
value = aws_ecs_cluster.cluster_shared.name
}

output "ecs_service_name" {
value = aws_ecs_service.app.name
output "schedule_vote_service_name" {
value = aws_ecs_service.schedule_vote.name
}

output "ecs_asg_name" {
value = aws_autoscaling_group.ecs.name
output "cluster_ec2_shared_asg_name" {
value = aws_autoscaling_group.cluster_ec2_shared_asg.name
}
2 changes: 1 addition & 1 deletion component/ecs-ec2/security_group.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "aws_security_group" "ecs_instance_sg" {
name = "${var.environment}-ecs-instance-sg"
description = "Security group for ECS EC2 instances"
description = "Security group for ECS EC2 instances (shared runtime)"
vpc_id = var.vpc_id

# Outbound: 기본 허용
Expand Down
12 changes: 6 additions & 6 deletions component/ecs-ec2/service.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
data "aws_ecs_task_definition" "app" {
data "aws_ecs_task_definition" "schedule_vote" {
task_definition = "${var.environment}-app"
}

resource "aws_ecs_service" "app" {
resource "aws_ecs_service" "schedule_vote" {
name = "${var.environment}-app-svc"
cluster = aws_ecs_cluster.this.id
task_definition = data.aws_ecs_task_definition.app.arn
cluster = aws_ecs_cluster.cluster_shared.id
task_definition = data.aws_ecs_task_definition.schedule_vote.arn
desired_count = 1

lifecycle {
Expand All @@ -17,14 +17,14 @@ resource "aws_ecs_service" "app" {
health_check_grace_period_seconds = 300

capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.ecs.name
capacity_provider = aws_ecs_capacity_provider.cluster_ec2_shared.name
weight = 1
}

deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200

depends_on = [
aws_ecs_cluster_capacity_providers.this
aws_ecs_cluster_capacity_providers.cluster_default_ec2
]
}
2 changes: 1 addition & 1 deletion component/ecs-ec2/task_definition.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "aws_cloudwatch_log_group" "app" {
resource "aws_cloudwatch_log_group" "schedule_vote" {
name = "/ecs/${var.environment}/app"
retention_in_days = 7
}
10 changes: 6 additions & 4 deletions component/ecs-ec2/userdata.sh.tftpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash
set -euo pipefail

# ECS Cluster 등록
echo "ECS_CLUSTER=${cluster_name}" >> /etc/ecs/ecs.config
cat <<EOF > /etc/ecs/ecs.config
ECS_CLUSTER=${cluster_name}
ECS_AGENT_IMAGE=public.ecr.aws/ecs/amazon-ecs-agent:1.75.0
EOF

# (권장) 컨테이너 로그 드라이버 안정화 등을 위해 재시작
systemctl enable --now ecs
systemctl enable ecs
systemctl restart ecs
Loading