Gpcnec76
Gpcnec76

Reputation: 69

Rails AWS/EB tmp file permissions

I'm trying to run a Rails application on AWS/EB that creates a TempFile. Code works correctly when I run in Development, but in Production it fails. Here is the code that is executed:

  v_index_file_save = Tempfile.new(['index','.lst'])

I get the following error message:

Errno::EACCES: Permission denied @ dir_s_mkdir - /var/app/current/tmp/1495598860-11267-0001-8597 

I found an entry that suggests creating a pre-compile script to change the permissions on my /tmp directory. Here is the scipt:

commands:
  01_set_tmp_permissions:
    command: "chmod 0777 /var/app/current/tmp"

Deploy seemingly works correctly, but the permissions are not updated on the /tmp/ directory. Any suggestions??

Upvotes: 0

Views: 1676

Answers (2)

Julia Jacobs
Julia Jacobs

Reputation: 509

Since I still ran into this issue in 2020 and this fix didn't quite work for me, I thought I'd share what did.

After re-reading the EB AWS docs about container commands, instead of /var/app/ondeck/tmp I changed the permissions for /var/app/staging/tmp:

The specified commands run as the root user, and are processed in alphabetical order by name. Container commands are run from the staging directory, where your source code is extracted prior to being deployed to the application server. Any changes you make to your source code in the staging directory with a container command will be included when the source is deployed to its final location.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-container-commands

So in the end my extension looks like this:

container_commands:
  01_set_tmp_permissions:
    command: "chmod -R go+w /var/app/staging/tmp/cache"

And it works! As in: on to the next problem :D

PS. There's a thread in Shopify/bootsnap about this issue, for reference: https://github.com/Shopify/bootsnap/issues/171. I posted my solution there too.

Upvotes: 4

Brian
Brian

Reputation: 5491

First off, the reason that your command isn't doing what you want is that it's running too early. If you check the documentation, it says

The commands are processed in alphabetical order by name, and they run before the application and web server are set up and the application version file is extracted.

So what's happening is that you're changing permissions on the directory containing the soon-to-be-replaced version of your application. (EB extracts the new version to /var/app/ondeck, deletes /var/app/current, and finally renames /var/app/ondeck to /var/app/current.)

You might have more luck using a container command like this:

container_commands:
  01_set_tmp_permissions:
    command: "chmod 0777 /var/app/ondeck/tmp"

Container commands run later in the deployment, so it might do what you want.

That being said, you should already have write access to /var/app/current/tmp. What happens when you run ls -ld /var/app/current/tmp? And what user is your app running as?

Upvotes: 4

Related Questions