3

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!

Sheridan Gray
  • 698
  • 1
  • 9
  • 23
  • you would run `RAILS_ENV=production rake db:seed`, because `rake db:seed` will run development env in default! – Hieu Pham Oct 08 '16 at 19:40
  • Thanks Hieu for the suggestion. I just tried RAILS_ENV=production rake db:seed and received the exact same response. Any other thoughts? – Sheridan Gray Oct 08 '16 at 19:57

3 Answers3

4

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)

gmcnaughton
  • 2,233
  • 1
  • 21
  • 28
  • This worked great. As a follow up, will I need to re-run this each time I deploy a new version of the app? – Sheridan Gray Oct 14 '16 at 22:44
  • Nope! Once deployed, you should never need to re-seed your database (because the database will persist between deployments - otherwise you'd keep losing all your data!) – gmcnaughton Oct 17 '16 at 13:46
2

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.

Tan Nguyen
  • 3,281
  • 1
  • 18
  • 18
  • So "sudo env PATH=$PATH RAILS_ENV=production bundle exec rake db:seed"? This worked for me. – MSC Aug 05 '18 at 23:16
1

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

Julia Jacobs
  • 469
  • 4
  • 7