amazon-web-servicesaws-lambda

OSError: [Errno 30] Read-only file system: 'tmp/<filename>'


I have a lambda function that leverages a library that wraps an API and manages authentication sessions. To manage the session, the library expects a file containing credentials and a session token to exist. If the token contained in the file is expired, the library will fetch another one using said credentials and write the new session token to the file.

However, the library's attempt to write to the file is causing my lambda to fail with OSError: [Errno 30] Read-only file system: 'tmp/oauth2.json', despite the file it is attempting to write to being in the /tmp directory. My lambda's file structure can be seen below. Is there any possible way I can work around this? I'm hoping that I just have a misunderstanding of where the authentication file should live.

If it is of any help, the library I'm using is yahoo_oauth, and the failure occurs when this code is executed:

from yahoo_oauth import OAuth2
oauth = OAuth2(None, None, from_file='tmp/oauth2.json')

enter image description here


Solution

  • you get FileNotFoundError when specifying /tmp/oauth2.json, it suggests that the file doesn’t exist yet in the /tmp directory. AWS Lambda initializes /tmp as an empty. Therefore, the library cannot locate the needed file.

    you can try to copy the file to your specific directory beforehand:

    import shutil
    import os
    from yahoo_oauth import OAuth2
    
    # Define source and destination paths
    source_path = 'tmp/oauth2.json'  # current path
    destination_path = '/tmp/oauth2.json' # desired path
    
    if not os.path.exists(destination_path):
        shutil.copyfile(source_path, destination_path)
    
    # Now initialize the OAuth2 object
    oauth = OAuth2(None, None, from_file=destination_path)