[TIL] Day73 #AWS #CodePipeline

Beanxxยท2022๋…„ 8์›” 9์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
73/120
post-thumbnail

2022.08.09(Tues)

[TIL] Day73
[SEB FE] Day74

โ˜‘๏ธย [Pair] Server ๋ฐฐํฌ ์ž๋™ํ™” ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์ถ• feat. AWS

  • AWS ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐํฌ ์ž๋™ํ™” ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์ถ•ํ•˜๊ธฐ
    • CodePipeline์„ ์ด์šฉํ•ด์„œ ๊ฐ ๋‹จ๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์ถ•ํ•˜๊ธฐ
    • Source ๋‹จ๊ณ„์—์„œ ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ์ €์žฅ๋œ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์—ฐ๊ฒฐํ•˜๊ธฐ
    • Deploy ๋‹จ๊ณ„์—์„œ CodeDeploy ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ EC2 ์ธ์Šคํ„ด์Šค์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜ํ•˜๊ธฐ
  1. ์ €๋ฒˆ์— ํ•ด๋ดค๋˜ session manager๋กœ EC2 ์—ฐ๊ฒฐํ•˜๊ธฐ

    # ํ„ฐ๋ฏธ๋„์„ bash๋กœ ๋ณ€๊ฒฝ
    $ bash
    
    # ์ตœ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™
    $ cd ~
    
    # ํŒจํ‚ค์ง€ ์ •๋ณด๋ฅผ ์ตœ์‹  ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ
    $ sudo apt update
    
    # nvm ์„ค์น˜
    $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    $ source ~/.nvm/nvm.sh
    
    # nvm ์ž˜ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ๋ฒ„์ „ ํ™•์ธํ•ด๋ณด๊ธฐ
    $ nvm โ€”version
    
    # node.js ์„ค์น˜
    $ nvm install node
    
    # npm ์„ค์น˜
    $ sudo apt install npm
  2. EC2 ์ธ์Šคํ„ด์Šค ์„ ํƒ โ†’ ์ž‘์—… โ†’ ์ธ์Šคํ„ด์Šค ์„ค์ • โ†’ ํƒœ๊ทธ ๊ด€๋ฆฌ

    • ํƒœ๊ทธ ์ถ”๊ฐ€ ์ด์œ ? ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์ถ• ๋‹จ๊ณ„์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž˜ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด
  3. ์ธ์Šคํ„ด์Šค ์„ ํƒ โ†’ ์ž‘์—… โ†’ ๋ณด์•ˆ โ†’ IAM ์—ญํ•  ์ˆ˜์ • โ†’ ์ƒˆ IAM ์—ญํ•  ์ƒ์„ฑ

    • ์—ญํ• (Role): AWS์˜ ๊ฐœ์ฒด๊ฐ€ ๋‹ค๋ฅธ ์„œ๋น„์Šค์— ์ ‘๊ทผํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•
    • EC2 ์ธ์Šคํ„ด์Šค ์—ญํ•  ๋ถ€์—ฌ โ‡’ ๋‹ค๋ฅธ AWS ์„œ๋น„์Šค ํ˜ธ์ถœ ๊ถŒํ•œ์„ ๊ฐ€์ง
  4. IAM ์—ญํ•  โ†’ ์—ญํ•  ๋งŒ๋“ค๊ธฐ

    • ๊ฐœ์ฒด - AWS ์„œ๋น„์Šค
    • ์‚ฌ์šฉ ์‚ฌ๋ก€ ์„ ํƒ - EC2 ์„ ํƒ
    • ๊ถŒํ•œ ์ •์ฑ… ์—ฐ๊ฒฐ - AmazonS3FullAccess, AmazonSSMFullAccess, AWSCodeDeployRole ์„ ํƒ
  5. ์ƒ์„ฑํ•œ IAM ์—ญํ•  ์„ ํƒ โ†’ ์‹ ๋ขฐ ๊ด€๊ณ„ ํŽธ์ง‘

    • ์‹ ๋ขฐ ๊ด€๊ณ„: ํ•ด๋‹น ์—ญํ• ์„ ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ์„œ๋น„์Šค๋‚˜ ์‚ฌ์šฉ์ž๋ฅผ ๋ช…์‹œํ•˜๋Š” ๋ถ€๋ถ„
    • Service ๊ฐ’ - ["[ec2.amazonaws.com](http://ec2.amazonaws.com/)", "[codedeploy.ap-northeast-2.amazonaws.com](http://codedeploy.ap-northeast-2.amazonaws.com/)"] ๊ฐ’ ๋ณ€๊ฒฝ
  6. EC2 - ์ธ์Šคํ„ด์Šค ์„ ํƒ - IAM ์—ญํ•  ์ˆ˜์ • - ์œ„์—์„œ ์ƒ์„ฑํ•œ ์—ญํ•  ์ ์šฉ

    • EC2๋Š” S3, CodeDeploy, SSM ์„œ๋น„์Šค์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  7. EC2 - ์ธ์Šคํ„ด์Šค ์„ ํƒ - ๋ณด์•ˆ - ๋ณด์•ˆ ๊ทธ๋ฃน - ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ํŽธ์ง‘

    • 80, 443 ํฌํŠธ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
      • 80๋ฒˆ ํฌํŠธ - ์„œ๋ฒ„ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด ํ•„์š”
      • 443๋ฒˆ ํฌํŠธ - CodeDeploy-Agent์˜ ์ •์ƒ์ ์ธ ์ž‘๋™์„ ์œ„ํ•ด ํ•„์š”
  8. git clone ๋ฐ›์€ repository ์ตœ์ƒ์œ„์— appspec.yml ํŒŒ์ผ ์ถ”๊ฐ€

    • appspec.yml: ๋ฐฐํฌ ์ž๋™ํ™”๋ฅผ ๋„์™€์ฃผ๋Š” CodeDeploy-Agent๊ฐ€ ์ธ์‹ํ•˜๋Š” ํŒŒ์ผ
    // appspec.yml
    version: 0.0
    os: linux
    files:
      - source: /
        destination: /Users/bean/Dev/codestates/Section_4/fe-sprint-practice-deploy
    
    hooks:
      # ์•ฑ์ด ๋ฉˆ์ถ”๊ณ  ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ
      ApplicationStop:
        - location: scripts/stop.sh
          runas: root
      # ์•ฑ์ด ์„ค์น˜๊ฐ€ ๋๋‚˜๊ณ  ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ
      AfterInstall:
        - location: scripts/initialize.sh
          runas: root
      # ์•ฑ์ด ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ณ  ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ
      ApplicationStart:
        - location: scripts/start.sh
          runas: root
  9. scripts ํด๋” ์ƒ์„ฑ ํ›„, initialize.sh, start.sh, stop.sh ํŒŒ์ผ ์ƒ์„ฑ

    • initialize.sh
      #!/bin/bash
      cd /Users/bean/Dev/codestates/Section_4/fe-sprint-practice-deploy/server
      npm install
      npm install pm2@latest -g
      sudo apt-get update
      sudo apt-get install authbind
      sudo touch /etc/authbind/byport/80
      sudo chown ubuntu /etc/authbind/byport/80
      sudo chmod 755 /etc/authbind/byport/80
    • start.sh
      #!/bin/bash
      
      cd /Users/bean/Dev/codestates/Section_4/fe-sprint-practice-deploy/server
      export DATABASE_USER=$(aws ssm get-parameters --region ap-northeast-2 --names DATABASE_USER --query Parameters[0].Value | sed 's/"//g')
      export DATABASE_PASSWORD=$(aws ssm get-parameters --region ap-northeast-2 --names DATABASE_PASSWORD --query Parameters[0].Value | sed 's/"//g')
      export DATABASE_PORT=$(aws ssm get-parameters --region ap-northeast-2 --names DATABASE_PORT --query Parameters[0].Value | sed 's/"//g')
      export DATABASE_HOST=$(aws ssm get-parameters --region ap-northeast-2 --names DATABASE_HOST --query Parameters[0].Value | sed 's/"//g')
      
      authbind --deep pm2 start app.js
    • stop.sh
      #!/bin/bash
      cd /Users/bean/Dev/codestates/Section_4/fe-sprint-practice-deploy/server
      pm2 stop app.js 2> /dev/null || true
      pm2 delete app.js 2> /dev/null || true
  10. CodeDeploy โ†’ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ โ†’ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ

    • ์ปดํ“จํŒ… ํ”Œ๋žซํผ - EC2/์˜จํ”„๋ ˆ๋ฏธ์Šค ์˜ต์…˜ ์„ ํƒ
  11. CodeDeploy โ†’ ์ƒ์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ โ†’ ๋ฐฐํฌ ๊ทธ๋ฃน ์ƒ์„ฑ

    • ์„œ๋น„์Šค ์—ญํ•  - ์ƒ์„ฑํ–ˆ๋˜ IAM role ์„ ํƒ
    • ํ™˜๊ฒฝ ๊ตฌ์„ฑ - Amazon EC2 ์ธ์Šคํ„ด์Šค ์„ ํƒ
    • ํƒœ๊ทธ - ํ‚ค/๊ฐ’ ์„ ํƒ
    • ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ํ™œ์„ฑํ™” ์ฒดํฌ ํ•ด์ œ
  12. CodePipeline ์ƒ์„ฑ

    • ์†Œ์Šค ์Šคํ…Œ์ด์ง€ - GitHub(version 2) ์„ ํƒ
      • Github์— ์—ฐ๊ฒฐ โ†’ ์ƒˆ ์•ฑ ์„ค์น˜ โ†’ ๋ฐฐํฌํ•  ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์—ฐ๊ฒฐ
      • ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ด๋ฆ„, ๋ธŒ๋žœ์น˜ ์ด๋ฆ„ ์ง€์ •
      • ์ถœ๋ ฅ ์•„ํ‹ฐํŒจํŠธ ํ˜•์‹ - โ€˜CodePipeline ๊ธฐ๋ณธ๊ฐ’โ€™์œผ๋กœ ์ง€์ •
    • ๋ฐฐํฌ ์Šคํ…Œ์ด์ง€
      • ๋ฐฐํฌ ๊ณต๊ธ‰์ž - AWS CodeDeploy ์„ ํƒ
      • ๋ฆฌ์ „ - ์•„์‹œ์•„ ํƒœํ‰์–‘(์„œ์šธ)
      12-2
  13. EC2 instance ํ„ฐ๋ฏธ๋„์—์„œ Log ํ™•์ธ

    13

๐Ÿ’กย Postman์œผ๋กœ ์ƒ์„ฑํ•œ EC2 ์ธ์Šคํ„ด์Šค IP์ฃผ์†Œ ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ! โ†’ โ€˜Hello Worldโ€™ ์ž˜ ๋‚˜์˜ด!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-08-09 แ„‹แ…ฉแ„’แ…ฎ 4 19 55

[ISSUE] ์œ„ ๊ณผ์ • 12๋ฒˆ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ด์Šˆ๋•Œ๋ฌธ์— ๋ฐฐํฌ ๋ชปํ•˜๋‹ค๊ฐ€ ์ƒˆ๋กœ์šด AWS ๊ณ„์ • ๋‹ค์‹œ ๋ถ€์—ฌ๋ฐ›์€ ํ›„์— ๋ฐฐํฌ๊นŒ์ง€ ์™„๋ฃŒ โœจ

issue
profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€