如何使用Elastic Beanstalk创建“tmp”目录?

时间:2022-08-25 09:19:13

I'm using Node.js and need to save files to a tmp directory within my app. The problem is that Elastic Beanstalk does not set the app directory to be writable by the app. So when I try to create the temp directory I get this error

我正在使用Node.js并需要将文件保存到我的应用程序中的tmp目录。问题是Elastic Beanstalk没有将app目录设置为可由应用程序写入。因此,当我尝试创建临时目录时,我收到此错误

fs.js:653
  return binding.mkdir(pathModule._makeLong(path),
                 ^
Error: EACCES, permission denied '/var/app/tmp/'
    at Object.fs.mkdirSync (fs.js:653:18)
    at Promise.<anonymous> (/var/app/current/routes/auth.js:116:18)
    at Promise.<anonymous> (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.emit (events.js:95:17)
    at Promise.emit (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/var/app/current/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
    at /var/app/current/node_modules/mongoose/lib/query.js:1394:13
    at model.Document.init (/var/app/current/node_modules/mongoose/lib/document.js:250:11)
    at completeOne (/var/app/current/node_modules/mongoose/lib/query.js:1392:10)
    at Object.cb (/var/app/current/node_modules/mongoose/lib/query.js:1151:11)

I've tried several things such as an app-setup.sh script within .ebextensions/scripts/app-setup.sh that looks like this

我已经尝试过几个东西,例如.ebextensions / scripts / app-setup.sh中的app-setup.sh脚本,看起来像这样

#!/bin/bash

# Check if this is the very first time that this script is running
if ([ ! -f /root/.not-a-new-instance.txt ]) then
    newEC2Instance=true
fi



# Get the directory of 'this' script
dirCurScript=$(dirname "${BASH_SOURCE[0]}")

# Fix the line endings of all files
find $dirCurScript/../../ -type f | xargs dos2unix -q -k

# Get the app configuration environment variables
source $dirCurScript/../../copy-to-slash/root/.elastic-beanstalk-app
export ELASTICBEANSTALK_APP_DIR="/$ELASTICBEANSTALK_APP_NAME"

appName="$ELASTICBEANSTALK_APP_NAME"
dirApp="$ELASTICBEANSTALK_APP_DIR"

dirAppExt="$ELASTICBEANSTALK_APP_DIR/.ebextensions"
dirAppTmp="$ELASTICBEANSTALK_APP_DIR/tmp"

dirAppData="$dirAppExt/data"
dirAppScript="$dirAppExt/scripts"


# Create tmp directory
mkdir -p $dirApp/tmp

# Set permissions
chmod 777 $dirApp
chmod 777 $dirApp/tmp

# Ensuring all the required environment settings after all the above setup
if ([ -f ~/.bash_profile ]) then
    source ~/.bash_profile
fi

# If new instance, now it is not new anymore
if ([ $newEC2Instance ]) then
    echo -n "" > /root/.not-a-new-instance.txt
fi


# Print the finish time of this script
echo $(date)


# Always successful exit so that beanstalk does not stop creating the environment
exit 0

As well as creating a file called 02_env.config within .ebextensions that looks like this

以及在.ebextensions中创建一个名为02_env.config的文件,如下所示

# .ebextensions/99datadog.config
    container_commands:
        01mkdir:
            command: "mkdir /var/app/tmp"
        02chmod:
            command: "chmod 777 /var/app/tmp"

Neither seem to work. How can I create a tmp directory within my app that is writable?

似乎都没有用。如何在我的应用程序中创建一个可写的tmp目录?

1 个解决方案

#1


6  

I recently experienced the same issue with a .NET application where the application was failing because it couldn't write to a directory, even after I had set the permissions.

我最近遇到了与应用程序失败的.NET应用程序相同的问题,因为它无法写入目录,即使在我设置了权限之后也是如此。

What I found was that after the whole .ebextensions process was completed, the final step was a web container permissions update which ended up overwriting my ebextensions permissions change.

我发现在整个.ebextensions过程完成后,最后一步是Web容器权限更新,最终覆盖了我的ebextensions权限更改。

To solve it I moved the directory outside of the web container and updated the application to write there instead.

为了解决这个问题,我将目录移到了Web容器之外,并更新了应用程序以在那里写入。

In your case I would suggest /tmp

在你的情况下,我会建议/ tmp

#1


6  

I recently experienced the same issue with a .NET application where the application was failing because it couldn't write to a directory, even after I had set the permissions.

我最近遇到了与应用程序失败的.NET应用程序相同的问题,因为它无法写入目录,即使在我设置了权限之后也是如此。

What I found was that after the whole .ebextensions process was completed, the final step was a web container permissions update which ended up overwriting my ebextensions permissions change.

我发现在整个.ebextensions过程完成后,最后一步是Web容器权限更新,最终覆盖了我的ebextensions权限更改。

To solve it I moved the directory outside of the web container and updated the application to write there instead.

为了解决这个问题,我将目录移到了Web容器之外,并更新了应用程序以在那里写入。

In your case I would suggest /tmp

在你的情况下,我会建议/ tmp