Pranab
Pranab

Reputation: 2787

How do I run a python script once at startup on Elastic Beanstalk

I want to run a python script 'indefinitely' on an EB instance, automatically once deployed (I don't want to SSH in). To do that, I think I should run

source /opt/python/run/venv/bin/activate && nohup python myscriptname.py &

after the Elastic Beanstalk instance is deployed. But where do I put the above command to automatically run this post deployment?

I have looked at How do I install a Python script on Amazon's Elastic Beanstalk? but I don't think a cron job is suitable for me.

I have used container_commands before but they seem to run pre-deployment.

Container commands run after the application and web server have been set up and the application version archive has been extracted, but before the application version is deployed.

How do I run a post-deploy script?

Upvotes: 2

Views: 2380

Answers (1)

denov
denov

Reputation: 12688

As always there's a few ways to do this in AWS. You could start an instance, customize away, then create an AMI from this image. You could then tell beanstalk to use this custom AMI

Or could use ebextensions to custom your instance on creation and app restart. I'm currently using this to install (if not already instaled) logstash and download the latest GeoLiteCity db when my app starts up.

Here's a sample from the docs

container_commands:
  collectstatic:
    command: "django-admin.py collectstatic --noinput"
  01syncdb:
    command: "django-admin.py syncdb --noinput"
    leader_only: true
  02migrate:
    command: "django-admin.py migrate"
    leader_only: true
  99customize:
    command: "scripts/customize.sh"

Upvotes: 1

Related Questions