AWSLambda: Try to pull images from multiple repositories (#5895)

This commit is contained in:
Bert Blommers 2023-02-02 11:56:50 -01:00 committed by GitHub
parent 4ed8b93e18
commit c1d85a005d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -765,9 +765,27 @@ class LambdaFunction(CloudFormationModel, DockerModel):
"host.docker.internal": "host-gateway"
}
image_repo = settings.moto_lambda_image()
image_ref = f"{image_repo}:{self.run_time}"
self.docker_client.images.pull(":".join(parse_image_ref(image_ref)))
# The requested image can be found in one of a few repos:
# - User-provided repo
# - mlupin/docker-lambda (the repo with up-to-date AWSLambda images
# - lambci/lambda (the repo with older/outdated AWSLambda images
#
# We'll cycle through all of them - when we find the repo that contains our image, we use it
image_repos = set(
[
settings.moto_lambda_image(),
"mlupin/docker-lambda",
"lambci/lambda",
]
)
for image_repo in image_repos:
image_ref = f"{image_repo}:{self.run_time}"
full_ref = ":".join(parse_image_ref(image_ref))
try:
self.docker_client.images.pull(full_ref)
break
except docker.errors.NotFound:
pass
container = self.docker_client.containers.run(
image_ref,
[self.handler, json.dumps(event)],
@ -1483,13 +1501,18 @@ class LambdaBackend(BaseBackend):
# 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:
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:
.. sourcecode:: bash
MOTO_DOCKER_LAMBDA_IMAGE=mLupin/docker-lambda
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.
.. note:: When using the decorators, a Docker container cannot reach Moto, as the Docker-container loses all mock-context. Any boto3-invocations used within your Lambda will try to connect to AWS.
"""
def __init__(self, region_name: str, account_id: str):

View File

@ -89,7 +89,7 @@ def moto_server_host() -> str:
def moto_lambda_image() -> str:
return os.environ.get("MOTO_DOCKER_LAMBDA_IMAGE", "lambci/lambda")
return os.environ.get("MOTO_DOCKER_LAMBDA_IMAGE", "mlupin/docker-lambda")
def moto_network_name() -> str:

View File

@ -123,7 +123,7 @@ def test_invoke_event_function():
function_name = str(uuid4())[0:6]
conn.create_function(
FunctionName=function_name,
Runtime="python2.7",
Runtime="python3.9",
Role=get_role_name(),
Handler="lambda_function.lambda_handler",
Code={"ZipFile": get_test_zip_file1()},