I'm trying to run a python script on my AWS Elastic Beanstalk 64 Bit Linux instance running Python 3.4. I'm currently using container_commands
within the ebextensions
directory to run some commands, but my issue is that these run before the application is fully deployed to the environment. Does anyone know how to have python scripts execute after deployment? I found this older post detailing a workaround for a Rails environment, but I'm using Python.

- 1,221
- 3
- 17
- 38
-
Well you can do it from outside of EB, what do you use to deploy? – Naguib Ihab Feb 09 '18 at 03:38
-
I'm deploying using the EB CLI. I could ssh into the environment and run the script manually, but I was just wondering if there was a way to set it up to run automatically after every deploy. – hackerman Feb 09 '18 at 06:35
2 Answers
I'd say you have two options:
- Host the script in the
appdeploy/post
folder that's mentioned in this article:
So when I was looking at the eb-tools.log on the EC2 instance I found it was searching for scripts to run in a /opt/elasticbeanstalk/hooks/appdeploy/post directory after restarting the web server. Turns out if you drop shell scripts into this directory they will be executed post deployment, just like you want! However this directory does not exist by default, as Amazon does not use any post-deploy scripts, so we also need to make sure this directory is created.
What I'd do if the first option didn't work out, is to add the code I want to run in the code I'm deploying to that EB and run it as part of the deployment process I am using. For example, if I'm deploying via a batch file on my windows machine I'd do something like:
call eb deploy call http://myurl.com/postDeploy

- 4,259
- 7
- 44
- 80
-
Hi Naguib, thanks for the answer! I've tried option 1 a couple times, but I'm having trouble running the python script. I have it in the main directory of my project as `test.py`, but every combination of file paths I've tried always results in an error saying `No such file or directory.` So I wrote a shell script according to the article, but the line `cd $EB_CONFIG_APP_CURRENT` doesn't work, and neither do paths like `~/var/app/current/test.py`, `/var/app/current/test.py`, `test.py`, `/tmp/deployment/application/test.py`... Do you think there's a way to access the file that I'm missing? – hackerman Feb 09 '18 at 06:31
-
Unfortunately it seems that `eb-tools.log` no longer contains any info about any commands it tries to run (previously referred to `/opt/elasticbeanstalk/hooks/appdeploy/post` directory according to the article). – Kirk Hammett Sep 23 '20 at 18:28
According to Custom Platform Hooks AWS docs:
Warning
Using custom platform hooks on managed platforms isn't supported. Custom platform hooks are designed for custom platforms. On Elastic Beanstalk managed platforms they might work differently or have some issues, and behavior might differ across platforms. On Amazon Linux AMI platforms (preceding Amazon Linux 2), they might still work in useful ways in some cases; use them with caution.
Custom platform hooks are a legacy feature that exists on Amazon Linux AMI platforms. On Amazon Linux 2 platforms, custom platform hooks in the
/opt/elasticbeanstalk/hooks/
folder are entirely discontinued. Elastic Beanstalk doesn't read or execute them. Amazon Linux 2 platforms support a new kind of platform hooks, specifically designed to extend Elastic Beanstalk managed platforms. You can add custom scripts and programs directly to a hooks directory in your application source bundle. Elastic Beanstalk runs them during various instance provisioning stages. For more information, expand the Platform Hooks section in Extending Elastic Beanstalk Linux platforms.
This means that on Amazon Linux 2 you can create .platform/hooks/
directory to host prebuild, predeploy and postdeploy folders with corresponding .sh scripts.

- 656
- 8
- 24