Python deployment failing on CodeDeploy

0

Hello.. I have an issue with my CodeDeploy that Im using to deploy a Python application.. Im using Django which is stored in CodeCommit and Im deploying to EC2 instances. It is failing and Im getting this error when trying to deploy.. "Script at specified location: scripts/install_dependencies.sh run as user root failed with exit code 5" Note that when I try to run the script manually (without CodeDeploy), it works without issues. I would appreciate if you guide me on how to get the following: -- Clear the error and get the deployment working. -- Make the application run as a service so I can execute stop_service.sh script when performing in-place software upgrade.

Thanks!!

asked a year ago690 views
2 Answers
1
Accepted Answer

Hi Ivan, based on the information you provided, the appspec.yml' looks fine - we can still improve it a bit as shown in this thread - but it's unclear how you're creating the application service definition as I mentioned in my previous reply because the error indicates that the service definition doesn't exist specifically the file 'app.service' in the directory '/lib/systemd/system'.

Can you follow the below steps and let me know the outcome?

  1. Create a file in your code repo in the 'scripts' folder with the following content and name it 'app.service':
[Unit]
Description=web_app
After=network.target

[Service]
Type=idle
Restart=on-failure
User=root
WorkingDirectory=/home/app/
ExecStart=/bin/bash -c 'python3 /home/app/webapp.py runserver 0.0.0.0:8080'

[Install]
WantedBy=multi-user.target

I assumed your application entry point is the file 'webapp.py', this is just an example and you can substitute the actual one you have. I also assumed the TCP port that you'll bind to the application is 8080, you can change it, but remember allowing that port in the instance's security group or ACL.

  1. Modify your 'install_dependencies.sh' to look like this:
#!/bin/bash
pip3 install -r requirements.txt
cp /home/app/scripts/app.service /lib/systemd/system
  1. Create a script in the folder 'scripts' and name if 'start_service.sh' and paste these commands:
#!/bin/bash
systemctl daemon-reload
systemctl enable app.service
systemctl start app.service
  1. Modify your 'appspec.yml' to look like this:
version: 0.0
os: linux
files:
  - source: /
    destination: /home/app/
   overwrite: true
file_exists_behavior: OVERWRITE

hooks:
  AfterInstall:
    - location: scripts/install_dependencies.sh
      timeout: 180
      runas: root
	  
  ApplicationStart:
    - location: scripts/start_service.sh
      timeout: 120
      runas: root

Let me know how it goes.

profile pictureAWS
mml
answered a year ago
  • I missed creating the service definition file (app.service). Followed your instruction and it worked like a charm!!! Many thanks!

0

Hi, error code 5 refers to I/O error in your script.

Are you able to run your application manually? Typically you would use in Django something like this to run your python app:

python3 /app_code_path/app.py runserver 0.0.0.0:port_number

where app_code_path: represents the path of your code repo in the EC2 instance and port_number is the TCP port that you would bind to the application in the instance OS.

Also can you share with me how your appspec.yml looks like and the content of 'install_dependencies.sh' script or any other scripts that you're referencing in the appspec.yml file?

As for your question on how to make the application run as a service, in Linux, you would typically need to create a file in this path '/lib/systemd/system' , name it like 'test_web' and provide this sample content:

[Unit] Description=sample_web_app After=network.target [Service] Type=idle Restart=on-failure User=root WorkingDirectory=/app_code_path/ ExecStart=/bin/bash -c 'python3 /app_code_path/app.py runserver 0.0.0.0:8080' [Install] WantedBy=multi-user.target

Now when you want to run the app as a service, you would need to execute these commands:

sudo systemctl daemon-reload

sudo systemctl enable test_web

sudo systemctl start test_web

profile pictureAWS
mml
answered a year ago
  • Thanks for the reply.. I tried following your instructions and modify the script a little bit but Im getting this error now:

    [stderr]Failed to execute operation: No such file or directory

    [stderr]Failed to start app.service: Unit not found.

    The 'appspec.yml' content is:

    version: 0.0 os: linux files:

    • source: / destination: /home/app/

    hooks: AfterInstall: - location: scripts/install_dependencies.sh timeout: 180

    And the "install_dependencies.sh' is:

    #!/bin/bash pip3 install -r requirements.txt systemctl daemon-reload systemctl enable app.service systemctl start app.service

    Am I missing anything here??

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions