Search code examples
pythonaws-lambdapytestboto3moto

Using Moto to Mock Lambda Response returns 'b"error running lambda: (2, 'WaitNamedPipe', 'The system cannot find the file specified.')"'


I am running a unit test (Pytest) to that invokes a mocked Lambda (with Moto) and expects a response. Assume we're importing boto3 and have created a lambda client constant. Here is the function:

def invoke_lambda():
    request = {'message': 'Hello World!'}
    lambda_response = LAMBDA_CLIENT.invoke(
        FunctionName='hello-world-lambda',
        InvocationType='RequestResponse',
        Payload=json.dumps(request)
    )

    response = lambda_response['Payload'].read()
    print(response)
    return response

Here is my unit test:

import zipfile

import boto3
from moto import mock_lambda

CLIENT = boto3.client('lambda', region_name='us-east-1')

# Expected response setup and zip file for lambda mock creation
def lambda_event():
    code = '''
        def lambda_handler(event, context):
            return event
        '''
    zip_output = io.BytesIO()
    zip_file = zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED)
    zip_file.writestr('lambda_function.py', code)
    zip_file.close()
    zip_output.seek(0)
    return zip_output.read()

# create mocked lambda with zip file
def mock_some_lambda(lambda_name, return_event):
    return CLIENT.create_function(
        FunctionName=lambda_name,
        Runtime='python2.7',
        Role='test-iam-role',
        Handler='lambda_function.lambda_handler',
        Code={
            'ZipFile': return_event,
        },
        Publish=True,
        Timeout=30,
        MemorySize=128
    )

# Test function
@mock_lambda
def test_invoke_lambda():
    mock_some_lambda('hello-world-lambda', lambda_event)
    response = function_code.invoke_lambda()
    assert response['message'] == 'Hello World!'

When running the Pytest, I see the Lambda response is: b"error running lambda: (2, 'WaitNamedPipe', 'The system cannot find the file specified.')"

Any ideas on how to fix this? I see some issues pointing to docker from google searches, but from my understanding Moto should be able to run this stuff out of the box with no need to run this in a docker container. Help would be great :)


Solution

  • Turns out I just needed to install Docker for Windows and have Docker running... I do not see this issue anymore so I guess everything is fine. – SamN just now edit