AWSLambda: Ability to customize Dockers' data dir (#6084)

This commit is contained in:
Bert Blommers 2023-03-18 09:35:48 -01:00 committed by GitHub
parent 5eef06ee51
commit 851f0c1181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 19 deletions

View File

@ -59,8 +59,6 @@ from tempfile import TemporaryDirectory
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
docker_3 = docker.__version__[0] >= "3"
def zip2tar(zip_bytes: bytes) -> bytes: def zip2tar(zip_bytes: bytes) -> bytes:
with TemporaryDirectory() as td: with TemporaryDirectory() as td:
@ -127,11 +125,7 @@ class _DockerDataVolumeContext:
self._vol_ref.volume = self._lambda_func.docker_client.volumes.create( self._vol_ref.volume = self._lambda_func.docker_client.volumes.create(
self._lambda_func.code_digest self._lambda_func.code_digest
) )
volumes = { volumes = {self.name: {"bind": settings.LAMBDA_DATA_DIR, "mode": "rw"}}
self.name: {"bind": "/tmp/data", "mode": "rw"}
if docker_3
else "/tmp/data"
}
self._lambda_func.docker_client.images.pull( self._lambda_func.docker_client.images.pull(
":".join(parse_image_ref("alpine")) ":".join(parse_image_ref("alpine"))
@ -141,7 +135,7 @@ class _DockerDataVolumeContext:
) )
try: try:
tar_bytes = zip2tar(self._lambda_func.code_bytes) 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: finally:
container.remove(force=True) container.remove(force=True)
@ -801,14 +795,11 @@ class LambdaFunction(CloudFormationModel, DockerModel):
finally: finally:
if container: if container:
try: try:
exit_code = container.wait(timeout=300) exit_code = container.wait(timeout=300)["StatusCode"]
except requests.exceptions.ReadTimeout: except requests.exceptions.ReadTimeout:
exit_code = -1 exit_code = -1
container.stop() container.stop()
container.kill() container.kill()
else:
if docker_3:
exit_code = exit_code["StatusCode"]
output = container.logs(stdout=False, stderr=True) output = container.logs(stdout=False, stderr=True)
output += container.logs(stdout=True, stderr=False) output += container.logs(stdout=True, stderr=False)
@ -1514,6 +1505,12 @@ class LambdaBackend(BaseBackend):
MOTO_DOCKER_LAMBDA_IMAGE=mlupin/docker-lambda 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. .. 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.
""" """

View File

@ -35,6 +35,8 @@ PRETTIFY_RESPONSES = bool(os.environ.get("MOTO_PRETTIFY_RESPONSES", False))
# Fully skip test that require docker # Fully skip test that require docker
SKIP_REQUIRES_DOCKER = bool(os.environ.get("TESTS_SKIP_REQUIRES_DOCKER", False)) 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(): def get_sf_execution_history_type():
""" """

View File

@ -44,7 +44,7 @@ include_package_data = True
all = all =
python-jose[cryptography]>=3.1.0,<4.0.0 python-jose[cryptography]>=3.1.0,<4.0.0
ecdsa!=0.15 ecdsa!=0.15
docker>=2.5.1 docker>=3.0.0
graphql-core graphql-core
PyYAML>=5.1 PyYAML>=5.1
cfn-lint>=0.40.0 cfn-lint>=0.40.0
@ -57,7 +57,7 @@ all =
server = server =
python-jose[cryptography]>=3.1.0,<4.0.0 python-jose[cryptography]>=3.1.0,<4.0.0
ecdsa!=0.15 ecdsa!=0.15
docker>=2.5.1 docker>=3.0.0
graphql-core graphql-core
PyYAML>=5.1 PyYAML>=5.1
cfn-lint>=0.40.0 cfn-lint>=0.40.0
@ -82,15 +82,15 @@ applicationautoscaling =
appsync = graphql-core appsync = graphql-core
athena = athena =
autoscaling = autoscaling =
awslambda = docker>=2.5.1 awslambda = docker>=3.0.0
batch = docker>=2.5.1 batch = docker>=3.0.0
batch_simple = batch_simple =
budgets = budgets =
ce = ce =
cloudformation = cloudformation =
python-jose[cryptography]>=3.1.0,<4.0.0 python-jose[cryptography]>=3.1.0,<4.0.0
ecdsa!=0.15 ecdsa!=0.15
docker>=2.5.1 docker>=3.0.0
graphql-core graphql-core
PyYAML>=5.1 PyYAML>=5.1
cfn-lint>=0.40.0 cfn-lint>=0.40.0
@ -118,8 +118,8 @@ datasync =
dax = dax =
dms = dms =
ds = sshpubkeys>=3.1.0 ds = sshpubkeys>=3.1.0
dynamodb = docker>=2.5.1 dynamodb = docker>=3.0.0
dynamodbstreams = docker>=2.5.1 dynamodbstreams = docker>=3.0.0
ebs = sshpubkeys>=3.1.0 ebs = sshpubkeys>=3.1.0
ec2 = sshpubkeys>=3.1.0 ec2 = sshpubkeys>=3.1.0
ec2instanceconnect = ec2instanceconnect =