๐Ÿ”ฅ TIL - Day 26

Kim Dae Hyunยท2021๋…„ 10์›” 12์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
30/93

๐Ÿ“Œ ElasticBeanstalk์— ๋ฐฐํฌ

EB ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์•„์ง ์ž์„ธํ•œ ์„ค์ •์€ ์ž˜ ๋ชฐ๋ผ์„œ ์Šคํ‚ตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋ ค์šด ๊ฒƒ์ด ์—†์—ˆ๋‹ค.

Github Action ์„ ์ด์šฉํ•ด์„œ CI/CD ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌ์„ฑํ•˜์—ฌ ๋ฐฐํฌ๋ฅผ ํ–ˆ๋‹ค. ์‚ฌ์‹ค ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ๋Š” ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™”๋‹ค..

[ .github/workflows/main.yml ]

name: backend
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout source code.
        uses: actions/checkout@master

      - name: Set up Python 3.8
        uses: actions/setup-python@v1
        with:
          python-version: "3.8"

      - name: Generate deployment package
        run: zip -r deploy.zip . -x '*.git*'

      - name: Get timestamp
        uses: gerred/actions/current-time@master
        id: current-time

      - name: Run string replace
        uses: frabert/replace-string-action@master
        id: format-time
        with:
          pattern: '[:\.]+'
          string: "${{ steps.current-time.outputs.time }}"
          replace-with: '-'
          flags: 'g'

      - name: Deploy to EB
        uses: einaregilsson/beanstalk-deploy@v16
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          application_name: python-app
          environment_name: Pythonapp-env
          version_label: "python-${{ steps.format-time.outputs.replaced }}"
          region: ap-northeast-2
          deployment_package: deploy.zip

Github์˜ Repository์—์„œ Secrets๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๊ธฐ๋ณธ์ ์ธ ๋ฐฐํฌ ์ค€๋น„๋Š” ๋๋‚œ๋‹ค.

๋‹ค์Œ์œผ๋กœ Flask์˜ ๊ธฐ๋ณธ ์—”ํŠธ๋ฆฌํฌ์ธํŠธ์ธ app.py์˜ ์ด๋ฆ„์„ application.py๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ๋ƒฅ ๋ฃฐ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๋ฐฐํฌ๋˜๋Š” ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ๋  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

pip freeze > requirements.txt

๊ธฐ๋ณธ์ ์ธ ์„ค์ •์€ ๋๋‚ฌ๋‹ค. ์ด์ œ ๋ฐฐํฌ๋˜๋Š” ํ™˜๊ฒฝ์— ๋งž๊ฒŒ url ๋“ฑ ๋””ํ…Œ์ผํ•œ ๋ถ€๋ถ„์„ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.


์ฒซ๋ฒˆ์งธ๋กœ DB(mongoDB)์— ๋Œ€ํ•œ ์ •๋ณด์ด๋‹ค. DB์— ์ ‘์†ํ•˜๋Š” ์ ‘์†์ •๋ณด๋ฅผ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ๊ณต๊ฐœ๋˜๋ฉด ์•ˆ ๋˜๋Š” ์ •๋ณด์ด๋ฏ€๋กœ EB์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•ด์ค€๋‹ค.

import os
from pymongo import MongoClient

MONGO_URL = os.environ['MONGO_URL']
MONGO_USERNAME = os.environ['MONGO_USERNAME']
MONGO_PASSWORD = os.environ['MONGO_PASSWORD']
client = MongoClient(MONGO_URL, 27017, username=MONGO_USERNAME, password=MONGO_PASSWORD)

์‚ฌ์‹ค ์ด ๋ถ€๋ถ„์—์„œ ์ •๋ง ์ด์ƒํ•œ ์‹ค์ˆ˜๋ฅผ ํ•ด์„œ ๋ช‡ ์‹œ๊ฐ„์„ ๋ฐฐํฌ ํ…Œ์ŠคํŠธ๋งŒ ํ–ˆ๋‹ค..
๊ธฐ์กด์— ์šฐ๋ฆฌ๋Š” python-decouple ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ privateํ•œ ๊ฐ’๋“ค์„ ๊ด€๋ฆฌํ–ˆ๋‹ค. ์—ฌ๊ธฐ์„œ privateํ•œ ๊ฐ’๋“ค์ด๋ž€ github์— ๊ณต๊ฐœํ•˜์ง€ ์•Š์„ ๊ฐ’๋“ค์„ ๋งํ•œ๋‹ค. ๋ฐฐํฌ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋ฉด์„œ ์ž๊พธ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ ์ด ๋ถ€๋ถ„์„ ์ƒ๊ฐ๋„ ๋ชปํ–ˆ๋‹ค.. ๋‹น์—ฐํ•œ๊ฒŒ ์•„๋‹Œ๊ฐ€... Github๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐํฌ๋ฅผ ํ•˜๋Š”๋ฐ ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ privateํ•œ ๊ฐ’๋“ค์„ Github์— ์•ˆ ์˜ฌ๋ฆฐ๋‹ค? .. ? ใ…‹ใ…‹...ใ…‹ใ…‹..ใ…‹ใ…‹

ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•˜์ž...

EB์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ํ™˜๊ฒฝ์—์„œ [๊ตฌ์„ฑ - ์†Œํ”„ํŠธ์›จ์–ด] ์—์„œ ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๋ฉด ํ™˜๊ฒฝ์†์„ฑ ์ด๋ผ๋Š” ๊ณณ์— ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

์ผ๋‹จ ๋ฐฐํฌ๋Š” ํ•ด๊ฒฐ๋๋‹ค. ์ด์ œ ๋ชจ๋“  ๊ธฐ๋Šฅ๋“ค์ด ๋ฐฐํฌํ™˜๊ฒฝ์—์„œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š” ์ง€๋ฅผ ํ™•์ธํ•ด์•ผํ•œ๋‹ค.


๐Ÿ“Œ Scheduler๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค...

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ 3๊ฐœ์˜ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉ ์ค‘์ด๋‹ค.. ๋กœ์ปฌ์—์„œ๋Š” ๋ชจ๋‘ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์ง€๋งŒ ๋ฐฐํฌํ™˜๊ฒฝ์—์„œ ๋™์ž‘ํ•˜์งˆ ์•Š๋Š”๋‹ค.. ํฐ์ผ์ด๋‹ค..

์ผ๋‹จ ์ฐพ์•„๋ณธ ์ฒซ๋ฒˆ์งธ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ cron.yaml ์„ค์ •ํŒŒ์ผ์„ ํ†ตํ•ด ์Šค์ผ€์ค„๋ง์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. flask์˜ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

version: 1
cron:
  - name: "category-data-init"
    url: "/api/category/data-initialize"
    schedule: "0 3 * * *"

๋งค์ผ ์˜ค์ „ 3์‹œ์— url๋กœ post ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์„ค์ •์ด๋‹ค. ๊ณต์‹๋ฌธ์„œ๋ฅผ ๊ฑฐ์˜ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค.

bp = Blueprint("category_data_scheduler", __name__, url_prefix='/')

@bp.route('/api/category/data-initialize')
def set_category_data():
	...

์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ํ˜ธ์ถœ์ด ์•ˆ๋˜๋Š” ๊ฒƒ์€ ๋‘˜์งธ์น˜๊ณ  ์• ์ดˆ์— ๋“ฑ๋ก ์ž์ฒด๊ฐ€ ์•ˆ ๋œ๋‹ค.. ๋ˆˆ๋ฌผ๋‚œ๋‹ค.. ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค..!

ํŠœํ„ฐ๋‹˜๊ป˜ ์งˆ๋ฌธ๋“œ๋ฆฐ ๊ฒฐ๊ณผ ๋ณดํ†ต์˜ ๊ฒฝ์šฐ EB์—์„œ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๋™์ž‘์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. ๋ณดํ†ต AWS์˜ Lambda๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ EC2์— ์Šค์ผ€์ค„๋Ÿฌ ๋ถ€๋ถ„๋งŒ ๋”ฐ๋กœ ์˜ฌ๋ ค์„œ ๊ธฐ๋™์‹œํ‚ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค. Lambda๋ฅผ ์จ๋ณด๊ณ  ์‹ถ์€ ์š•์‹ฌ์ด ์ƒ๊ธฐ์ง€๋งŒ ์‹œ๊ฐ„์ด ๋งŽ์ด ์—†์œผ๋‹ˆ ์ผ๋‹จ ์™„์„ฑ์„ ๋ชฉํ‘œ๋กœ ์Šค์ผ€์ค„๋Ÿฌ ๋ถ€๋ถ„๋งŒ ๋”ฐ๋กœ ๋—ด์–ด EC2์— ์˜ฌ๋ ค์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ค.

cron.yaml ์ € ๋  ๊ฒƒ ๊ฐ™์€ ๋ฐฉ๋ฒ•์€ ๋Œ€์ฒด ๋ฌด์—‡์ธ๊ฐ€ ....



์•”ํŠผ ์ž๋™ ๋ฐฐํฌ ์™„๋ฃŒ^^
Today ๊ตญํšŒ

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

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