How do I save data files to EFS using a Node.js application?

0

I am testing my Node.js application which I deployed using Elastic Beanstalk, and I've successfully mounted an EFS volume to my EC2 instance on the "/efs-mount-point" directory. The problem is I don't know how to get my application to save data to this directory or to my EFS volume.

I am using the fs module to write short text files like this:
fs.writeFile(path, textData, callback);
If I set the path value to something like "./data/filename.txt", then the app works fine except that it does not save the data to EFS. It saves to what I assume is the EBS volume instead. I've tried setting the path to my EFS mount point directory "./efs-mount-point/filename.txt", but then nothing is saved at all.

I was under the impression that EFS storage could be treated like local file system storage in our applications. Am I simply using the wrong path location, or are there additional AWS configurations required to make a Node app save files to the EFS volume?

Sev2
已提问 6 年前2855 查看次数
4 回答
0

Thanks for trying EFS!

You're under the correct impression that reading and writing from EFS should work just like local file system operations, except that the files will be available in a shared file system.

At first glance, I think your issue may be that you're calling your write API with a relative file path rather than absolute by starting with a ".". When you save to "./efs-mount-point/filename.txt", the OS will resolve to "current-directory/efs-mount-point/filename.txt", where "current-directory" is likely the workspace of your Node app. Assuming you mounted EFS to "/efs-mount-point", try changing your API call to say "/efs-mount-point/filename.txt" and see if the files appear in EFS.

Will @ AWS

已回答 6 年前
0

Thank you for your response, but unfortunately, I am still not able to write or read my EFS volume using my Node.js application.

You were right about the relative read path causing problems. When I tried to read the directory of my EFS mount point, I was previously getting a directory does not exist error. When I changed the readFile path name from "./efs-mount-point" to "/home/ec2-user/efs-mount-point", my Node app produced a "permission denied" error instead, which leads me to believe this is a step in the right direction. Unfortunately, I am unable to determine why permission is denied.

I can manually write files to my "/home/ec2-user/efs-mount-point" directory in SSH console using the "touch testFile.txt" command, so I think I've given the root user sufficient permissions. I've even tried opening up my Security Group settings to allow all traffic on both inbound and outbound, but I still get the same permission denied error in my Node app.

I would note that I am using Elastic Beanstalk to launch my Node application, and the EC2 instance and EFS volume are in different security groups (as instructed in the AWS docs). I am using NSF4 to mount my EFS volume automatically on reboot. It only successfully mounts on reboot when I specify the mount path as "/home/ec2-user/efs-mount-point". The path "/efs-mount-point" does not mount successfully. Am I mounting my EFS volume incorrectly, or do you think it's some security setting I've overlooked?

Sev2
已回答 6 年前
0

Great to hear we're getting closer. For the next issue you're running into it sounds like the user the Node.js application is running as doesn't have permissions to the data in EFS. By default EFS file systems are owned by root. When you SSH in as ec2-user you're operating as root, so thats why you're able to read/write from the linux shell. I'm not super familiar with Node, but i'd guess that when you launch a node application it executes as a non-root user (maybe called "node"), and since that user hasn't been granted access to any EFS directories or files you're getting permission denied.

The easiest way to fix this is to issue a 'chmod 777 /myefsmountpoint'. This gives all users read/write/execute permissions to the root directory, allowing them to create new files and read/write to them. You may have to 'chmod 777 /myefsmountpoint/somefile' for any data that already exists that you want Node to have access to. We have a doc that talks a little more about this -
https://docs.aws.amazon.com/efs/latest/ug/accessing-fs-nfs-permissions.html

To the issue about mounting at boot, the only thing I can think of is that when you reboot your instance the directory you're trying to mount to isn't there. This may happen if EBS is only backing your home directory and not root. If this was the case, each time you rebooted your / directory would be rebuilt from the AMI. If true, you could workaround it by either automating the creation of the directory, or mounting to a path backed by EBS (like home).

已回答 6 年前
0

Thank you for your response. You were right that I had to run the chmod 777 command to enable access to my node application, but in order to get it to work, I had to run it on the home directory, ec2-user directory, and efs-mount-point directory. Without running the command on all 3 directories, it doesn't work. Example:
sudo chmod 777 home
sudo chmod 777 ec2-user
sudo chmod 777 efs-mount-point

You were also right that if I try to save my Node documents to a subdirectory, like efs-mount-point/data, then I also have to run the chmod command a 4th time. Example:
sudo chmod 777 data

The good news is I am now able to read and write files to my efs-mount-point directory! Everything seems to be working as expected, so I will mark this question as answered. Thanks again.

Sev2
已回答 6 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则