From 851f0c1181313952f30a744ec87a2d9aa513ff96 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 18 Mar 2023 09:35:48 -0100 Subject: [PATCH] AWSLambda: Ability to customize Dockers' data dir (#6084) --- moto/awslambda/models.py | 21 +++++++++------------ moto/settings.py | 2 ++ setup.cfg | 14 +++++++------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index c5ca3f294..56d1ea985 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -59,8 +59,6 @@ from tempfile import TemporaryDirectory logger = logging.getLogger(__name__) -docker_3 = docker.__version__[0] >= "3" - def zip2tar(zip_bytes: bytes) -> bytes: with TemporaryDirectory() as td: @@ -127,11 +125,7 @@ class _DockerDataVolumeContext: self._vol_ref.volume = self._lambda_func.docker_client.volumes.create( self._lambda_func.code_digest ) - volumes = { - self.name: {"bind": "/tmp/data", "mode": "rw"} - if docker_3 - else "/tmp/data" - } + volumes = {self.name: {"bind": settings.LAMBDA_DATA_DIR, "mode": "rw"}} self._lambda_func.docker_client.images.pull( ":".join(parse_image_ref("alpine")) @@ -141,7 +135,7 @@ class _DockerDataVolumeContext: ) try: tar_bytes = zip2tar(self._lambda_func.code_bytes) - container.put_archive("/tmp/data", tar_bytes) + container.put_archive(settings.LAMBDA_DATA_DIR, tar_bytes) finally: container.remove(force=True) @@ -801,14 +795,11 @@ class LambdaFunction(CloudFormationModel, DockerModel): finally: if container: try: - exit_code = container.wait(timeout=300) + exit_code = container.wait(timeout=300)["StatusCode"] except requests.exceptions.ReadTimeout: exit_code = -1 container.stop() container.kill() - else: - if docker_3: - exit_code = exit_code["StatusCode"] output = container.logs(stdout=False, stderr=True) output += container.logs(stdout=True, stderr=False) @@ -1514,6 +1505,12 @@ class LambdaBackend(BaseBackend): 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: + + .. sourcecode:: bash + + MOTO_LAMBDA_DATA_DIR=/tmp/data + .. 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. """ diff --git a/moto/settings.py b/moto/settings.py index 865a791e0..ce0dace60 100644 --- a/moto/settings.py +++ b/moto/settings.py @@ -35,6 +35,8 @@ PRETTIFY_RESPONSES = bool(os.environ.get("MOTO_PRETTIFY_RESPONSES", False)) # Fully skip test that require docker SKIP_REQUIRES_DOCKER = bool(os.environ.get("TESTS_SKIP_REQUIRES_DOCKER", False)) +LAMBDA_DATA_DIR = os.environ.get("MOTO_LAMBDA_DATA_DIR", "/tmp/data") + def get_sf_execution_history_type(): """ diff --git a/setup.cfg b/setup.cfg index f4f1288a4..da18a49b3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,7 +44,7 @@ include_package_data = True all = python-jose[cryptography]>=3.1.0,<4.0.0 ecdsa!=0.15 - docker>=2.5.1 + docker>=3.0.0 graphql-core PyYAML>=5.1 cfn-lint>=0.40.0 @@ -57,7 +57,7 @@ all = server = python-jose[cryptography]>=3.1.0,<4.0.0 ecdsa!=0.15 - docker>=2.5.1 + docker>=3.0.0 graphql-core PyYAML>=5.1 cfn-lint>=0.40.0 @@ -82,15 +82,15 @@ applicationautoscaling = appsync = graphql-core athena = autoscaling = -awslambda = docker>=2.5.1 -batch = docker>=2.5.1 +awslambda = docker>=3.0.0 +batch = docker>=3.0.0 batch_simple = budgets = ce = cloudformation = python-jose[cryptography]>=3.1.0,<4.0.0 ecdsa!=0.15 - docker>=2.5.1 + docker>=3.0.0 graphql-core PyYAML>=5.1 cfn-lint>=0.40.0 @@ -118,8 +118,8 @@ datasync = dax = dms = ds = sshpubkeys>=3.1.0 -dynamodb = docker>=2.5.1 -dynamodbstreams = docker>=2.5.1 +dynamodb = docker>=3.0.0 +dynamodbstreams = docker>=3.0.0 ebs = sshpubkeys>=3.1.0 ec2 = sshpubkeys>=3.1.0 ec2instanceconnect =