I know that you can run tasks as part of each deploy, but I only want to bootstrap the database once.
5 Answers
Kristian's answer is close, but you should also add:
container_commands:
seeddb:
command: 'export HOME=/root; rake db:seed'
leader_only: true
So that the DB is only seeded from 1 EC2 instance, rather than all of them at the same time. Depending on your EB deployment/version, the export HOME
may or may not be needed.

- 2,882
- 4
- 27
- 28
-
1I used the above container command as prescribed and it worked perfectly. I must admit that I use [seed_fu](https://github.com/mbleigh/seed-fu) though. – Dan Jan 29 '15 at 04:30
-
Still works for me. I am wondering, when will this command be executed? – Karens Dec 08 '15 at 10:03
-
3I still wonder how this will only seed the database once, aren't these commands supposed to run EACH time a new application version is uploaded? (AFAIK leader_only is to avoid several instances running the same command at once, which is a different thing) – Gustavo Rubio Feb 05 '16 at 21:16
-
1@GustavoRubio You can place `if` statements in your seed file to only run the seeds if the database is empty. – Cruz Nunez Nov 29 '17 at 21:56
I used the information provided at this address to create a script that will run AFTER migrations and after each deploy: http://www.emind.co/how-to/how-to-run-rake-dbseed-in-amazon-elastic-beanstalk
I preferred this method so that I could keep track of the file on the EC2 instance. When I initially deployed this on my old servers (which were running Linux <1.0.9) I had no issue. However, I recently had to upgrade the server machines to 64bit Amazon Linux 2014.03 v1.0.9 running Ruby 2.0 (Puma) and the script began to fail. It will likely fail if you are using different Linux versions.
The key here is /usr/local/bin/ to your rake command to use the proper rake. I took this directly from the other scripts found at /opt/elasticbeanstalk/hooks/appdeploy/pre/:
#.ebextensions/db_seed.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/13_db_seed.sh":
mode: "00755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/containerfiles/envvars
cd $EB_CONFIG_APP_ONDECK
su -c "leader_only /usr/local/bin/rake db:seed" $EB_CONFIG_APP_USER ||
echo "Rake task failed to run, skipping seeding."
true
It should be noted that this script is still not correct as I am not running "bundle exec rake" which is highly advised if you plan to run any rake command (it's a feature request currently on AWS for other scripts). If you wish to have ALL scripts run the bundle exec before "rake" take a look at the .config files posted here: https://github.com/alienfast/elastic-beanstalk
If this still doesn't work for you, I suggest sshing to one of your EC2 instances that are running your app, or deploy a new one running the same Linux/Ruby version and navigate to the "/opt/elasticbeanstalk/hooks/appdeploy/pre/" to see what the other scripts are doing.
I hope others find this useful!

- 597
- 6
- 12
to do commands on your elastic beanstalk instance, you can setup custom commands configurations that will get run when you do update your application. (In fact, there is a whole docs page dedicated to the different types of container commands you can do).
- if you don't already have an
.ebextensions
directory in the root of your application, create one. - any file you put in here will be run. name it whatever you want as long as it ends with "
.config
" -- I arbitrarily named mineseed.config
borrowing from what hfogel wrote, you can put something in there like this:
container_commands: 01seed: command: rake db:seed
add this new code to your repo:
git add .
thengit commit -m 'added seed config script'
thengit push
- then push this new code to your aws eb instance(s):
git aws.push
To verify that your command actually ran, go into your elastic beanstalk console and expand the details for your environment, and navigate to the logs
tab, and refresh your logs and view them. Inside there, simply do a ctrl + f
for "seed" until you see your seed command was run. If you don't see it in there somewhere, then it didn't run.

- 21,204
- 19
- 101
- 176
-
@JamelToms it worked in 2013.. maybe EB has changed the way it runs commands. go ahead and look through their documentation and find something about that, try it, and update this question with a new answer – Kristian Jul 21 '14 at 16:53
The only way I found was to ssh into the ec2 instance and run "rake db:seed RAILS_ENV=production" manually from /var/app/current.

- 39
- 1
- 3
-
1be careful not to run this task as root. db:seed creates some files in /var/app/tmp which your app can't read or overwrite if they are created by root. If they are created by root you will get EACCES errors in the log when you try to create new pages – Will May 29 '13 at 10:56
-
2This does not work: `rake db:seed RAILS_ENV=production /usr/bin/rake:9:in 'require': no such file to load -- rubygems (LoadError) from /usr/bin/rake:9` – Peter Sankauskas May 30 '13 at 23:58
It's a while since the question was asked, so perhaps you've figured it out. Anyway, you can add a file called (for instance) seed.config in the .beanstalk folder. Enter something like this and your seeds will be run:
container_commands:
01seed:
command: rake db:seed
-
by `.beanstalk` folder do you mean `.elasticbeanstalk` ? Also, did you have to `eb stop` and `eb start` for that to take effect? I added those commands to a seed.config file and did `git aws.push` but the seeding did not show up in my logs – Kristian May 11 '13 at 18:57