Skip to content

Commit 6c8216c

Browse files
committed
AWSLambda: Use shogo82148 as the default image provider
1 parent 0a9c7bf commit 6c8216c

12 files changed

Lines changed: 44 additions & 45 deletions

moto/awslambda/models.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,6 @@ def convert(s: Any) -> str: # type: ignore[misc]
964964
return s
965965

966966
def _invoke_lambda(self, event: str | None = None) -> tuple[str, bool, str]:
967-
import docker
968967
import docker.errors
969968

970969
# Create the LogGroup if necessary, to write the result to
@@ -1000,6 +999,10 @@ def _invoke_lambda(self, event: str | None = None) -> tuple[str, bool, str]:
1000999
if settings.is_test_proxy_mode():
10011000
env_vars["HTTPS_PROXY"] = env_vars["MOTO_HTTP_ENDPOINT"]
10021001
env_vars["AWS_CA_BUNDLE"] = "/var/task/ca.crt"
1002+
else:
1003+
# When the proxy is active, we should keep the original URL
1004+
# Else: redirect requests to our server
1005+
env_vars["AWS_ENDPOINT_URL"] = env_vars["MOTO_HTTP_ENDPOINT"]
10031006

10041007
container = exit_code = None
10051008
log_config = docker.types.LogConfig(type=docker.types.LogConfig.types.JSON)
@@ -1017,9 +1020,8 @@ def _invoke_lambda(self, event: str | None = None) -> tuple[str, bool, str]:
10171020
elif network_mode:
10181021
run_kwargs["network_mode"] = network_mode
10191022
elif settings.TEST_SERVER_MODE:
1020-
# AWSLambda can make HTTP requests to a Docker container called 'motoserver'
10211023
# Only works if our Docker-container is named 'motoserver'
1022-
# TODO: should remove this and rely on 'network_mode' instead, as this is too tightly coupled with our own test setup
1024+
# TODO: should remove this in 6.x - it's no longer necessary for internal tests, but users may rely on it
10231025
run_kwargs["links"] = {"motoserver": "motoserver"}
10241026

10251027
# add host.docker.internal host on linux to emulate Mac + Windows behavior
@@ -1039,12 +1041,19 @@ def _invoke_lambda(self, event: str | None = None) -> tuple[str, bool, str]:
10391041
# - lambci/lambda (the repo with older/outdated AWSLambda images
10401042
#
10411043
# We'll cycle through all of them - when we find the repo that contains our image, we use it
1044+
1045+
idx = re.search("[0-9]", self.run_time).start() # type: ignore[union-attr,arg-type]
1046+
language, version = self.run_time[:idx], self.run_time[idx:] # type: ignore[index]
1047+
10421048
image_repos = { # dict maintains insertion order
10431049
settings.moto_lambda_image(): None,
1050+
f"ghcr.io/shogo82148/lambda-{language}:{version}": None,
10441051
"mlupin/docker-lambda": None,
10451052
"lambci/lambda": None,
10461053
}
10471054
for image_repo in image_repos:
1055+
if not image_repo:
1056+
continue
10481057
image_ref = (
10491058
image_repo
10501059
if ":" in image_repo
@@ -1807,7 +1816,7 @@ def del_function(self, name_or_arn: str, qualifier: str | None = None) -> None:
18071816
else:
18081817
self._functions[name]["versions"].remove(function)
18091818

1810-
# If theres no functions left
1819+
# If there are no functions left
18111820
if (
18121821
not self._functions[name]["versions"]
18131822
and not self._functions[name]["latest"]
@@ -1903,9 +1912,11 @@ class LambdaBackend(BaseBackend):
19031912
19041913
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.
19051914
1906-
When running the MotoProxy, calls to other AWS services are automatically proxied.
1915+
Moto injects the `AWS_ENDPOINT_URL` environment variable into every Lambda, which means that calls to other AWS services are automatically intercepted.
1916+
1917+
Note that, if you use a non-standard or older Docker image, the `AWS_ENDPOINT_URL` may not be supported.
19071918
1908-
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:
1919+
If that is the case, use the environment variables `MOTO_HOST` and `MOTO_PORT` to build the AWS endpoint url and point it to Moto:
19091920
19101921
.. sourcecode:: python
19111922
@@ -1945,8 +1956,9 @@ def lambda_handler(event, context):
19451956
19461957
The Docker images used by Moto are taken from the following repositories:
19471958
1948-
- `mlupin/docker-lambda` (for recent versions)
1949-
- `lambci/lambda` (for older/outdated versions)
1959+
- `shogo82148/lambda-python` (for recent versions)
1960+
- `mlupin/docker-lambda` (for older/outdated versions)
1961+
- `lambci/lambda` (for even older/outdated versions)
19501962
19511963
Use the following environment variable to configure Moto to look for images in an additional repository:
19521964

moto/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def moto_server_host() -> str:
125125
return "http://host.docker.internal"
126126

127127

128-
def moto_lambda_image() -> str:
129-
return os.environ.get("MOTO_DOCKER_LAMBDA_IMAGE", "mlupin/docker-lambda")
128+
def moto_lambda_image() -> str | None:
129+
return os.environ.get("MOTO_DOCKER_LAMBDA_IMAGE")
130130

131131

132132
def moto_network_name() -> str | None:

tests/test_awslambda/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from moto import mock_aws
1111

12+
from .utilities import PYTHON_VERSION
13+
1214

1315
def lambda_aws_verified(func):
1416
"""
@@ -98,7 +100,7 @@ def check_iam_role_is_ready_to_use(role_arn: str, role_name: str, region: str):
98100
fn_name = f"fn_for_{role_name}"
99101
_lambda.create_function(
100102
FunctionName=fn_name,
101-
Runtime="python3.11",
103+
Runtime=PYTHON_VERSION,
102104
Role=role_arn,
103105
Handler="lambda_function.lambda_handler",
104106
Code={"ZipFile": get_test_zip_file1()},

tests/test_awslambda/test_lambda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
from . import lambda_aws_verified
2121
from .utilities import (
22+
PYTHON_VERSION,
2223
_process_lambda,
2324
create_invalid_lambda,
2425
get_role_name,
2526
get_test_zip_file1,
2627
get_test_zip_file2,
2728
)
2829

29-
PYTHON_VERSION = "python3.11"
3030
LAMBDA_FUNC_NAME = "test"
3131
_lambda_region = "us-west-2"
3232

tests/test_awslambda/test_lambda_alias.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
1111

1212
from .utilities import (
13+
PYTHON_VERSION,
1314
get_role_name,
1415
get_test_zip_file1,
1516
get_test_zip_file2,
1617
)
1718

1819
# See our Development Tips on writing tests for hints on how to write good tests:
1920
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html
20-
PYTHON_VERSION = "python3.11"
2121

2222

2323
@mock_aws

tests/test_awslambda/test_lambda_concurrency.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
from moto import mock_aws
77

8-
from .utilities import get_role_name, get_test_zip_file1
8+
from .utilities import PYTHON_VERSION, get_role_name, get_test_zip_file1
99

10-
PYTHON_VERSION = "python3.11"
1110
_lambda_region = "us-west-2"
1211

1312

tests/test_awslambda/test_lambda_eventsourcemapping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
from ..markers import requires_docker
1616
from .utilities import (
17+
PYTHON_VERSION,
1718
get_role_name,
1819
get_test_zip_file3,
1920
get_test_zip_file_error,
2021
wait_for_log_msg,
2122
)
2223

23-
PYTHON_VERSION = "python3.11"
2424
_lambda_region = "us-west-2"
2525
botocore_version = sys.modules["botocore"].__version__
2626

@@ -676,7 +676,7 @@ def test_event_source_mapping_tagging_lifecycle():
676676
client = boto3.client("lambda", region_name="us-east-1")
677677
client.create_function(
678678
FunctionName="any-function-name",
679-
Runtime="python3.6",
679+
Runtime=PYTHON_VERSION,
680680
Role=iam_role["Role"]["Arn"],
681681
Handler="any-handler",
682682
Code={

tests/test_awslambda/test_lambda_invoke.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..markers import requires_docker
1414
from .test_lambda import LooseVersion, boto3_version
1515
from .utilities import (
16+
PYTHON_VERSION,
1617
get_lambda_using_environment_port,
1718
get_lambda_using_network_mode,
1819
get_proxy_zip_file,
@@ -24,7 +25,6 @@
2425
get_zip_with_multiple_files,
2526
)
2627

27-
PYTHON_VERSION = "python3.11"
2828
_lambda_region = "us-west-2"
2929

3030

@@ -278,7 +278,7 @@ def test_invoke_function_with_multiple_files_in_zip():
278278
}
279279

280280

281-
if settings.TEST_SERVER_MODE:
281+
if not settings.TEST_DECORATOR_MODE:
282282

283283
@mock_aws
284284
def test_invoke_function_get_ec2_volume():

tests/test_awslambda/test_lambda_layers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
from tests.test_awslambda import delete_all_layer_versions
1515
from tests.test_s3 import s3_aws_verified
1616

17-
from .utilities import get_role_name, get_test_zip_file1
17+
from .utilities import PYTHON_VERSION, get_role_name, get_test_zip_file1
1818

19-
PYTHON_VERSION = "python3.11"
2019
_lambda_region = "us-west-2"
2120

2221
boto3_version = sys.modules["botocore"].__version__

tests/test_awslambda/test_lambda_layers_invoked.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
from moto import mock_aws
88
from tests.markers import requires_docker
99

10-
from .utilities import _process_lambda, get_role_name
10+
from .utilities import PYTHON_VERSION, _process_lambda, get_role_name
1111

12-
PYTHON_VERSION = "python3.11"
1312
_lambda_region = "us-west-2"
1413

1514

@@ -56,7 +55,7 @@ def test_invoke_local_lambda_layers():
5655

5756
function_arn = conn.create_function(
5857
FunctionName=lambda_name,
59-
Runtime="python3.11",
58+
Runtime=PYTHON_VERSION,
6059
Role=get_role_name(),
6160
Handler="lambda_function.lambda_handler",
6261
Code={"ZipFile": get_requests_zip_file()},

0 commit comments

Comments
 (0)