lambda

class moto.awslambda.models.LambdaBackend(region_name: str, account_id: str)

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.

Warning

When invoking a function using the decorators, the created Docker container cannot reach Moto (or it’s in-memory state). Any AWS SDK-requests within your Lambda will try to connect to AWS instead.

It is possible to connect from AWS Lambdas to other services, as long as you are running MotoProxy or the MotoServer in a Docker container.

When running the MotoProxy, calls to other AWS services are automatically proxied.

When running MotoServer, 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 following repositories:

  • mlupin/docker-lambda (for recent versions)

  • lambci/lambda (for older/outdated versions)

Use the following environment variable to configure Moto to look for images in an additional repository:

MOTO_DOCKER_LAMBDA_IMAGE=mlupin/docker-lambda

_-_-_-_

Use the following environment variable if you want to configure the data directory used by the Docker containers:

MOTO_LAMBDA_DATA_DIR=/tmp/data

_-_-_-_

If you want to mock the Lambda-containers invocation without starting a Docker-container, use the simple decorator:

@mock_aws(config={"lambda": {"use_docker": False}})

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

    The Code.ImageUri is not validated by default. Set environment variable MOTO_LAMBDA_STUB_ECR=false if you want to validate the image exists in our mocked ECR.

  • [X] create_function_url_config

    The Qualifier-parameter is not yet implemented. Function URLs are not yet mocked, so invoking them will fail

  • [X] delete_alias

  • [ ] delete_code_signing_config

  • [X] delete_event_source_mapping

  • [X] delete_function

  • [ ] delete_function_code_signing_config

  • [X] delete_function_concurrency

  • [X] delete_function_event_invoke_config

  • [X] delete_function_url_config

    The Qualifier-parameter is not yet implemented

  • [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

  • [X] get_function_event_invoke_config

  • [X] get_function_url_config

    The Qualifier-parameter is not yet implemented

  • [X] get_layer_version

  • [ ] get_layer_version_by_arn

  • [ ] get_layer_version_policy

  • [X] get_policy

  • [ ] get_provisioned_concurrency_config

  • [ ] get_runtime_management_config

  • [X] invoke

    Invoking a Function with PackageType=Image is not yet supported.

    Invoking a Funcation against Lambda without docker now supports customised responses, the default being Simple Lambda happy path OK. You can use a dedicated API to override this, by configuring a queue of expected results.

    A request to invoke will take the first result from that queue.

    Configure this queue by making an HTTP request to /moto-api/static/lambda-simple/response. An example invocation looks like this:

    expected_results = {"results": ["test", "test 2"], "region": "us-east-1"}
    resp = requests.post(
        "http://motoapi.amazonaws.com/moto-api/static/lambda-simple/response",
        json=expected_results
    )
    assert resp.status_code == 201
    
    client = boto3.client("lambda", region_name="us-east-1")
    resp = client.invoke(...) # resp["Payload"].read().decode() == "test"
    resp = client.invoke(...) # resp["Payload"].read().decode() == "test2"
    
  • [ ] invoke_async

  • [ ] invoke_with_response_stream

  • [X] list_aliases

  • [ ] list_code_signing_configs

  • [X] list_event_source_mappings

  • [X] list_function_event_invoke_configs

  • [ ] list_function_url_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 Establish concurrency limit/reservations for a function

    Actual lambda restricts concurrency to 1000 (default) per region/account across all functions; we approximate that behavior by summing across all functions (hopefully all in the same account and region) and allowing the caller to simulate an increased quota.

    By default, no quota is enforced in order to preserve compatibility with existing code that assumes it can do as many things as it likes. To model actual AWS behavior, define the MOTO_LAMBDA_CONCURRENCY_QUOTA environment variable prior to testing.

  • [X] put_function_event_invoke_config

  • [ ] put_provisioned_concurrency_config

  • [ ] put_runtime_management_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

  • [X] update_function_event_invoke_config

  • [X] update_function_url_config

    The Qualifier-parameter is not yet implemented