AWS

사이드프로젝트 서버 배포 - Github Action + CodeDeploy + EC2 + RDS

sendkite 2024. 8. 4. 01:53

CI/CD

사이드프로젝트 배포할때 쓰려고 기록

코드 예제 : https://github.com/sendkite/githubAction

 

1. EC2 설정하기

  • 2024.7 우분투 LTS 기준
1. EC2 생성
2. tag 입력 - CodeDeploy-Element
3. Inbound rule 설정 > 80, 8080, 443 
4. 정적인 IP 설정 
    1. Elastic IP 생성 (Allocate)
    2. Elastic IP - EC2 매핑 (Associate)
5. ssh로 EC2 접속 
6. apt 업데이트 sudo apt update && sudo apt upgrade)
7. 자바 설치
(참고 : <https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/generic-linux-install.html>)
	- wget -O - <https://apt.corretto.aws/corretto.key> | sudo gpg --dearmor -o /usr/share/keyrings/corretto-keyring.gpg && echo "deb [signed-by=/usr/share/keyrings/corretto-keyring.gpg] <https://apt.corretto.aws> stable main" | sudo tee /etc/apt/sources.list.d/corretto.list
	- sudo apt-get update; 
	- sudo apt-get install -y java-17-amazon-corretto-jdk
	- java --version
	- cd /home/ubuntu
8. Codedeploy agent 설치 (참고 - <https://docs.aws.amazon.com/ko_kr/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html>)
	- sudo apt install ruby-full
	- sudo apt install wget
	- wget <https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install>
	- chmod +x ./install
	- sudo ./install auto
	- systemctl status codedeploy-agent
	

9. EC2 권한 설정
    1. IAM
    2. Create Role (AWS Service, EC2)
    3. CodeDeployFullAccess
    4. S3FullAccess
    
10. EC2에 IAM롤 적용

2. RDS 생성

1. Create RDS
2. Easy create > mysql > free tier

 

3. S3 버킷 생성

  • S3 bucket 생성
  • IAM 생성(S3FullAccess, CodeDeployFullAccess)
  • accessKey 생성

 

4. Code Deploy 설정

  • IAM Role 생성

  • Code Deploy > Application > 생성 (EC2/On-premisses)
  • Deployment groups 생성
    • Role 매핑
    • tag로 EC2 매핑

 

5. Github Action 설정

  • github secret에 s3 접근 key 설정 (Github repo > setting > secret > actions 변수 생성)
  • .github/workflows/deploy.yaml 파일에 배포 스크립트 작성
    • build script
    • aws cli러 s3 업로드
    • code deploy 실행 script
  • EC2 접속
    • code deploy agent 재시작
      • sudo service codedeploy-agent restart
    • log 보면서 git action TEST
      • tail -F /var/log/aws/codedeploy-agent/codedeploy-agent.log

6. appsepc.yml 작성

  • 권한 설정
  • hook 작성
    • shell script
      • JAR 복사 및 실행
반응형