[Python] Attempting to run chess engine (Stockfish) on AWS Lambda — how to fix this...

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    (This is my very first post here; any and all criticisms/feedback are welcome!)

    I found a very similar post on StackOverflow; in it, the poster was doing pretty much the exact same thing I'm currently looking to do: running Stockfish on AWS Lambda. How to run Stockfish on AWS Lambda. However, the error that James there ran into was different from the error that I've been getting. They'd gotten a ValueError, while I've been running into this error:

    "errorMessage": "[Errno 13] Permission denied: '/tmp/stockfish_14_x64_avx2'",
    "errorType": "PermissionError",
    "requestId": "7e69cdda-189b-49f0-94c9-5f4701d4a62a",
    "stackTrace": [
    " File \"/var/task/lambda_function.py\", line 14, in lambda_handler\n stockfish = Stockfish(path=stockfish_new_path)\n",
    " File \"/var/task/stockfish/models.py\", line 47, in __init__\n self._stockfish = subprocess.Popen(\n",
    " File \"/var/lang/lib/python3.13/subprocess.py\", line 1038, in __init__\n self._execute_child(args, executable, preexec_fn, close_fds,\n",
    " File \"/var/lang/lib/python3.13/subprocess.py\", line 1859, in _execute_child\n self._posix_spawn(args, executable, env, restore_signals, close_fds,\n",
    " File \"/var/lang/lib/python3.13/subprocess.py\", line 1803, in _posix_spawn\n self.pid = os.posix_spawn(executable, args, env, **kwargs)\n"



    This is the code that I have at the moment:

    import os, shutil, stat
    import json
    from stockfish import Stockfish

    stockfish_path = '/var/task/stockfish_14_x64_avx2'
    stockfish_new_path = '/tmp/stockfish_14_x64_avx2'

    def lambda_handler(event, context):
    shutil.copytree(stockfish_path, stockfish_new_path)
    os.chmod(stockfish_new_path, 0o777)

    print_permissions(stockfish_new_path)

    stockfish = Stockfish(path=stockfish_new_path)

    # Set the chess position or FEN notation as needed
    position = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2"
    stockfish.set_fen_position(position)

    best_move = stockfish.get_best_move()

    response = {
    "best_move": best_move
    }

    return {
    "statusCode": 200,
    "body": json.dumps(response)
    }

    def print_permissions(file_path):
    st = os.stat(file_path)
    oct_perm = oct(st.st_mode)
    print(oct_perm)


    I've been butting my head against this problem for a few hours now, having looked to read all I can about AWS Lambda and its directories and permissions and such. Currently, I just have the Stockfish executable in the /var/task directory; previously, I'd tried using a Lambda Layer (and so the executable would've been under /opt).

    Trying to run the Stockfish executable while it's in /var/task directory brings up the Permission Denied error. And so, I looked to try using chmod on it, but that wasn't possible to do while it's in /var/task either, so I moved it to /tmp, which from what I understand is the only place in the Lambda runtime environment that is writable. And once it's in /tmp, I tried chmod again, which succeeded — but still, that doesn't seem to resolve the Permission Denied error.

    I suspect that I may be missing something completely obvious; this is my first time working with AWS Lambda, or just cloud computing in general. Any thoughts on how to resolve this Permission Denied issue?

    Continue reading...

Compartilhe esta Página