Skip to content

Commit 41ea0b1

Browse files
author
Sudhakar Reddy
committed
feat: allow extensions to be disabled
1 parent 71388dd commit 41ea0b1

File tree

6 files changed

+93
-5
lines changed

6 files changed

+93
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ You can build RIE into a base image. Download the RIE from GitHub to your local
8383
```sh
8484
#!/bin/sh
8585
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
86-
exec /usr/local/bin/aws-lambda-rie /usr/bin/npx aws-lambda-ric
86+
exec /usr/local/bin/aws-lambda-rie /var/lang/bin/npx aws-lambda-ric $1
8787
else
88-
exec /usr/bin/npx aws-lambda-ric
88+
exec /var/lang/bin/npx aws-lambda-ric $1
8989
fi
9090
```
9191

cmd/aws-lambda-rie/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,18 @@ func main() {
6363
log.WithError(err).Fatalf("The command line value for \"--runtime-interface-emulator-address\" is not a valid network address %q.", opts.RuntimeInterfaceEmulatorAddress)
6464
}
6565

66+
enableExtensions := true
67+
envDisableExtensionValue, envDisableExtensionSet := os.LookupEnv("AWS_LAMBDA_RIE_DISABLE_EXTENSIONS")
68+
if envDisableExtensionSet && envDisableExtensionValue != "FALSE" {
69+
enableExtensions = false
70+
log.Info("Disabled extensions")
71+
}
72+
6673
bootstrap, handler := getBootstrap(args, opts)
6774
sandbox := rapidcore.
6875
NewSandboxBuilder().
6976
AddShutdownFunc(context.CancelFunc(func() { os.Exit(0) })).
70-
SetExtensionsFlag(true).
77+
SetExtensionsFlag(enableExtensions).
7178
SetInitCachingFlag(opts.InitCachingEnabled)
7279

7380
if len(handler) > 0 {

test/integration/local_lambda/test_end_to_end.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def test_env_var_with_equal_sign(self):
9999

100100
self.assertEqual(b'"4=4"', r.content)
101101

102-
103102
def test_two_invokes(self):
104103
image, rie, image_name = self.tagged_name("twoinvokes")
105104

@@ -255,7 +254,42 @@ def test_custom_client_context(self):
255254
content = json.loads(r.content)
256255
self.assertEqual("bar", content["foo"])
257256
self.assertEqual(123, content["baz"])
257+
258+
def test_disable_extension_with_empty_env_val(self):
259+
image, rie, image_name = self.tagged_name("disable_extension_check_with_empty_value")
260+
params = f"--name {image} -d --env AWS_LAMBDA_RIE_DISABLE_EXTENSIONS= -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_extension_is_enabled_handler"
261+
262+
with self.create_container(params, image):
263+
r = self.invoke_function()
264+
265+
self.assertEqual(b'"false"', r.content)
266+
267+
def test_disable_extension_with_non_empty_env_val(self):
268+
image, rie, image_name = self.tagged_name("disable_extension_check_with_non-empty_value")
269+
params = f"--name {image} -d --env AWS_LAMBDA_RIE_DISABLE_EXTENSIONS=somevalue -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_extension_is_enabled_handler"
270+
271+
with self.create_container(params, image):
272+
r = self.invoke_function()
273+
274+
self.assertEqual(b'"false"', r.content)
275+
276+
def test_enable_extension_with_env_var(self):
277+
image, rie, image_name = self.tagged_name("enable_extension_check_with_env_var")
278+
params = f"--name {image} -d --env AWS_LAMBDA_RIE_DISABLE_EXTENSIONS=FALSE -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_extension_is_enabled_handler"
279+
280+
with self.create_container(params, image):
281+
r = self.invoke_function()
282+
283+
self.assertEqual(b'"true"', r.content)
258284

285+
def test_enable_extension_without_env_var(self):
286+
image, rie, image_name = self.tagged_name("enable_extension_without_env_var")
287+
params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_extension_is_enabled_handler"
288+
289+
with self.create_container(params, image):
290+
r = self.invoke_function()
291+
292+
self.assertEqual(b'"true"', r.content)
259293

260294
if __name__ == "__main__":
261295
main()

test/integration/testdata/Dockerfile-allinone

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM public.ecr.aws/lambda/python:3.12-$IMAGE_ARCH
33

44
WORKDIR /var/task
55
COPY ./ ./
6-
6+
# Copy extension
7+
ADD bash-extension /opt/extensions/
78
# This is to verify env vars are parsed correctly before executing the function
89
ENV MyEnv="4=4"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Name of the extension
4+
EXTENSION_NAME="bash-extension"
5+
6+
# Log file path
7+
LOG_FILE="/tmp/extension.log"
8+
9+
10+
# Function to register the extension with the Lambda service
11+
register_extension() {
12+
curl -s -D /tmp/headers -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2020-01-01/extension/register" \
13+
-H "Content-Type: application/json" \
14+
-H "Lambda-Extension-Name: $EXTENSION_NAME" \
15+
-d '{"events": ["INVOKE"]}'
16+
EXTENSION_ID=$(cat /tmp/headers | grep "Lambda-Extension-Identifier" | grep -oP '[a-f0-9\-]{36}')
17+
echo "Extension Id: $EXTENSION_ID" >> $LOG_FILE
18+
}
19+
20+
# Function to process events
21+
process_events() {
22+
# Main loop
23+
while true; do
24+
echo "Waiting for next event"
25+
EVENT_DATA=$(curl -s -X GET \
26+
-H "Lambda-Extension-Identifier: $EXTENSION_ID" \
27+
"http://${AWS_LAMBDA_RUNTIME_API}/2020-01-01/extension/event/next")
28+
29+
# Check if the event is an invocation
30+
if [[ $(echo "$EVENT_DATA" | jq -r '.eventType') == "INVOKE" ]]; then
31+
echo "Invocation event received: $EVENT_DATA"
32+
# Log the invocation event data
33+
echo "$EVENT_DATA" >> "$LOG_FILE"
34+
fi
35+
done
36+
}
37+
38+
# Register the extension
39+
register_extension
40+
41+
# Process events
42+
process_events

test/integration/testdata/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def success_handler(event, context):
2222
def check_env_var_handler(event, context):
2323
return os.environ.get("MyEnv")
2424

25+
def check_extension_is_enabled_handler(event, context):
26+
if os.path.isfile("/tmp/extension.log"):
27+
return "true"
28+
return "false"
2529

2630
def assert_env_var_is_overwritten(event, context):
2731
print(os.environ.get("AWS_LAMBDA_FUNCTION_NAME"))

0 commit comments

Comments
 (0)