appspec.yml

version: 0.0
os: linux
files:
  - source: app.pyc
    destination: /opt/scripts/
hooks:
  BeforeInstall:
    - location: scripts/BeforeInstall.sh
      timeout: 100
      runas: root
  AfterInstall:
    - location: scripts/AfterInstall.sh
      timeout: 100
      runas: root
  ApplicationStart:
    - location: scripts/ApplicationStart.sh
      timeout: 100
      runas: root
  ApplicationStop:
    - location: scripts/ApplicationStop.sh
      timeout: 100
      runas: root

Beforeinstall.sh

#!/bin/bash
if [ -d /opt/scripts/ ]; then
    sudo rm -rf /opt/scripts/
fi

mkdir -p /opt/scripts/

Afterinstall.sh

#!/bin/bash
sudo yum install -y python3-pip
pip3 install flask

ApplicationStart.sh

#!/bin/bash
cd /opt/scripts/
nohup python3 app.pyc > /dev/null 2>&1 &

ApplicationStop.sh

#!/bin/bash
SERVER_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" localhost:80/health)
PYTHON_PID=$(pgrep python3)

if [ $SERVER_STATUS == 200 ]; then
    kill -9 $PYTHON_PID
    echo "killed python pid"
fi

buildspec.yml 수정하기

version: 0.2

phases:
  install:
    runtime-versions:
      python: 3.8
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - python3 -m compileall src/app.py
      - mv src/__pycache__/*.pyc ./app.pyc
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - app.pyc
    - appspec.yml
    - scripts/
    - scripts/BeforeInstall.sh
    - scripts/AfterInstall.sh
    - scripts/ApplicationStart.sh
    - scripts/ApplicationStop.sh

Git Repo Push

스크린샷 2025-01-06 오전 12.18.46.png