lambda

class moto.awslambda.models.LambdaBackend(region_name)

Implementation of the AWS Lambda endpoint. Invoking functions is supported - they will run inside a Docker container, emulating the real AWS behaviour as closely as possible.

It is possible to connect from AWS Lambdas to other services, as long as you are running Moto in ServerMode. The Lambda has access to environment variables MOTO_HOST and MOTO_PORT, which can be used to build the url that MotoServer runs on:

def lambda_handler(event, context):
    host = os.environ.get("MOTO_HOST")
    port = os.environ.get("MOTO_PORT")
    url = host + ":" + port
    ec2 = boto3.client('ec2', region_name='us-west-2', endpoint_url=url)

    # Or even simpler:
    full_url = os.environ.get("MOTO_HTTP_ENDPOINT")
    ec2 = boto3.client("ec2", region_name="eu-west-1", endpoint_url=full_url)

    ec2.do_whatever_inside_the_existing_moto_server()

Moto will run on port 5000 by default. This can be overwritten by setting an environment variable when starting Moto:

# This env var will be propagated to the Docker container running the Lambda functions
MOTO_PORT=5000 moto_server

The Docker container uses the default network mode, bridge. The following environment variables are available for fine-grained control over the Docker connection options:

# Provide the name of a custom network to connect to
MOTO_DOCKER_NETWORK_NAME=mycustomnetwork moto_server

# Override the network mode
# For example, network_mode=host would use the network of the host machine
# Note that this option will be ignored if MOTO_DOCKER_NETWORK_NAME is also set
MOTO_DOCKER_NETWORK_MODE=host moto_server

The Docker images used by Moto are taken from the lambci/lambda-repo by default. Use the following environment variable to configure a different repo:

MOTO_DOCKER_LAMBDA_IMAGE=mLupin/docker-lambda

Note

When using the decorators, a Docker container cannot reach Moto, as it does not run as a server. Any boto3-invocations used within your Lambda will try to connect to AWS.

Example usage

@mock_lambda
def test_lambda_behaviour:
    boto3.client("lambda")
    ...

Implemented features for this service

  • [ ] add_layer_version_permission

  • [X] add_permission

  • [X] create_alias

  • [ ] create_code_signing_config

  • [X] create_event_source_mapping

  • [X] create_function

  • [X] delete_alias

  • [ ] delete_code_signing_config

  • [X] delete_event_source_mapping

  • [X] delete_function

  • [ ] delete_function_code_signing_config

  • [X] delete_function_concurrency

  • [ ] delete_function_event_invoke_config

  • [X] delete_layer_version

  • [ ] delete_provisioned_concurrency_config

  • [ ] get_account_settings

  • [X] get_alias

  • [X] get_code_signing_config

  • [X] get_event_source_mapping

  • [X] get_function

  • [ ] get_function_code_signing_config

  • [X] get_function_concurrency

  • [ ] get_function_configuration

  • [ ] get_function_event_invoke_config

  • [X] get_layer_version

  • [ ] get_layer_version_by_arn

  • [ ] get_layer_version_policy

  • [X] get_policy

  • [ ] get_provisioned_concurrency_config

  • [X] invoke

  • [ ] invoke_async

  • [ ] list_aliases

  • [ ] list_code_signing_configs

  • [X] list_event_source_mappings

  • [ ] list_function_event_invoke_configs

  • [X] list_functions

  • [ ] list_functions_by_code_signing_config

  • [ ] list_layer_versions

  • [X] list_layers

  • [ ] list_provisioned_concurrency_configs

  • [X] list_tags

  • [X] list_versions_by_function

  • [X] publish_layer_version

  • [ ] publish_version

  • [ ] put_function_code_signing_config

  • [X] put_function_concurrency

  • [ ] put_function_event_invoke_config

  • [ ] put_provisioned_concurrency_config

  • [ ] remove_layer_version_permission

  • [X] remove_permission

  • [X] tag_resource

  • [X] untag_resource

  • [X] update_alias

    The RevisionId parameter is not yet implemented

  • [ ] update_code_signing_config

  • [X] update_event_source_mapping

  • [X] update_function_code

  • [X] update_function_configuration

  • [ ] update_function_event_invoke_config