[7주차] Jenkins 환경에서의 AWS ELB, EC2 배포 자동화 (Next SSR)

[월간-CS][24년 4월] React, Next 배포와 배포 자동화 A부터 Z
이민석's avatar
May 26, 2024
[7주차] Jenkins 환경에서의 AWS ELB, EC2 배포 자동화 (Next SSR)

이 문서는 [월간-CS][24년 4월] React, Next 배포와 배포 자동화 A부터 Z를 위해서 작성된 문서입니다. 이 문서에는 그 어떤 저작권이 없으며, 편하게 참고 및 사용하셔도 됩니다.

개요

이번 주차는 2주차에서 진행했던 과제를 Jenkins위에서 진행하는 방식입니다.

인프라 배포

인프라 배포는 아래 두 문서 중의 하나를 선택해서 진행해주세요.

특히 Console 문서를 통해서 진행하는 경우에는 #실습 진행 ~ #컴퓨팅 리소스 고도화하기 : 배포 자동화까지 진행해주세요.

Jenkins 환경 구성하기

Jenkins 환경 구성을 배포하기 위해서 아래 문서를 따라해주세요.

해당 문서에서 #Jenknis 실행을 위한 EC2 준비하기 ~ # Jenkins 파이프라인 구성하기까지 진행해주세요.

CodeDeploy 용 코드 넣기

  • ~/scripts/after-install.sh

    DIRECTORY=/home/ubuntu/project
    
    cd $DIRECTORY
    
    sudo npm i pm2 -g
    sudo npm i
    sudo npm run start

  • ~/appspec.yml

    version: 0.0
    os: linux
    files:
      - source: /
        destination: /home/ubuntu/project
        overwrite: yes
    
    file_exists_behavior: OVERWRITE
    
    permissions:
      - object: /home/ubuntu
        pattern: '**'
        owner: ubuntu
        group: ubuntu
    
    hooks:
      AfterInstall:
        - location: scripts/after-install.sh
          timeout: 300
          runas: ubuntu

Jenkins 파이프라인 최종 설정하기

  1. AWS EC2 Instance Connect에서 aws-cli 설치하기

    sudo apt install unzip
    
    sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    
    sudo unzip awscliv2.zip
    
    sudo ./aws/install
  2. AWS EC2 Instance Connect에서 사용자 변경하기

    sudo -s
    su jenkins
  3. AWS EC2 Instance Connect에서 aws profile 설정하기

    aws configure --profile monthly-cs
  4. [Dashboard] - [2024-04-cicd-week-6-template] - [구성] - [Pipeline]을 아래로 교체하기

    1. 주요 설정값

    2. 예시

      pipeline {
          agent any
          tools {
              nodejs "node20"
              git "git"
          }
          stages {
              stage('prepare') {
                  steps {
                      echo 'prepare'
                       git branch: "${BRANCH}", credentialsId: "unchaptered", url: 'https://github.com/monthly-cs/2024-04-cicd-week-2-template'
                       sh  'ls -al'
                  }
              }
              stage('build') {
                  steps {
                          dir('./'){
                              sh 'ls -al'
                              sh "yarn install"
                              sh "CI=false yarn build"
                      }
                  }
              }
              stage('deploy') {
                  steps {
                      zip -r app.zip ./
      
                      aws s3 cp \
                        --region ap-northeast-2 \
                        ./app.zip \
                        s3://<S3_BUCKET_NAME>/app.zip
      
                      aws deploy create-deployment \
                       --application-name <CODE_DEPLOY_APP>	 \
                       --deployment-group-name <CODE_DEPLOY_GROUP_NAME> \ 
                       --s3-location bucket=<S3_BUCKET_NAME>,bundleType=zip,key=app.zip
                  }
              }
          }
      }
      

리소스 정리

7주간의 스터디가 완료되었습니다.

북부 버지니아(us-east-1) 리전 기준으로 모든 리소스를 삭제해주세요. 찾아야 하는 리소스의 리스트는 아래와 같습니다.

  • S3

  • CloudFront

  • ACM(AWS Certificate Manager)

  • ELB(Elastic Load Balacner)

  • EC2 Instance

  • EC2 Snapshot

  • EC2 AMI (Amazon Machine Image)

  • EC2 EBS(Elastic Block Storage)

  • EC2 Security Group

  • VPC

  • VPC Subnet

  • VPC IGW(Internet Gateway)

  • VPC NAT(Network Address Translation)

  • VPC RTB(Routing Table)

  • IAM User (🚩🚩🚩, 반드시 제거할 것)


 

Share article
RSSPowered by inblog