AWS #2 Docker + AWS ECS 파이프라인 구성

AWS #2 Docker + AWS ECS 파이프라인 구성 #

#2026-02-07


#1 배포 파일 생성

StudyNote/
├── backend/Dockerfile           # 백엔드 컨테이너
├── frontend/Dockerfile          # 프론트엔드 멀티스테이지 빌드
├── frontend/nginx.conf          # Nginx 설정
├── docker-compose.yml           # 로컬 Docker 실행
├── .github/workflows/deploy.yml # CI/CD 파이프라인
├── .aws/
│   ├── task-definition-backend.json
│   └── task-definition-frontend.json
├── terraform/main.tf            # AWS 인프라 (VPC, ECS, ALB, ECR)
└── README.md                    # 배포 가이드

먼저 필요한 파일들을 위 구조로 생성해줬다.

#

#2 AWS key 추가

다음으로 GitHub Secrets에 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY를 추가한다.

key는 AWS Console에서 발급받는데 AWS Console 로그인 -> 계정 클릭 -> 보안 자격 증명 -> Access keys -> Create access key 해준 뒤에 Access Key ID와 Secret Access Key를 복사한다.

그리고 Github secrets를 Settings → Secrets and variables → Actions -> New repository secret -> AWS_ACCESS_KEY_ID로 복사한 키, AWS_SECRET_ACCESS_KEY로 복사한 시크릿을 넣어준다.

#

#3 Terraform으로 인프라 생성

terraform apply로 AWS 인프라를 생성한다.

cd /Users/yshmbid/Documents/home/github/StudyNote/terraform

# 1. init
brew install terraform
terraform init

# 2. plan
brew install awscli
aws configure
#입력:
#AWS Access Key ID: (GitHub에 넣은 그 키)
#AWS Secret Access Key: (GitHub에 넣은 그 시크릿)
#Default region name: ap-northeast-2
#Default output format: json

terraform plan

# 3. apply
terraform apply

#

#4 Docker 이미지를 ECR에 푸시

# ECR 로그인
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com

# 백엔드 빌드 & 푸시
cd /Users/yshmbid/Documents/home/github/StudyNote/backend
docker build -t studynote-backend .
docker tag studynote-backend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest

# 프론트엔드 빌드 & 푸시
cd /Users/yshmbid/Documents/home/github/StudyNote/frontend
docker build -t studynote-frontend .
docker tag studynote-frontend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest

푸시 후 ECS 서비스를 재시작한다.

aws ecs update-service --cluster studynote-cluster --service studynote-backend-service --force-new-deployment
aws ecs update-service --cluster studynote-cluster --service studynote-frontend-service --force-new-deployment

그 다음 url로 접속한다: http://studynote-alb-123356897.ap-northeast-2.elb.amazonaws.com

#

#5 트러블슈팅

안내받은대로 했는데 503 Service Temporarily Unavailable이 떴다.

Mac은 ARM인데 AWS ECS는 x86이라서 ARM/x86 아키텍처 문제였고 amd64 플랫폼 지정해서 다시 빌드해줬다.

# 백엔드
cd /Users/yshmbid/Documents/home/github/StudyNote/backend
docker build --platform linux/amd64 -t studynote-backend .
docker tag studynote-backend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest

# 프론트엔드
cd /Users/yshmbid/Documents/home/github/StudyNote/frontend
docker build --platform linux/amd64 -t studynote-frontend .
docker tag studynote-frontend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest

# 서비스 재시작
aws ecs update-service --cluster studynote-cluster --service studynote-backend-service --force-new-deployment
aws ecs update-service --cluster studynote-cluster --service studynote-frontend-service --force-new-deployment

#

#cf 클로드 참고자료

terraform apply 하면 AWS에 VPC, ECS 클러스터, ALB, ECR 등이 생성됨. 완료 후 ALB DNS 주소가 출력되고 그게 접속 URL

참고로 처음 apply 전에 ECR에 이미지가 없어서 ECS 서비스가 실패할 수 있음 그럴 경우:

# 먼저 이미지 수동 푸시
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com

cd backend
docker build -t studynote-backend .
docker tag studynote-backend:latest <ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest
docker push <ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest

cd ../frontend
docker build -t studynote-frontend .
docker tag studynote-frontend:latest <ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest
docker push <ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest

<ACCOUNT_ID>는 AWS 계정 ID (12자리 숫자).

#

#cf2 트러블슈팅후 바뀐 코드

백엔드를 backend/ 안에서 빌드하면 docs/, _index.md, data/가 빌드 컨텍스트에 포함되지 않아서 실패한다.

프로젝트 루트(StudyNote/)에서 -f backend/Dockerfile .으로 빌드해야함.

# 백엔드 (프로젝트 루트에서 빌드)
cd /Users/yshmbid/Documents/home/github/StudyNote
docker build --platform linux/amd64 -t studynote-backend -f backend/Dockerfile .
docker tag studynote-backend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-backend:latest

# 프론트엔드 (그대로)
cd /Users/yshmbid/Documents/home/github/StudyNote/frontend
docker build --platform linux/amd64 -t studynote-frontend .
docker tag studynote-frontend:latest 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest
docker push 949346723599.dkr.ecr.ap-northeast-2.amazonaws.com/studynote-frontend:latest

# 서비스 재시작
aws ecs update-service --cluster studynote-cluster --service studynote-backend-service --force-new-deployment
aws ecs update-service --cluster studynote-cluster --service studynote-frontend-service --force-new-deployment

#