Skip to content

Enhancement? Support for Entrez on AWS Lambda and perhaps other serverless platforms #1517

Closed

Description

@cparmet

Setup

Hi there. Huge huge fan of the Entrez module.

This is more of an FYI, in case it's useful to know my experience. I defer to the team about whether this enhancement rises to the level of a revision to the package.

I had a Flask web app that uses Entrez working on my local Windows 10 machine. When I deployed to Amazon Web Services as a Lambda function, it ran into trouble.

        Entrez.email = "<my email address>"
        handle = Entrez.esearch(db="pubmed", retmax=10, term=lookupID)
        record = Entrez.read(handle)

Expected behaviour

A record to a PubMed entry, as it does on my local machine.

Actual behaviour

On Lambda, esearch threw an exception and no record was read.

The error occurs in parser.py, makedirs:

  • 'OSError: [Errno 30] Read-only file system: '/home/sbx_user1060'

Full trace here:
trace (showing error with Entrez module).docx.

Steps to reproduce

Deploy a python project to AWS Lambda function that calls an Entrez method, such as esearch or esummary.

Source of error

The trace shows the error happens when the Data Handler creates new directories for local_dtd and local_xsd. I learned that AWS Lambda only gives functions write access within a specific directory, 'tmp'. So Entrez is forbidden from creating the directories where it wants to.

    if platform.system() == 'Windows':
        directory = os.path.join(os.getenv("APPDATA"), "biopython")
    else:  # Unix/Linux/Mac
        home = os.path.expanduser('~')
        directory = os.path.join(home, '.config', 'biopython')
        del home
    local_dtd_dir = os.path.join(directory, 'Bio', 'Entrez', 'DTDs')
    local_xsd_dir = os.path.join(directory, 'Bio', 'Entrez', 'XSDs')
    del directory
    del platform
    try:
        os.makedirs(local_dtd_dir)  # use exist_ok=True on Python >= 3.2
    except OSError as exception:
        # Check if local_dtd_dir already exists, and that it is a directory.
        # Trying os.makedirs first and then checking for os.path.isdir avoids
        # a race condition.
        if not os.path.isdir(local_dtd_dir):
            raise exception
    try:
        os.makedirs(local_xsd_dir)  # use exist_ok=True on Python >= 3.2

Solution that worked for me

I forked Entrez and changed the directory for DataHandler. This allowed my project to work on Lambda.

Forked here: https://github.com/cparmet/Entrez-AWS

I replaced this code ...

if platform.system() == 'Windows':
     directory = os.path.join(os.getenv("APPDATA"), "biopython")
else:  # Unix/Linux/Mac
     home = os.path.expanduser('~')
     directory = os.path.join(home, '.config', 'biopython')
     del home

With...

 directory = '/tmp/'

Closing

I read that another serverless platform, Google Cloud Functions, also limits read/write access. But I'm not informed about it or what the workaround would be.

Thanks for reading.

Activity

  1. peterjc commented on Jan 28, 2018

    @peterjc
    Member

    Would exposing the cache location via something like Bio.Entrez.cache work for you? I can see this might have other applications, e.g. running Biopython as apache as part of a webservice.

    CC @mdehoon

  2. mdehoon commented on Jan 29, 2018

    @mdehoon
    Contributor

    @peterjc Yes exposing Bio.Entrez.cache would work. It's a bit fragile, since python will try to create the directory as soon as Bio.Entrez.Parser is imported.
    So

    >>> from Bio import Entrez
    >>> Entrez.cache = 'my_cache_directory'
    >>> from Bio.Entrez import Parser

    works, but

    >>> from Bio import Entrez
    >>> from Bio.Entrez import Parser
    >>> Entrez.cache = 'my_cache_directory'

    wouldn't.

  3. peterjc commented on Jan 29, 2018

    @peterjc
    Member

    Hmm. Style wise it is unfortunate - lots of projects like to have all their imports at top level before any code (and there are flake8 plugins etc to enforce this).

    We could delay looking at the cache folder, but I think even with that proviso it would work for this use case.

    What do you think @cparmet ?

  4. cparmet commented on Jan 29, 2018

    @cparmet
    ContributorAuthor
  5. peterjc commented on Jan 29, 2018

    @peterjc
    Member

    @cparmet would you like to make a pull request implementing this idea?

  6. cparmet commented on Jan 29, 2018

    @cparmet
    ContributorAuthor
  7. peterjc commented on Nov 10, 2020

    @peterjc
    Member

    I believe this was fixed by exposing the cache setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions