> For the complete documentation index, see [llms.txt](https://aws.youngwjung.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aws.youngwjung.com/archive/aws-codedeploy/fix-error.md).

# 배포 실패 수정 방법

1\. Deploy 섹션에서 **Details**를 클릭해서 CodeDeploy 대시보드로 이동

<div align="left"><img src="/files/0eOwppjwBa4whVCVI6DD" alt=""></div>

![](/files/7LtuuMYxMXX0nM0RsPaV)

2\. Deployment 상태창에서 **View events** 클릭

![](/files/CtzJ4tdvQoL0kOzPF8mm)

3\. 실패한 이벤트의 **Error Code**에 있는 **ScriptFailed** 클릭

![](/files/tR2lUVRavCwRRJ0G3wXB)

4\. 이벤트 로그를 보면 초기 데이터베이스 설정을 할때 데이터베이스에 접근이 안되서 에러가 발생하는것을 확인할수 있습니다.

<div align="left"><img src="/files/gOeBZ6PWojtBcgDtFOqA" alt=""></div>

5\. `scripts/InitializeDatabase.sh` 파일을 보면 **Host** 값이 *localhost*로 되어 있습니다.&#x20;

```
#!/bin/bash

set -e

cat << EOF | psql -h localhost -U postres
CREATE DATABASE local_library;
CREATE USER local_library WITH PASSWORD 'asdf1234';
GRANT ALL PRIVILEGES ON DATABASE local_library TO local_library;
ALTER USER local_library CREATEDB;
EOF
```

**Host** 값을 RDS에 이미 생성되어 있는 *locallibrary-prod* 인스턴스의 엔드포인트로 바꾸고 어드민 유저이름과 비밀번호는 **Secrets Manager**에서 *prodDatabaseSecret*로 시작하는 Secret에서 확인가능합니다. 확인후에 아래와 같이 수정해 주시면 됩니다.

```
#!/bin/bash

set -e

cat << EOF | PGPASSWORD=<ADMIN_PASSWORD> psql -h <RDS_ENDPOINT> -U <ADMIN_USERNAME>
CREATE DATABASE local_library;
CREATE USER local_library WITH PASSWORD 'asdf1234';
GRANT ALL PRIVILEGES ON DATABASE local_library TO local_library;
ALTER USER local_library CREATEDB;
EOF
```

6\. 추가로 `locallibrary/settings.py`에 명시한 RDS 엔드포인트도 업데이트 하고 수정한 코드를 GitHub에 반영하면 CodeDeploy로 해당 변경분을 자동으로 배포합니다.

7\. 이번에는 ApplicationStart 단계에서 배포가 실패합니다. 로그를 보면 다음과 같습니다.

![](/files/CzGEWL0jTNsVe3JjwRUW)

8\. System Manager Session Manager를 통해서 배포가 진행된 EC2 인스턴스에 접속해서 아파치 서버 상태를 보면

```
[ec2-user@ip-10-0-55-192 ~]$ systemctl status httpd.service -l
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2022-04-07 00:59:20 UTC; 8min ago
     Docs: man:httpd.service(8)
  Process: 23225 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 23225 (code=exited, status=1/FAILURE)

Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal systemd[1]: Starting The Apache HTTP Server...
Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal httpd[23225]: httpd: Syntax error on line 362 of /etc/httpd/conf/httpd.conf: Syntax error on line 1 of /etc/httpd/conf.d/app.conf: Cannot load /root/django-locallibrary-tutorial/venv/lib64/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so into server: /root/django-locallibrary-tutorial/venv/lib64/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so: cannot open shared object file: No such file or directory
Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal systemd[1]: Failed to start The Apache HTTP Server.
Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal systemd[1]: Unit httpd.service entered failed state.
Apr 07 00:59:20 ip-10-0-55-192.ap-northeast-2.compute.internal systemd[1]: httpd.service failed.
[ec2-user@ip-10-0-55-192 ~]$
```

9\. 기존에 개발환경에서 테스트할때는 `/root/django-locallibrary-tutorial/venv`에 Python 가상환경을 생성했는데 운영환경을 구성할때는 `/opt/venv` 경로에 가상환경을 생성하면서 아파치 설정파일에서 모듈 및 애플리케이션 소스코드를 찾지 못하고 있어서 발생하는 문제입니다. `app.conf`에 있는 `/root/django-locallibrary-tutorial/venv`를 `/opt/venv`로 `/root/django-locallibrary-tutorial`를 `/opt/django-locallibrary-tutorial` 로 수정하고 코드를 GitHub에 반영합니다.

```
LoadModule wsgi_module "/opt/venv/lib64/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so"
WSGIPythonHome "/opt/venv"

<Directory /opt/django-locallibrary-tutorial/locallibrary>
    Require all granted
</Directory>

WSGIDaemonProcess locallibrary python-path=/opt/django-locallibrary-tutorial:/opt/venv/lib/python3.7/site-packages
WSGIProcessGroup locallibrary
WSGIScriptAlias / /opt/django-locallibrary-tutorial/locallibrary/wsgi.py
```
