简体   繁体   中英

How do I seed a database for Rails app on AWS Elastic Beanstalk

I built a small Rails app locally and am trying to deploy it to AWS Elastic Beanstalk.

This is the guide I have been following:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Ruby_rails.html

The deployment was successful but the seeding of the database did not occur so all of my static list values are empty.

Although I don't think it is the "ideal" solution, I did try to manually seed the database by SSHing into the EB instance.

eb ssh
cd /var/app/current/
rake db:seed

and then the result is:

[ec2-user@ip-172-31-13-16 current]$ rake db:seed
Rails Error: Unable to access log file. Please ensure that /var/app/current/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/app/current/log/production.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
   (0.1ms)  begin transaction

 ...

(0.1ms)  rollback transaction
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ReadOnlyException: attempt to write a readonly database: INSERT INTO "users" ("first_name", "last_name", "email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)
/var/app/current/db/seeds.rb:9:in `<top (required)>'
SQLite3::ReadOnlyException: attempt to write a readonly database
/var/app/current/db/seeds.rb:9:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

What is the proper way to do this?

Thanks in advance!

Both errors ( Unable to access log file and attempt to write a readonly database ) are due to file access permissions.

When you ssh into an EC2 instance, you usually log in as a user who doesn't have write access to the capistrano deploy directory. (You have read access, so you can see the files, but not write - so you can't write logs or create a new sqlite database file).

You can use sudo to run rake as another user:

sudo -u app env PATH=$PATH RAILS_ENV=production bundle exec rake db:seed

(Change "-u app" to whatever username your app runs as - or leave it out just to run the command as root)

Try RAILS_ENV=production bundle exec rake db:seed

it executes the rake script with the command db:seed in the context of the current bundle.

In case you're here and the above solutions didn't work for you.

Apart from using the command provided in this answer above by benchwarmer:

https://stackoverflow.com/a/17232607/1216245

I had to run the seed command providing env vars for the master key and all rds settings.

bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>

And it worked, finally :)

You can check all this in the Configuration panel for your environment in the AWS console (dashboard).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM