2022년 6월 1일 수요일

aws beanstalk ebextension을 이용한 스크립트 실행

 서버가 시작되거나 어플리케이션이 시작되는 등의 일부 이벤트 상황에 스크립트를 실행시킬 수 있다.


빈스톡 구플랫폼인 Amazon Linux 1은 

https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/custom-platform-hooks.html


현세대 플랫폼인 Amazon Linux 2는

https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/platforms-linux-extend.html

여기서 확인하면 된다.


아래 내용은 Linux 1에 해당되는 내용이다.

우선 실행할 스크립트를 .ebextensions 폴더에 파일로 만든다.(https://mfcdev.blogspot.com/2021/10/aws-ebextensions-eb.html 참조)


files:

    "/startTime.sh":

        mode: "000700"

        owner: root

        group: root

        content: |

            #!/bin/bash

            now=`date`

            echo [start] $now >> /start_time


위 처럼 파일을 만들었다면, 이 파일을 특정 폴더에 넣어주면 특정 시점에 자동으로 실행시킬 수 있다.

위 내용에 아래의 내용을 추가하자.


container_commands:

    01mkdir_appdeploy_post:

        test: '[ ! -d /opt/elasticbeanstalk/hooks/appdeploy/post ]'

        command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"

    02mkdir_restartappserver_post:

            test: '[ ! -d /opt/elasticbeanstalk/hooks/restartappserver/post ]'

            command: "mkdir /opt/elasticbeanstalk/hooks/restartappserver/post"

    03_copy_appdeploy:

        command: "cp /startTime.sh /opt/elasticbeanstalk/hooks/appdeploy/post/"

    04_copy_restartappserver:

        command: "cp /startTime.sh /opt/elasticbeanstalk/hooks/restartappserver/post/"

/opt/elasticbeanstalk/hooks/restartappserver/post, /opt/elasticbeanstalk/hooks/appdeploy/post 두 개의 폴더를 만들고 있다. 빈스톡 환경으로 서버가 만들어져도 위 폴더가 없기 때문에 만들어서 스크립트 파일을 넣어줘야 한다.

restartappserver는 앱서버가 재시작하면 수행하고, appdeploy는 어플리케이션을 배포할 때 수행한다. 그 뒤에 있는 post는 각각의 단계에서 수행되는 시점인데 pre, enact, post가 있으며 순서는 pre -> enact -> post로 불리며, 각 폴더의 스크립트들은 알파벳 순서대로 호출된다.

위처럼 해놓으면 앱 재시작, 어플리케이션 배포할 때 startTime.sh 스크립트를 실행해준다.


Linux 2에서는 위와 같이 하지 않고, .platform/hooks/ 폴더 하위에 prebuild, predeploy, postdeploy 폴더를 만들고 스크립트 파일을 넣어주면 동작한다. 앱 재시작같은 이벤트는 없다. 자세한 내용은 위의 Linux 2 url을 참고 바란다.

댓글 없음: