-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
lambda_executors.py
1690 lines (1433 loc) 路 67.4 KB
/
lambda_executors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import contextlib
import dataclasses
import glob
import json
import logging
import os
import re
import shlex
import subprocess
import sys
import tempfile
import threading
import time
import traceback
import uuid
from multiprocessing import Process, Queue
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from localstack import config
from localstack.config import LAMBDA_TRUNCATE_STDOUT, TMP_FOLDER
from localstack.constants import DEFAULT_LAMBDA_CONTAINER_REGISTRY
from localstack.runtime.hooks import hook_spec
from localstack.services.awslambda.lambda_utils import (
API_PATH_ROOT,
LAMBDA_RUNTIME_PROVIDED,
get_container_network_for_lambda,
get_main_endpoint_from_container,
get_record_from_event,
is_java_lambda,
is_nodejs_runtime,
rm_docker_container,
store_lambda_logs,
)
from localstack.services.install import GO_LAMBDA_RUNTIME, INSTALL_PATH_LOCALSTACK_FAT_JAR
from localstack.utils import bootstrap
from localstack.utils.aws import aws_stack
from localstack.utils.aws.aws_models import LambdaFunction
from localstack.utils.aws.dead_letter_queue import (
lambda_error_to_dead_letter_queue,
sqs_error_to_dead_letter_queue,
)
from localstack.utils.cloudwatch.cloudwatch_util import cloudwatched
from localstack.utils.collections import select_attributes
from localstack.utils.common import (
TMP_FILES,
CaptureOutput,
get_all_subclasses,
get_free_tcp_port,
in_docker,
is_port_open,
json_safe,
last_index_of,
long_uid,
md5,
now,
retry,
run,
run_safe,
safe_requests,
save_file,
short_uid,
timestamp,
to_bytes,
to_str,
truncate,
wait_for_port_open,
)
from localstack.utils.container_utils.container_client import (
ContainerConfiguration,
ContainerException,
DockerContainerStatus,
PortMappings,
)
from localstack.utils.docker_utils import DOCKER_CLIENT
from localstack.utils.run import FuncThread
# constants
LAMBDA_EXECUTOR_JAR = INSTALL_PATH_LOCALSTACK_FAT_JAR
LAMBDA_EXECUTOR_CLASS = "cloud.localstack.LambdaExecutor"
LAMBDA_HANDLER_ENV_VAR_NAME = "_HANDLER"
EVENT_FILE_PATTERN = "%s/lambda.event.*.json" % config.dirs.tmp
LAMBDA_SERVER_UNIQUE_PORTS = 500
LAMBDA_SERVER_PORT_OFFSET = 5000
LAMBDA_API_UNIQUE_PORTS = 500
LAMBDA_API_PORT_OFFSET = 9000
MAX_ENV_ARGS_LENGTH = 20000
# port number used in lambci images for stay-open invocation mode
STAY_OPEN_API_PORT = 9001
INTERNAL_LOG_PREFIX = "ls-daemon: "
# logger
LOG = logging.getLogger(__name__)
# maximum time a pre-allocated container can sit idle before getting killed
MAX_CONTAINER_IDLE_TIME_MS = 600 * 1000
# SQS event source name
EVENT_SOURCE_SQS = "aws:sqs"
# maps lambda arns to concurrency locks
LAMBDA_CONCURRENCY_LOCK = {}
# CWD folder of handler code in Lambda containers
DOCKER_TASK_FOLDER = "/var/task"
# TODO remove once clarification of apigateway contexts is complete. Really bad hack!!
ALLOWED_IDENTITY_FIELDS = ["cognitoIdentityId", "cognitoIdentityPoolId"]
# Lambda event type
LambdaEvent = Union[Dict[str, Any], str, bytes]
# Hook definitions
HOOKS_ON_LAMBDA_DOCKER_SEPARATE_EXECUTION = "localstack.hooks.on_docker_separate_execution"
HOOKS_ON_LAMBDA_DOCKER_REUSE_CONTAINER_CREATION = (
"localstack.hooks.on_docker_reuse_container_creation"
)
on_docker_separate_execution = hook_spec(HOOKS_ON_LAMBDA_DOCKER_SEPARATE_EXECUTION)
on_docker_reuse_container_creation = hook_spec(HOOKS_ON_LAMBDA_DOCKER_REUSE_CONTAINER_CREATION)
class InvocationException(Exception):
def __init__(self, message, log_output=None, result=None):
super(InvocationException, self).__init__(message)
self.log_output = log_output
self.result = result
class LambdaContext:
DEFAULT_MEMORY_LIMIT = 1536
def __init__(
self, lambda_function: LambdaFunction, qualifier: str = None, context: Dict[str, Any] = None
):
context = context or {}
self.function_name = lambda_function.name()
self.function_version = lambda_function.get_qualifier_version(qualifier)
self.client_context = context.get("client_context")
self.invoked_function_arn = lambda_function.arn()
if qualifier:
self.invoked_function_arn += ":" + qualifier
self.cognito_identity = context.get("identity") and select_attributes(
context.get("identity"), ALLOWED_IDENTITY_FIELDS
)
self.aws_request_id = str(uuid.uuid4())
self.memory_limit_in_mb = lambda_function.memory_size or self.DEFAULT_MEMORY_LIMIT
self.log_group_name = f"/aws/lambda/{self.function_name}"
self.log_stream_name = f"{timestamp(format='%Y/%m/%d')}/[1]{short_uid()}"
def get_remaining_time_in_millis(self):
# TODO implement!
return 1000 * 60
class AdditionalInvocationOptions:
# Maps file keys to file paths. The keys can be used as placeholders in the env. variables
# and command args to reference files - e.g., given `files_to_add` as {"f1": "/local/path"} and
# `env_updates` as {"MYENV": "{f1}"}, the Lambda handler will receive an environment variable
# `MYENV=/lambda/path` and the file /lambda/path will be accessible to the Lambda handler
# (either locally, or inside Docker).
files_to_add: Dict[str, str]
# Environment variable updates to apply for the invocation
env_updates: Dict[str, str]
# Updated command to use for starting the Lambda process (or None)
updated_command: Optional[str]
# Updated handler as entry point of Lambda function (or None)
updated_handler: Optional[str]
def __init__(
self,
files_to_add=None,
env_updates=None,
updated_command=None,
updated_handler=None,
):
self.files_to_add = files_to_add or {}
self.env_updates = env_updates or {}
self.updated_command = updated_command
self.updated_handler = updated_handler
class InvocationResult:
def __init__(self, result, log_output=""):
if isinstance(result, InvocationResult):
raise Exception("Unexpected invocation result type: %s" % result)
self.result = result
self.log_output = log_output or ""
class InvocationContext:
lambda_function: LambdaFunction
function_version: str
handler: str
event: LambdaEvent
lambda_command: Union[str, List[str]] # TODO: change to List[str] ?
docker_flags: Union[str, List[str]] # TODO: change to List[str] ?
environment: Dict[str, Optional[str]]
context: LambdaContext
invocation_type: str # "Event" or "RequestResponse"
def __init__(
self,
lambda_function: LambdaFunction,
event: LambdaEvent,
environment=None,
context=None,
lambda_command=None,
docker_flags=None,
function_version=None,
invocation_type=None,
):
self.lambda_function = lambda_function
self.handler = lambda_function.handler
self.event = event
self.environment = {} if environment is None else environment
self.context = {} if context is None else context
self.lambda_command = lambda_command
self.docker_flags = docker_flags
self.function_version = function_version
self.invocation_type = invocation_type
class LambdaExecutorPlugin:
"""Plugin abstraction that allows to hook in additional functionality into the Lambda executors."""
INSTANCES: List["LambdaExecutorPlugin"] = []
def initialize(self):
"""Called once, for any active plugin to run initialization logic (e.g., downloading dependencies).
Uses lazy initialization - i.e., runs only after the first should_apply() call returns True"""
pass
def should_apply(self, context: InvocationContext) -> bool:
"""Whether the plugin logic should get applied for the given Lambda invocation context."""
return False
def prepare_invocation(
self, context: InvocationContext
) -> Optional[Union[AdditionalInvocationOptions, InvocationResult]]:
"""Return additional invocation options for given Lambda invocation context. Optionally, an
InvocationResult can be returned, in which case the result is returned to the client right away."""
return None
def process_result(
self, context: InvocationContext, result: InvocationResult
) -> InvocationResult:
"""Optionally modify the result returned from the given Lambda invocation."""
return result
def init_function_configuration(self, lambda_function: LambdaFunction):
"""Initialize the configuration of the given function upon creation or function update."""
pass
def init_function_code(self, lambda_function: LambdaFunction):
"""Initialize the code of the given function upon creation or function update."""
pass
@classmethod
def get_plugins(cls) -> List["LambdaExecutorPlugin"]:
if not cls.INSTANCES:
classes = get_all_subclasses(LambdaExecutorPlugin)
cls.INSTANCES = [clazz() for clazz in classes]
return cls.INSTANCES
class LambdaInvocationForwarderPlugin(LambdaExecutorPlugin):
"""Plugin that forwards Lambda invocations to external targets defined in LAMBDA_FORWARD_URL"""
def should_apply(self, context: InvocationContext) -> bool:
"""If LAMBDA_FORWARD_URL is configured, forward the invocation of this Lambda to the target URL."""
func_forward_url = self._forward_url(context)
return bool(func_forward_url)
def prepare_invocation(
self, context: InvocationContext
) -> Optional[Union[AdditionalInvocationOptions, InvocationResult]]:
forward_url = self._forward_url(context)
result = self._forward_to_url(
forward_url,
context.lambda_function,
context.event,
context.context,
context.invocation_type,
)
return result
def _forward_to_url(
self,
forward_url: str,
lambda_function: LambdaFunction,
event: Union[Dict, bytes],
context: LambdaContext,
invocation_type: str,
) -> InvocationResult:
func_name = lambda_function.name()
url = "%s%s/functions/%s/invocations" % (forward_url, API_PATH_ROOT, func_name)
copied_env_vars = lambda_function.envvars.copy()
copied_env_vars["LOCALSTACK_HOSTNAME"] = config.HOSTNAME_EXTERNAL
copied_env_vars["LOCALSTACK_EDGE_PORT"] = str(config.EDGE_PORT)
headers = aws_stack.mock_aws_request_headers("lambda")
headers["X-Amz-Region"] = lambda_function.region()
headers["X-Amz-Request-Id"] = context.aws_request_id
headers["X-Amz-Handler"] = lambda_function.handler
headers["X-Amz-Function-ARN"] = context.invoked_function_arn
headers["X-Amz-Function-Name"] = context.function_name
headers["X-Amz-Function-Version"] = context.function_version
headers["X-Amz-Role"] = lambda_function.role
headers["X-Amz-Runtime"] = lambda_function.runtime
headers["X-Amz-Timeout"] = str(lambda_function.timeout)
headers["X-Amz-Memory-Size"] = str(context.memory_limit_in_mb)
headers["X-Amz-Log-Group-Name"] = context.log_group_name
headers["X-Amz-Log-Stream-Name"] = context.log_stream_name
headers["X-Amz-Env-Vars"] = json.dumps(copied_env_vars)
headers["X-Amz-Last-Modified"] = str(int(lambda_function.last_modified.timestamp() * 1000))
headers["X-Amz-Invocation-Type"] = invocation_type
headers["X-Amz-Log-Type"] = "Tail"
if context.client_context:
headers["X-Amz-Client-Context"] = context.client_context
if context.cognito_identity:
headers["X-Amz-Cognito-Identity"] = context.cognito_identity
data = run_safe(lambda: to_str(event)) or event
data = json.dumps(json_safe(data)) if isinstance(data, dict) else str(data)
LOG.debug(
"Forwarding Lambda invocation to LAMBDA_FORWARD_URL: %s", config.LAMBDA_FORWARD_URL
)
result = safe_requests.post(url, data, headers=headers)
if result.status_code >= 400:
raise Exception(
"Received error status code %s from external Lambda invocation" % result.status_code
)
content = run_safe(lambda: to_str(result.content)) or result.content
LOG.debug(
"Received result from external Lambda endpoint (status %s): %s",
result.status_code,
content,
)
result = InvocationResult(content)
return result
def _forward_url(self, context: InvocationContext) -> str:
env_vars = context.lambda_function.envvars
return env_vars.get("LOCALSTACK_LAMBDA_FORWARD_URL") or config.LAMBDA_FORWARD_URL
def handle_error(
lambda_function: LambdaFunction, event: Dict, error: Exception, asynchronous: bool = False
):
if asynchronous:
if get_record_from_event(event, "eventSource") == EVENT_SOURCE_SQS:
sqs_queue_arn = get_record_from_event(event, "eventSourceARN")
if sqs_queue_arn:
# event source is SQS, send event back to dead letter queue
return sqs_error_to_dead_letter_queue(sqs_queue_arn, event, error)
else:
# event source is not SQS, send back to lambda dead letter queue
lambda_error_to_dead_letter_queue(lambda_function, event, error)
class LambdaAsyncLocks:
locks: Dict[str, Union[threading.Semaphore, threading.Lock]]
creation_lock: threading.Lock
def __init__(self):
self.locks = {}
self.creation_lock = threading.Lock()
def assure_lock_present(
self, key: str, lock: Union[threading.Semaphore, threading.Lock]
) -> Union[threading.Semaphore, threading.Lock]:
with self.creation_lock:
return self.locks.setdefault(key, lock)
LAMBDA_ASYNC_LOCKS = LambdaAsyncLocks()
class LambdaExecutor:
"""Base class for Lambda executors. Subclasses must overwrite the _execute method"""
def __init__(self):
# keeps track of each function arn and the last time it was invoked
self.function_invoke_times = {}
def _prepare_environment(self, lambda_function: LambdaFunction):
# setup environment pre-defined variables for docker environment
result = lambda_function.envvars.copy()
# injecting aws credentials into docker environment if not provided
aws_stack.inject_test_credentials_into_env(result)
# injecting the region into the docker environment
aws_stack.inject_region_into_env(result, lambda_function.region())
return result
def execute(
self,
func_arn: str, # TODO remove and get from lambda_function
lambda_function: LambdaFunction,
event: Dict,
context: LambdaContext = None,
version: str = None,
asynchronous: bool = False,
callback: Callable = None,
lock_discriminator: str = None,
):
# note: leave here to avoid circular import issues
from localstack.utils.aws.message_forwarding import lambda_result_to_destination
def do_execute(*args):
@cloudwatched("lambda")
def _run(func_arn=None):
with contextlib.ExitStack() as stack:
if lock_discriminator:
stack.enter_context(LAMBDA_ASYNC_LOCKS.locks[lock_discriminator])
# set the invocation time in milliseconds
invocation_time = int(time.time() * 1000)
# start the execution
raised_error = None
result = None
dlq_sent = None
invocation_type = "Event" if asynchronous else "RequestResponse"
inv_context = InvocationContext(
lambda_function,
event=event,
function_version=version,
context=context,
invocation_type=invocation_type,
)
try:
result = self._execute(lambda_function, inv_context)
except Exception as e:
raised_error = e
dlq_sent = handle_error(lambda_function, event, e, asynchronous)
raise e
finally:
self.function_invoke_times[func_arn] = invocation_time
callback and callback(
result, func_arn, event, error=raised_error, dlq_sent=dlq_sent
)
lambda_result_to_destination(
lambda_function, event, result, asynchronous, raised_error
)
# return final result
return result
return _run(func_arn=func_arn)
# Inform users about asynchronous mode of the lambda execution.
if asynchronous:
LOG.debug(
"Lambda executed in Event (asynchronous) mode, no response will be returned to caller"
)
FuncThread(do_execute).start()
return InvocationResult(None, log_output="Lambda executed asynchronously.")
return do_execute()
def _execute(self, lambda_function: LambdaFunction, inv_context: InvocationContext):
"""This method must be overwritten by subclasses."""
raise NotImplementedError
def startup(self):
"""Called once during startup - can be used, e.g., to prepare Lambda Docker environment"""
pass
def cleanup(self, arn=None):
"""Called once during startup - can be used, e.g., to clean up left-over Docker containers"""
pass
def provide_file_to_lambda(self, local_file: str, inv_context: InvocationContext) -> str:
"""Make the given file available to the Lambda process (e.g., by copying into the container) for the
given invocation context; Returns the path to the file that will be available to the Lambda handler."""
raise NotImplementedError
def apply_plugin_patches(self, inv_context: InvocationContext) -> Optional[InvocationResult]:
"""Loop through the list of plugins, and apply their patches to the invocation context (if applicable)"""
invocation_results = []
for plugin in LambdaExecutorPlugin.get_plugins():
if not plugin.should_apply(inv_context):
continue
# initialize, if not done yet
if not hasattr(plugin, "_initialized"):
LOG.debug("Initializing Lambda executor plugin %s", plugin.__class__)
plugin.initialize()
plugin._initialized = True
# invoke plugin to prepare invocation
inv_options = plugin.prepare_invocation(inv_context)
if not inv_options:
continue
if isinstance(inv_options, InvocationResult):
invocation_results.append(inv_options)
continue
# copy files
file_keys_map = {}
for key, file_path in inv_options.files_to_add.items():
file_in_container = self.provide_file_to_lambda(file_path, inv_context)
file_keys_map[key] = file_in_container
# replace placeholders like "{<fileKey>}" with corresponding file path
for key, file_path in file_keys_map.items():
for env_key, env_value in inv_options.env_updates.items():
inv_options.env_updates[env_key] = str(env_value).replace(
"{%s}" % key, file_path
)
if inv_options.updated_command:
inv_options.updated_command = inv_options.updated_command.replace(
"{%s}" % key, file_path
)
inv_context.lambda_command = inv_options.updated_command
# update environment
inv_context.environment.update(inv_options.env_updates)
# update handler
if inv_options.updated_handler:
inv_context.handler = inv_options.updated_handler
if invocation_results:
# TODO: This is currently indeterministic! If multiple execution plugins attempt to return
# an invocation result right away, only the first one is returned. We need a more solid
# mechanism for conflict resolution if multiple plugins interfere!
if len(invocation_results) > 1:
LOG.warning(
"Multiple invocation results returned from "
"LambdaExecutorPlugin.prepare_invocation calls - choosing the first one: %s",
invocation_results,
)
return invocation_results[0]
def process_result_via_plugins(
self, inv_context: InvocationContext, invocation_result: InvocationResult
) -> InvocationResult:
"""Loop through the list of plugins, and apply their post-processing logic to the Lambda invocation result."""
for plugin in LambdaExecutorPlugin.get_plugins():
if not plugin.should_apply(inv_context):
continue
invocation_result = plugin.process_result(inv_context, invocation_result)
return invocation_result
class ContainerInfo:
"""Contains basic information about a docker container."""
def __init__(self, name, entry_point):
self.name = name
self.entry_point = entry_point
@dataclasses.dataclass
class LambdaContainerConfiguration(ContainerConfiguration):
# Files required present in the container for lambda execution
required_files: List[Tuple[str, str]] = dataclasses.field(default_factory=list)
class LambdaExecutorContainers(LambdaExecutor):
"""Abstract executor class for executing Lambda functions in Docker containers"""
def execute_in_container(
self,
lambda_function: LambdaFunction,
inv_context: InvocationContext,
stdin=None,
background=False,
) -> Tuple[bytes, bytes]:
raise NotImplementedError
def run_lambda_executor(self, lambda_function: LambdaFunction, inv_context: InvocationContext):
env_vars = inv_context.environment
runtime = lambda_function.runtime or ""
event = inv_context.event
stdin_str = None
event_body = event if event is not None else env_vars.get("AWS_LAMBDA_EVENT_BODY")
event_body = json.dumps(event_body) if isinstance(event_body, dict) else event_body
event_body = event_body or ""
is_large_event = len(event_body) > MAX_ENV_ARGS_LENGTH
is_provided = runtime.startswith(LAMBDA_RUNTIME_PROVIDED)
if (
not is_large_event
and lambda_function
and is_provided
and env_vars.get("DOCKER_LAMBDA_USE_STDIN") == "1"
):
# Note: certain "provided" runtimes (e.g., Rust programs) can block if we pass in
# the event payload via stdin, hence we rewrite the command to "echo ... | ..." below
env_updates = {
"AWS_LAMBDA_EVENT_BODY": to_str(
event_body
), # Note: seems to be needed for provided runtimes!
"DOCKER_LAMBDA_USE_STDIN": "1",
}
env_vars.update(env_updates)
# Note: $AWS_LAMBDA_COGNITO_IDENTITY='{}' causes Rust Lambdas to hang
env_vars.pop("AWS_LAMBDA_COGNITO_IDENTITY", None)
if is_large_event:
# in case of very large event payloads, we need to pass them via stdin
LOG.debug(
"Received large Lambda event payload (length %s) - passing via stdin",
len(event_body),
)
env_vars["DOCKER_LAMBDA_USE_STDIN"] = "1"
if env_vars.get("DOCKER_LAMBDA_USE_STDIN") == "1":
stdin_str = event_body
if not is_provided:
env_vars.pop("AWS_LAMBDA_EVENT_BODY", None)
elif "AWS_LAMBDA_EVENT_BODY" not in env_vars:
env_vars["AWS_LAMBDA_EVENT_BODY"] = to_str(event_body)
# apply plugin patches
result = self.apply_plugin_patches(inv_context)
if isinstance(result, InvocationResult):
return result
if config.LAMBDA_DOCKER_FLAGS:
inv_context.docker_flags = (
f"{config.LAMBDA_DOCKER_FLAGS} {inv_context.docker_flags or ''}".strip()
)
event_stdin_bytes = stdin_str and to_bytes(stdin_str)
error = None
try:
result, log_output = self.execute_in_container(
lambda_function,
inv_context,
stdin=event_stdin_bytes,
)
except ContainerException as e:
result = e.stdout or ""
log_output = e.stderr or ""
error = e
except InvocationException as e:
result = e.result or ""
log_output = e.log_output or ""
error = e
try:
result = to_str(result).strip()
except Exception:
pass
log_output = to_str(log_output).strip()
# Note: The user's code may have been logging to stderr, in which case the logs
# will be part of the "result" variable here. Hence, make sure that we extract
# only the *last* line of "result" and consider anything above that as log output.
if isinstance(result, str) and "\n" in result:
lines = result.split("\n")
idx = last_index_of(
lines, lambda line: line and not line.startswith(INTERNAL_LOG_PREFIX)
)
if idx >= 0:
result = lines[idx]
additional_logs = "\n".join(lines[:idx] + lines[idx + 1 :])
log_output += "\n%s" % additional_logs
func_arn = lambda_function and lambda_function.arn()
output = OutputLog(result, log_output)
LOG.debug(
f"Lambda {func_arn} result / log output:"
f"\n{output.stdout_formatted()}"
f"\n>{output.stderr_formatted()}"
)
# store log output - TODO get live logs from `process` above?
store_lambda_logs(lambda_function, log_output)
if error:
output.output_file()
raise InvocationException(
"Lambda process returned with error. Result: %s. Output:\n%s"
% (result, log_output),
log_output,
result,
) from error
# create result
invocation_result = InvocationResult(result, log_output=log_output)
# run plugins post-processing logic
invocation_result = self.process_result_via_plugins(inv_context, invocation_result)
return invocation_result
def prepare_event(self, environment: Dict, event_body: str) -> bytes:
"""Return the event as a stdin string."""
# amend the environment variables for execution
environment["AWS_LAMBDA_EVENT_BODY"] = event_body
return event_body.encode()
def _execute(self, lambda_function: LambdaFunction, inv_context: InvocationContext):
runtime = lambda_function.runtime
handler = lambda_function.handler
environment = inv_context.environment = self._prepare_environment(lambda_function)
event = inv_context.event
context = inv_context.context
# configure USE_SSL in environment
if config.USE_SSL:
environment["USE_SSL"] = "1"
# prepare event body
if not event:
LOG.info(
'Empty event body specified for invocation of Lambda "%s"', lambda_function.arn()
)
event = {}
event_body = json.dumps(json_safe(event))
event_bytes_for_stdin = self.prepare_event(environment, event_body)
inv_context.event = event_bytes_for_stdin
Util.inject_endpoints_into_env(environment)
environment["EDGE_PORT"] = str(config.EDGE_PORT)
environment[LAMBDA_HANDLER_ENV_VAR_NAME] = handler
if os.environ.get("HTTP_PROXY"):
environment["HTTP_PROXY"] = os.environ["HTTP_PROXY"]
if lambda_function.timeout:
environment["AWS_LAMBDA_FUNCTION_TIMEOUT"] = str(lambda_function.timeout)
if context:
environment["AWS_LAMBDA_FUNCTION_NAME"] = context.function_name
environment["AWS_LAMBDA_FUNCTION_VERSION"] = context.function_version
environment["AWS_LAMBDA_FUNCTION_INVOKED_ARN"] = context.invoked_function_arn
if context.cognito_identity:
environment["AWS_LAMBDA_COGNITO_IDENTITY"] = json.dumps(context.cognito_identity)
if context.client_context is not None:
environment["AWS_LAMBDA_CLIENT_CONTEXT"] = json.dumps(
to_str(base64.b64decode(to_bytes(context.client_context)))
)
# pass JVM options to the Lambda environment, if configured
if config.LAMBDA_JAVA_OPTS and is_java_lambda(runtime):
if environment.get("JAVA_TOOL_OPTIONS"):
LOG.info(
"Skip setting LAMBDA_JAVA_OPTS as JAVA_TOOL_OPTIONS already defined in Lambda env vars"
)
else:
LOG.debug(
"Passing JVM options to container environment: JAVA_TOOL_OPTIONS=%s",
config.LAMBDA_JAVA_OPTS,
)
environment["JAVA_TOOL_OPTIONS"] = config.LAMBDA_JAVA_OPTS
# accept any self-signed certificates for outgoing calls from the Lambda
if is_nodejs_runtime(runtime):
environment["NODE_TLS_REJECT_UNAUTHORIZED"] = "0"
# run Lambda executor and fetch invocation result
LOG.info("Running lambda: %s", lambda_function.arn())
result = self.run_lambda_executor(lambda_function=lambda_function, inv_context=inv_context)
return result
def provide_file_to_lambda(self, local_file: str, inv_context: InvocationContext) -> str:
if config.LAMBDA_REMOTE_DOCKER:
LOG.info("TODO: copy file into container for LAMBDA_REMOTE_DOCKER=1 - %s", local_file)
return local_file
mountable_file = Util.get_host_path_for_path_in_docker(local_file)
_, extension = os.path.splitext(local_file)
target_file_name = f"{md5(local_file)}{extension}"
target_path = f"/tmp/{target_file_name}"
inv_context.docker_flags = inv_context.docker_flags or ""
inv_context.docker_flags += f"-v {mountable_file}:{target_path}"
return target_path
class LambdaExecutorReuseContainers(LambdaExecutorContainers):
"""Executor class for executing Lambda functions in re-usable Docker containers"""
def __init__(self):
super(LambdaExecutorReuseContainers, self).__init__()
# locking thread for creation/destruction of docker containers.
self.docker_container_lock = threading.RLock()
# On each invocation we try to construct a port unlikely to conflict
# with a previously invoked lambda function. This is a problem with at
# least the lambci/lambda:go1.x container, which execs a go program that
# attempts to bind to the same default port.
self.next_port = 0
self.max_port = LAMBDA_SERVER_UNIQUE_PORTS
self.port_offset = LAMBDA_SERVER_PORT_OFFSET
def execute_in_container(
self,
lambda_function: LambdaFunction,
inv_context: InvocationContext,
stdin=None,
background=False,
) -> Tuple[bytes, bytes]:
func_arn = lambda_function.arn()
lambda_cwd = lambda_function.cwd
runtime = lambda_function.runtime
env_vars = inv_context.environment
# Choose a port for this invocation
with self.docker_container_lock:
env_vars["_LAMBDA_SERVER_PORT"] = str(self.next_port + self.port_offset)
self.next_port = (self.next_port + 1) % self.max_port
# create/verify the docker container is running.
LOG.debug(
'Priming docker container with runtime "%s" and arn "%s".',
runtime,
func_arn,
)
container_info = self.prime_docker_container(
lambda_function, dict(env_vars), lambda_cwd, inv_context.docker_flags
)
if not inv_context.lambda_command and inv_context.handler:
command = shlex.split(container_info.entry_point)
command.append(inv_context.handler)
inv_context.lambda_command = command
lambda_docker_ip = DOCKER_CLIENT.get_container_ip(container_info.name)
if not self._should_use_stay_open_mode(lambda_function, lambda_docker_ip, check_port=True):
LOG.debug("Using 'docker exec' to run invocation in docker-reuse Lambda container")
# disable stay open mode for this one, to prevent starting runtime API server
env_vars["DOCKER_LAMBDA_STAY_OPEN"] = None
return DOCKER_CLIENT.exec_in_container(
container_name_or_id=container_info.name,
command=inv_context.lambda_command,
interactive=True,
env_vars=env_vars,
stdin=stdin,
)
inv_result = self.invoke_lambda(lambda_function, inv_context, lambda_docker_ip)
return (inv_result.result, inv_result.log_output)
def invoke_lambda(
self,
lambda_function: LambdaFunction,
inv_context: InvocationContext,
lambda_docker_ip=None,
) -> InvocationResult:
full_url = self._get_lambda_stay_open_url(lambda_docker_ip)
client = aws_stack.connect_to_service("lambda", endpoint_url=full_url)
event = inv_context.event or "{}"
LOG.debug(f"Calling {full_url} to run invocation in docker-reuse Lambda container")
response = client.invoke(
FunctionName=lambda_function.name(),
InvocationType=inv_context.invocation_type,
Payload=to_bytes(event),
LogType="Tail",
)
log_output = base64.b64decode(response.get("LogResult") or b"").decode("utf-8")
result = response["Payload"].read().decode("utf-8")
if "FunctionError" in response:
raise InvocationException(
"Lambda process returned with error. Result: %s. Output:\n%s"
% (result, log_output),
log_output,
result,
)
return InvocationResult(result, log_output)
def _should_use_stay_open_mode(
self,
lambda_function: LambdaFunction,
lambda_docker_ip: Optional[str] = None,
check_port: bool = False,
) -> bool:
"""Return whether to use stay-open execution mode - if we're running in Docker, the given IP
is defined, and if the target API endpoint is available (optionally, if check_port is True)."""
if not lambda_docker_ip:
func_arn = lambda_function.arn()
container_name = self.get_container_name(func_arn)
lambda_docker_ip = DOCKER_CLIENT.get_container_ip(container_name_or_id=container_name)
should_use = lambda_docker_ip and config.LAMBDA_STAY_OPEN_MODE
if not should_use or not check_port:
return should_use
full_url = self._get_lambda_stay_open_url(lambda_docker_ip)
return is_port_open(full_url)
def _get_lambda_stay_open_url(self, lambda_docker_ip: str) -> str:
return f"http://{lambda_docker_ip}:{STAY_OPEN_API_PORT}"
def _execute(self, func_arn: str, *args, **kwargs) -> InvocationResult:
if not LAMBDA_CONCURRENCY_LOCK.get(func_arn):
concurrency_lock = threading.RLock()
LAMBDA_CONCURRENCY_LOCK[func_arn] = concurrency_lock
with LAMBDA_CONCURRENCY_LOCK[func_arn]:
return super(LambdaExecutorReuseContainers, self)._execute(func_arn, *args, **kwargs)
def startup(self):
self.cleanup()
# start a process to remove idle containers
if config.LAMBDA_REMOVE_CONTAINERS:
self.start_idle_container_destroyer_interval()
def cleanup(self, arn: str = None):
if arn:
self.function_invoke_times.pop(arn, None)
return self.destroy_docker_container(arn)
self.function_invoke_times = {}
return self.destroy_existing_docker_containers()
def prime_docker_container(
self,
lambda_function: LambdaFunction,
env_vars: Dict,
lambda_cwd: str,
docker_flags: str = None,
):
"""
Prepares a persistent docker container for a specific function.
:param lambda_function: The Details of the lambda function.
:param env_vars: The environment variables for the lambda.
:param lambda_cwd: The local directory containing the code for the lambda function.
:return: ContainerInfo class containing the container name and default entry point.
"""
with self.docker_container_lock:
# Get the container name and id.
func_arn = lambda_function.arn()
container_name = self.get_container_name(func_arn)
status = self.get_docker_container_status(func_arn)
LOG.debug('Priming Docker container (status "%s"): %s', status, container_name)
docker_image = Util.docker_image_for_lambda(lambda_function)
# Container is not running or doesn't exist.
if status < 1:
# Make sure the container does not exist in any form/state.
self.destroy_docker_container(func_arn)
# get container startup command and run it
LOG.debug("Creating container: %s", container_name)
self.create_container(lambda_function, env_vars, lambda_cwd, docker_flags)
LOG.debug("Starting docker-reuse Lambda container: %s", container_name)
DOCKER_CLIENT.start_container(container_name)
def wait_up():
cont_status = DOCKER_CLIENT.get_container_status(container_name)
assert cont_status == DockerContainerStatus.UP
if not in_docker():
return
# if we're executing in Docker using stay-open mode, additionally check if the target is available
lambda_docker_ip = DOCKER_CLIENT.get_container_ip(container_name)
if self._should_use_stay_open_mode(lambda_function, lambda_docker_ip):
full_url = self._get_lambda_stay_open_url(lambda_docker_ip)
wait_for_port_open(full_url, sleep_time=0.5, retries=8)
# give the container some time to start up
retry(wait_up, retries=15, sleep=0.8)
container_network = self.get_docker_container_network(func_arn)
entry_point = DOCKER_CLIENT.get_image_entrypoint(docker_image)
LOG.debug(
'Using entrypoint "%s" for container "%s" on network "%s".',
entry_point,
container_name,
container_network,
)
return ContainerInfo(container_name, entry_point)
def create_container(
self,
lambda_function: LambdaFunction,
env_vars: Dict,
lambda_cwd: str,
docker_flags: str = None,
):
docker_image = Util.docker_image_for_lambda(lambda_function)
container_config = LambdaContainerConfiguration(image_name=docker_image)
container_config.name = self.get_container_name(lambda_function.arn())
# make sure AWS_LAMBDA_EVENT_BODY is not set (otherwise causes issues with "docker exec ..." above)
env_vars.pop("AWS_LAMBDA_EVENT_BODY", None)
container_config.env_vars = env_vars
container_config.network = get_container_network_for_lambda()
container_config.additional_flags = docker_flags
container_config.dns = config.LAMBDA_DOCKER_DNS
if lambda_cwd: