Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
4ea10e1
Init Deployment
yunhoch0i Jun 20, 2025
e3d4ab8
ECR demo
yunhoch0i Jun 23, 2025
3352c7d
Merge pull request #11 from yunhoch0i/main
imyourhopeee Jun 24, 2025
bf11ac5
코드 구조 리팩토링
yunhoch0i Jul 5, 2025
79fe8b4
ECR 계정 분리 및 ECS 연동
yunhoch0i Jul 5, 2025
68e3d4c
리소스 별 모듈화
yunhoch0i Jul 5, 2025
389103c
output.tf 수정
yunhoch0i Jul 5, 2025
ccd1a9a
terraform state를 통해 이미지 로드
yunhoch0i Jul 5, 2025
7bc4f0d
account 별 S3 관리
yunhoch0i Jul 5, 2025
2334255
최신 AMI 적용
yunhoch0i Jul 5, 2025
33351af
리소스 별 모듈화
yunhoch0i Jul 5, 2025
8e93f2a
Merge branch 'feat/#10' into main
yunhoch0i Jul 5, 2025
d58e78e
구조 정리
yunhoch0i Jul 6, 2025
e260dad
CI/CD .tf없는 폴더 실행 X
yunhoch0i Jul 6, 2025
588676a
CI/CD Test
yunhoch0i Jul 6, 2025
53197da
폴더 구조 개편
yunhoch0i Jul 6, 2025
1d0cceb
CI/CD Test
yunhoch0i Jul 6, 2025
90d152d
arn 연동 설정
yunhoch0i Jul 6, 2025
8b1d54a
중복 변수 삭제
yunhoch0i Jul 6, 2025
7af192c
vpc/variables.tf 수정
yunhoch0i Jul 6, 2025
cebfbdb
data 블록 수정
yunhoch0i Jul 6, 2025
5200244
오타 수정
yunhoch0i Jul 6, 2025
be1a5ab
EC2 인스턴스 변경
yunhoch0i Jul 6, 2025
0cc4fa9
remote state 오타 수정
yunhoch0i Jul 6, 2025
d0292ec
HTTPS 설정
yunhoch0i Jul 7, 2025
641e708
fix fmt
imyourhopeee Jul 7, 2025
a93055f
vpc 설정
yunhoch0i Jul 7, 2025
0eca6c5
오타 수정
yunhoch0i Jul 7, 2025
ca4ab9e
acm backend 수정
yunhoch0i Jul 7, 2025
0bbd880
tfsec scan 문제 해결
yunhoch0i Jul 8, 2025
d826715
security_group 종속성 문제 해결
yunhoch0i Jul 8, 2025
70cbfad
루트 디렉토리 참조, .tf 없는 폴더는 실행 X
rnjsdbwlsqwer Jul 6, 2025
08b12c6
alb_security_group egress 규칙 설정
yunhoch0i Jul 8, 2025
b3b12f1
폴더 구조 개편
yunhoch0i Jul 8, 2025
86d76be
Update main.tf
yunhoch0i Jul 12, 2025
955eec6
Update cd.yml
yunhoch0i Jul 12, 2025
5188a24
Merge branch 'main' into feat/#10
yunhoch0i Jul 12, 2025
8ac1608
backend key 값 변경
yunhoch0i Jul 14, 2025
41c6234
OIDC 코드 추가, ci/cd 코드 수정
rnjsdbwlsqwer Jul 9, 2025
80c54c0
ci 중복제거 수정
rnjsdbwlsqwer Jul 9, 2025
8e506be
포맷 체크
rnjsdbwlsqwer Jul 9, 2025
6c48f96
iam 정책 추가
rnjsdbwlsqwer Jul 9, 2025
73e5af7
포맷 체크
rnjsdbwlsqwer Jul 9, 2025
4c35fb9
OIDC 코드 수정, PR output 수정
rnjsdbwlsqwer Jul 13, 2025
a1481b8
change pr output line
imyourhopeee Jul 14, 2025
48a14b9
change pr output line_2
imyourhopeee Jul 14, 2025
4e08b5c
change pr output line_3
imyourhopeee Jul 14, 2025
f18141f
change pr output line_3
imyourhopeee Jul 14, 2025
0667ce0
change pr output line_4
imyourhopeee Jul 14, 2025
9e2d21d
모듈 폴더 이름 수정
rnjsdbwlsqwer Jul 14, 2025
a130645
ci 코드 수정
rnjsdbwlsqwer Jul 14, 2025
d42a1d1
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
3fe4c91
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
d89905a
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
62c5d78
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
62d4044
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
7df3f7d
ci 글자 깨짐 수정
rnjsdbwlsqwer Jul 14, 2025
ff63f4b
코드 구조 리팩토링
yunhoch0i Jul 5, 2025
6b49e62
terraform state를 통해 이미지 로드
yunhoch0i Jul 5, 2025
8164cd9
Init Deployment
yunhoch0i Jun 20, 2025
99039e8
구조 정리
yunhoch0i Jul 6, 2025
08bacc4
CI/CD .tf없는 폴더 실행 X
yunhoch0i Jul 6, 2025
dce807a
CI/CD Test
yunhoch0i Jul 6, 2025
d3079cf
루트 디렉토리 참조, .tf 없는 폴더는 실행 X
rnjsdbwlsqwer Jul 6, 2025
630a757
폴더 구조 개편
yunhoch0i Jul 8, 2025
fb1b8a1
Update cd.yml
yunhoch0i Jul 12, 2025
82d6e42
branch rebase
yunhoch0i Jul 16, 2025
6d6f7ff
branch rebase
yunhoch0i Jul 16, 2025
7b0bef0
delete empty file
yunhoch0i Jul 16, 2025
7ca81ba
solve conflicts
yunhoch0i Jul 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 65 additions & 11 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ jobs:
- 'dev-team-account/**'
stage:
- 'stage-team-account/**'
security:
- 'security-team-account/**'
management:
- 'management-team-account/**'



- name: Build Matrix from Filter (with subdirs)
Expand All @@ -51,18 +56,66 @@ jobs:
["prod"]="ROLE_ARN_PROD"
["dev"]="ROLE_ARN_DEV"
["stage"]="ROLE_ARN_STAGE"
["security"]="ROLE_ARN_SECURITY"
["management"]="ROLE_ARN_MANAGEMENT"

)

MATRIX_ITEMS=()
declare -A DEPENDENCY_MAP=(
["prod-team-account/vpc"]=""
["prod-team-account/iam"]=""
["prod-team-account/acm"]=""
["operation-team-account/ecr"]="prod-team-account/deploy/iam"
["prod-team-account/alb"]="prod-team-account/deploy/vpc prod-team-account/deploy/acm"
["prod-team-account/ecs"]="prod-team-account/deploy/vpc prod-team-account/deploy/iam prod-team-account/deploy/alb operation-team-account/deploy/ecr"
["prod-team-account/codedeploy"]="prod-team-account/deploy/ecs"
)

# 변경된 경로에 따라 matrix 구성
for KEY in "${!ROLE_MAP[@]}"; do
VAR_NAME="FILTER_OUTPUTS_${KEY}"
VALUE="${!VAR_NAME}"

if [ "$VALUE" = "true" ]; then
BASE_DIR="${KEY}-team-account"
# Push 이벤트에 포함된 변경된 파일 목록을 호출
echo "Comparing changes between ${{ github.event.before }} and ${{ github.event.after }}"
CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }})

# 변경된 파일이 속한 서비스 폴더(backend.tf가 있는 폴더) 목록 검색
CHANGED_DIRS=()
for file in $CHANGED_FILES; do
dir=$(dirname "$file")
while [ "$dir" != "." ]; do
if [ -f "$dir/backend.tf" ]; then
CHANGED_DIRS+=("$dir"); break;
fi;
dir=$(dirname "$dir");
done
done
CHANGED_DIRS=($(echo "${CHANGED_DIRS[@]}" | tr ' ' '\n' | sort -u))

if [ ${#CHANGED_DIRS[@]} -eq 0 ]; then
echo "No terraform project directories with changes found."; echo "matrix=[]" >> $GITHUB_OUTPUT; exit 0;
fi
echo "Changed project directories: ${CHANGED_DIRS[@]}"

# 변경된 폴더와 정의된 의존성을 기반으로 배포 순서를 결정
TSORT_INPUT=""
ALL_DIRS_TO_CONSIDER="${CHANGED_DIRS[@]}"
for DIR in "${CHANGED_DIRS[@]}"; do
dependencies=${DEPENDENCY_MAP[$DIR]}
for DEP in $dependencies; do
TSORT_INPUT+="$DEP $DIR\n"; ALL_DIRS_TO_CONSIDER+=" $DEP";
done
done
ALL_DIRS_TO_CONSIDER=($(echo "$ALL_DIRS_TO_CONSIDER" | tr ' ' '\n' | sort -u))

ORDERED_DIRS=$(echo -e "$TSORT_INPUT" | tsort 2>/dev/null || echo "$ALL_DIRS_TO_CONSIDER")
echo "Calculated execution order: $ORDERED_DIRS"

# 실행할 최종 매트릭스를 JSON 형식으로 생성
MATRIX_ITEMS=()

for DIR in $ORDERED_DIRS; do
if [[ " ${CHANGED_DIRS[@]} " =~ " ${DIR} " ]]; then
ACCOUNT_PREFIX=$(echo $DIR | cut -d- -f1)
ROLE_KEY="${ROLE_MAP[$ACCOUNT_PREFIX]}"
MATRIX_ITEMS+=("{\"dir\":\"$DIR\",\"role_key\":\"$ROLE_KEY\"}")

# 루트 디렉터리 검사
TF_COUNT_ROOT=$(find "$BASE_DIR" -maxdepth 1 -name '*.tf' | wc -l)
Expand All @@ -79,15 +132,15 @@ jobs:
fi
fi
done

fi
done

# 최종 matrix JSON 출력
if [ ${#MATRIX_ITEMS[@]} -eq 0 ]; then
echo "matrix=[]" >> $GITHUB_OUTPUT
else
JSON="[$(IFS=,; echo "${MATRIX_ITEMS[*]}")]"
echo "matrix=$JSON" >> $GITHUB_OUTPUT
JSON="[$(IFS=,; echo "${MATRIX_ITEMS[*]}")]"; echo "matrix=$JSON" >> $GITHUB_OUTPUT;
fi

terraform-apply:
Expand All @@ -96,9 +149,10 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: # matrix 기반 반복 실행
fail-fast: true
max-parallel: 1
matrix:
include: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
fail-fast: false # 하나 실패해도 나머지 job은 계속 진행

steps:
- name: Checkout repository
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Application-Deployment CI
name: Application-DeploymentCI


on:
pull_request:
Expand Down Expand Up @@ -66,6 +67,7 @@ jobs:
echo "$DIR|$ROLE_KEY" >> $TMP_FILE
fi
fi

fi
done

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
*.tfstate.backup.*
*.tfstate.backup.json
*.tfstate.backup.json.*
.terraform.lock.hcl
.terraform.lock.hcl
9 changes: 9 additions & 0 deletions operation-team-account/deploy/ecr/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "cloudfence-operation-state"
key = "deploy/ecr.tfstate"
region = "ap-northeast-2"
dynamodb_table = "s3-operation-lock"
encrypt = true
}
}
62 changes: 62 additions & 0 deletions operation-team-account/deploy/ecr/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}

}

provider "aws" {
region = "ap-northeast-2"
}

data "terraform_remote_state" "iam" {
backend = "s3"
config = {
bucket = "cloudfence-prod-state"
key = "deploy/iam.tfstate"
region = "ap-northeast-2"
}
}

# operation-team-account의 ECR 리포지토리 생성 및 정책 설정
data "aws_iam_policy_document" "ecr_repo_policy_document" {
statement {
sid = "AllowCrossAccountPush"
effect = "Allow"
principals {
type = "AWS"
# prod 계정의 역할 ARN은 변수로 전달
identifiers = [data.terraform_remote_state.iam.outputs.github_actions_role_arn]
}
actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken"
]
}
}


# ECR 리포지토리 생성
resource "aws_ecr_repository" "app_ecr_repo" {
name = var.project_name
image_tag_mutability = "IMMUTABLE"

image_scanning_configuration {
scan_on_push = true
}
}

# 정책을 리포지토리에 연결
resource "aws_ecr_repository_policy" "app_ecr_repo_policy" {
repository = aws_ecr_repository.app_ecr_repo.name
policy = data.aws_iam_policy_document.ecr_repo_policy_document.json
}
4 changes: 4 additions & 0 deletions operation-team-account/deploy/ecr/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "repository_url" {
description = "The URL of the ECR repository"
value = aws_ecr_repository.app_ecr_repo.repository_url
}
5 changes: 5 additions & 0 deletions operation-team-account/deploy/ecr/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "project_name" {
description = "The name of the project"
type = string
default = "cloudfence"
}
9 changes: 9 additions & 0 deletions prod-team-account/deploy/acm/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "cloudfence-prod-state"
key = "deploy/acm.tfstate"
region = "ap-northeast-2"
dynamodb_table = "s3-prod-lock"
encrypt = true
}
}
44 changes: 44 additions & 0 deletions prod-team-account/deploy/acm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
}

provider "aws" {
region = "ap-northeast-2"
}

# ACM 인증서 요청
resource "aws_acm_certificate" "cert" {
domain_name = var.domain_name
subject_alternative_names = ["*.${var.domain_name}"]
validation_method = "DNS"

lifecycle {
create_before_destroy = true
}
}

# DNS 검증을 위한 Route 53 레코드 생성
resource "aws_route53_record" "cert_validation" {
for_each = {
for dvo in aws_acm_certificate.cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}

allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = var.zone_id
}

# DNS 검증이 완료될 때까지 대기하고 인증서 발급 완료
resource "aws_acm_certificate_validation" "cert" {
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
4 changes: 4 additions & 0 deletions prod-team-account/deploy/acm/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "certificate_arn" {
description = "The ARN of the validated ACM certificate"
value = aws_acm_certificate_validation.cert.certificate_arn
}
11 changes: 11 additions & 0 deletions prod-team-account/deploy/acm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "domain_name" {
description = "The domain name for the SSL certificate"
type = string
default = "cloudfence.cloud"
}

variable "zone_id" {
description = "The Route 53 Hosted Zone ID for the domain"
type = string
default = "Z0324594CRM7IYDEWX83"
}
9 changes: 9 additions & 0 deletions prod-team-account/deploy/alb/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "cloudfence-prod-state"
key = "deploy/alb.tfstate"
region = "ap-northeast-2"
dynamodb_table = "s3-prod-lock"
encrypt = true
}
}
Loading