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__)
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.
"""

View File

@ -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():
"""

View File

@ -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 =