- Newest
- Most votes
- Most comments
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?
- 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.
- 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
- 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
- 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.
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
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??
Relevant content
- asked a year ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 months ago
I missed creating the service definition file (app.service). Followed your instruction and it worked like a charm!!! Many thanks!