Search code examples
ruby-on-railsamazon-web-servicesamazon-elastic-beanstalkseeding

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!


Solution

  • 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)