[AWS] 6. CI/CD 4

문정준's avatar
Jun 19, 2025
[AWS] 6. CI/CD 4

CI/CD

  • Github을 사용한 지속적 통합 (CI, Continuous Integration)
    • 코드가 한 곳으로 모이고 통합되기 때문에 충돌 방지
  • 지속적 서비스 제공 또는 배포 (CD, Continuous Delivery / Deployment)
    • Polling 기법 또는 Webhook 기법
 

Polling

  • 서버를 하나 만든 후 Github에 일정 주기마다 request를 보냄
  • Github에서 받은 정보 중 코드의 변화(commit)를 감지하면 코드를 받음
  • 코드를 테스트한 후 성공 시 빌드
  • 빌드 성공 시 AWS로 코드 push
  • 대표적인 도구가 travis
notion image

Webhook

  • Github 측에서 서버와 Connection을 연결
  • 코드 변경이 생길 경우 Github에서 서버에게 hook을 날림
  • 서버는 hook을 받으면 코드를 받음
  • 이후는 Polling과 동일
    • Polling은 서버 측에서 계속 요청을 보내는 방식이나, Webhook은 서버에게 변경 시 hook을 보내는 방법
    • Webhook을 사용하면 자원 소모량이 감소 (계속 Request를 보내지 않아도 됨 + 타이밍을 똑같이 파악할 수 있음)
  • 대표적인 도구가 Jenkins
  • 본 실습에서는 Github Action을 사용
notion image
 
 

Elastic Beanstalk + RDS

  • RDS 생성 : 이전과 동일
 

Elastic Beanstalk 생성

1단계

  • 사전 설정 - 사용자 지정 구성 선택
notion image
 

3단계

  • VPC 선택
  • 인스턴스 전부 선택
    • 퍼블릭 IP 주소 활성화
notion image
 
  • 데이터베이스 서브넷 전부 활성화
notion image
 
  • 용량
    • 밸런싱된 로드
    • 인스턴스 최소 2, 최대 4 설정
      • 일반 설정일 때 2개의 인스턴스 생성, 서버 부하 시 최대 4개까지 증가
notion image
 
  • 로드 밸런서 : 퍼블릭
    • 내부망 설정 시 Beanstalk을 외부에서 감지할 수 없음
notion image
 
  • 로드 밸런서 유형 : 애플리케이션 로드 밸런서
    • 내부적으로 보안 검사 등의 로직 처리
notion image
 
  • 리스너, 프로세스 : 일반 설정
    • 리스너, 프로세스 생성 시 포트를 변경해서 서버가 원활하게 작동하는 지 확인 가능
notion image
 
  • 프로세스 추가 시
notion image
  • 롤링 업데이트 : 변경 불가 (블루/그린 배포)
    • 무중단 배포 가능
notion image
 
  • 환경 속성 추가
    • 이전 예제 참고
notion image
 

생성된 환경 확인

로드 밸런서

  • 가용 영역 : 내가 설정했던 인스턴스 서브넷
  • 리스너 : 80 포트를 검사 중 (/ 주소로 들어올 경우)
    • 타겟 : 대상 그룹 (EC2가 묶여 있는 그룹, 보안 그룹)
notion image
 

EC2 & RDS 인바운드 규칙 편집

  • EC2 인스턴스 보안 그룹 확인
    • 인스턴스가 여러 개여도 보안 그룹은 동일함
notion image
 

rds-group 규칙 수정

  • aws-v4에 연결되어 있던 규칙을 aws-v5 보안 규칙으로 수정해야 함
notion image
notion image
 

Webhook 설정

1. Git Clone

  • 예제인 Aws-v5 리포지토리를 git clone → 기존 프로젝트 외 새로 생긴 폴더 확인
notion image
 

2. Github Action 파일 확인

  • deploy.yml
    • name : 프로젝트 이름
    • on : CD를 하는 Github Action → master 브랜치를 Listening
    • jobs : 동작 임무
      • build : 빌드 시 하는 동작들
        • runs-on : 배포할 환경 → ubuntu (버전 확인)
        • steps : 절차
          • name : Task 이름
          • uses : Task에 사용할 다른 Github Action 또는 플러그인
          • with : uses에 전달할 설정
          • run : Task에서 실행하는 셀 명령어
name: aws-v5 on: push: branches: - master # https://github.com/actions/setup-java jobs: build: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: 21 distribution: temurin # 캐싱을 통해 gradle의 중복 생성을 피하고 이로 인한 충돌 방지, 빌드 시간 단축 cache: gradle - name: Pemission run: chmod +x ./gradlew - name: Build with Gradle run: ./gradlew clean build # UTC가 기준이기 때문에 한국시간으로 맞추려면 +9시간 해야 한다 - name: Get current time uses: 1466587594/get-current-time@v2 id: current-time with: format: YYYY-MM-DDTHH-mm-ss utcOffset: "+09:00" - name: Show Current Time run: echo "CurrentTime=${{steps.current-time.outputs.formattedTime}}" # EB에 CD 하기 위해 추가 작성 - name: Generate deployment package run: | mkdir deploy cp build/libs/*.jar deploy/application.jar cp Procfile deploy/Procfile cp -r .ebextensions deploy/.ebextensions cd deploy && zip -r deploy.zip . - name: Deploy to EB uses: einaregilsson/beanstalk-deploy@v22 with: aws_access_key: ${{ secrets.AWS_ACCESS_KEY }} aws_secret_key: ${{ secrets.AWS_SECRET_KEY }} application_name: aws-v5 # 엘리스틱 빈스톡 애플리케이션 이름! environment_name: Aws-v5-env # 엘리스틱 빈스톡 환경 이름! version_label: aws-v5-${{steps.current-time.outputs.formattedTime}} region: ap-northeast-2 deployment_package: deploy/deploy.zip
 
notion image
 

Github Actions 실행

  • 리포지토리에서 Action 클릭
notion image
 
  • Enable 클릭
notion image
 
  • 커밋 시 deploy를 자동 인식하여 코드 수행
notion image
 
notion image
notion image
 
  • 각 Task 내부에서 수행한 로그 확인 가능
notion image
 

AWS 보안 키 등록

  • 현재 Github Action에서 AWS에게 접근하게 되면, AWS 입장에서 Github 서버는 Anonymous
    • 신뢰할 수 없음 : 접근 차단
  • 서버가 자동으로 AWS에 접근할 수 있도록 유저의 권한을 옮겨줘야 함 : 개인 키, 공개 키 전달
 
notion image
 
notion image
 
  • 키 생성 : AWS 사용자 생성 필요 → AWS 사용자의 권한을 서버에게 부여
    • IAM 진입
    • 사용자 생성 클릭
notion image
 
notion image
 
notion image
 
notion image
 
notion image
 
notion image
 
notion image
 

무중단 배포 확인

  • Github Action
    • 블루/그린 배포 정책으로 인해 무중단 배포가 수행 중임을 확인 가능
notion image
 
 
  • 기존 동작 중인 서버
notion image
notion image
 

배포 후

  • ELB 환경 재 설정
notion image
 
  • EC2 새 환경 실행 중
notion image
notion image
 
  • Health Check 성공
notion image
 
  • 이전에 실행되던 서버(인스턴스)는 종료
notion image
Share article

sxias