[Django] Elastic Beanstalk + Github Actions로 배포 자동화하기

울상냥·2023년 4월 18일
0

배포

목록 보기
17/18
post-thumbnail

EB CLI를 통한 배포과정이 귀찮을 것 같아서 Github Actions를 통해 EB에 소스 업로드를 진행하고자 했다.

Django

django.config

Elastic Beanstalk에 wsgi경로 설정을 해주어야 한다.
.ebextentions 디렉토리의 django.config 에 다음과 같은 내용을 추가한다.

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: config.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: config.settings
    "PYTHONPATH": "/var/app/current:$PYTHONPATH"
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

경로에 맞게 path를 작성해 주자.

관련 로그는 web.stdout에서 확인할 수 있다.

health check

Elastic Beanstalk 의 health check를 위한

pip install django-ebhealthcheck

을 진행해준다.
이 상태검사 때문에 아주 긴 시간 삽질을 했는데 이부분에 대해서는 따로 글을 작성했다...

settings.py

INSTALLED_APPS에 ebhealthcheck를 추가해준다.

INSTALLED_APPS = [

	    'ebhealthcheck.apps.EBHealthCheckConfig',
]

배포 서버이기에 몇가지 설정을 변경해주었다.

DEBUG = False
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

보안이 필요한 사항들은 환경변수에 등록하여 읽어오도록 해주었다.

SECRET_KEY = os.environ.get("SECRET_KEY")
KAKAO_REST_KEY = os.environ.get("KAKAO_REST_KEY")

ALLOWED_HOSTS에 서버 도메인을 추가해주었다.

ALLOWED_HOSTS = ['catmemetest.site']

requirements.txt

requirements.txt를 생성하여 EB에게 설치할 패키지를 알려줘야한다.

pip freeze > requirements.txt
명령어를 수행하면 해당 파일이 생성된다.

파일을 생성해주지 않으면 web.stdout 로그에서

Apr 14 08:18:19 ip-172-31-11-163 web: ModuleNotFoundError: No module named 'django'

와같은 에러가 발생..


Elastic Beanstalk

django.config 의 wsgi경로 설정으로는 부족했는지 에러를 토해냈고..

Apr 14 09:53:18 ip-172-31-11-163 web: ModuleNotFoundError: No module named 'application'

구성 > 소프트웨어 > 편집 > 컨테이너 옵션에서

WSGIPath가 application으로 설정되어있는데 config.wsgi로 수정해 주었다.

처음에 config/wsgi로 설정했더니

Apr 14 10:03:13 ip-172-31-11-163 web: ModuleNotFoundError: No module named 'config/wsgi'

네..

WSGI경로 설정이 잘못되었다면 502 Bad Gateway Error가 뜨니 설정과 로그를 낱낱이 파헤쳐보자.


Github Actions

git에 push되면 자동으로 배포가 진행되도록 Github Actions를 사용하였다.
이전 프로젝트에서도 사용해보았기 때문에 yml파일은 금방 작성했던것 같다.

IAM 사용자 생성Actions secrets설정 과정은 생략하고,,

deploy.yml

name: Deploy Django project

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

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

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

      - name: Get current time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH-mm-ss
          utcOffset: "+09:00"

      - name: Deploy to EB
        uses: einaregilsson/beanstalk-deploy@v21

        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          application_name: catmemetest #EB 애플리케이션명
          environment_name: Catmemetest-env #EB 환경명
          version_label: github-action-${{steps.current-time.outputs.formattedTime}}
          region: ap-northeast-2
          deployment_package: deploy.zip

git push를 해보자

초록불은 멀고도 험난했다

(66 workflow runs.. 엄청난 삽질의 결과이다..!!!🫠)

제발 초록불 주세요

profile
안되면 되게하라

0개의 댓글