Search code examples
pythonlambdaboto3moto

moto mock_lambda get_function not working


Im trying to call the get_function method using moto mock lambda, and im getting UnrecognizedClientException as result, which means the mock is not working. The following is the code im using. Note that the call to create_function does work correctly. For some reason when i call get_function the mock stops working.

import boto3
import io
import zipfile
import json
import os
from moto import mock_lambda
import botostubs

def get_zip_lambda():
    pfunc = """
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', pfunc)
    zip_file.close()
    zip_output.seek(0)
    return zip_output.read()


DEF_ECHO_LAMBDA = {
    'Runtime': 'python3.7',
    'Role': 'test-iam-role',
    'Handler': 'lambda_function.lambda_handler',
    'Code': {
        'ZipFile': get_zip_lambda(),
    },
    'Description': 'lambda function',
    'Timeout': 3,
    'MemorySize': 128,
    'Publish': True
}

def test_configure_current_service():
    # Arrange
    with mock_lambda():
        fn_name = "TestEcho_ian-local"
        original_variables = {"original_env_variable": "original_value"}

        aws_lambda: botostubs.Lambda = boto3.client('lambda')
        echo_lambda = DEF_ECHO_LAMBDA
        echo_lambda['FunctionName'] = fn_name
        response = aws_lambda.create_function(**echo_lambda)

        get_response = aws_lambda.get_function(FunctionName=fn_name)
        print(f"get testing lambda response: {get_response}")

test_configure_current_service()

Solution

  • It appears that moto hasn't implemented the get_function completely. I tried adding the argument Qualifier and then it works.

    Also, If you try calling using the arn of the function instead of the name it doesn't work. It appears that you need to use always the function name and the qualifier.

    get_response = aws_lambda.get_function(FunctionName=fn_name, Qualifier="$LATEST")