Skip to content

Commit a88f810

Browse files
authored
Add Warning For Lambda Extension With Unsupported Runtimes (#160)
* Add Warning For Lambda Extension With Unsupported Runtimes * Add test for warning code path * Bump to v0.5.5 * Add function name to warning message * Fix runtime / function name order
1 parent de12c8b commit a88f810

File tree

6 files changed

+98
-22
lines changed

6 files changed

+98
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ A CLI to install the New Relic AWS Lambda integration and layers.
3838
* java11
3939
* nodejs10.x
4040
* nodejs12.x
41+
* nodejs14.x
4142
* provided
4243
* provided.al2
4344
* python2.7

newrelic_lambda_cli/layers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77

88
from newrelic_lambda_cli import api, subscriptions, utils
9-
from newrelic_lambda_cli.cliutils import failure, success
9+
from newrelic_lambda_cli.cliutils import failure, success, warning
1010
from newrelic_lambda_cli.functions import get_function
1111
from newrelic_lambda_cli.integrations import _get_license_key_policy_arn
1212
from newrelic_lambda_cli.types import LayerInstall, LayerUninstall
@@ -125,7 +125,16 @@ def _add_new_relic(input, config, nr_license_key):
125125
if runtime_handler and handler != runtime_handler:
126126
update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_HANDLER"] = handler
127127

128-
if input.enable_extension:
128+
if input.enable_extension and not utils.supports_lambda_extension(runtime):
129+
warning(
130+
"The %s runtime for %s does not support Lambda Extensions, reverting to a "
131+
"CloudWatch Logs based ingestion. Make sure you run `newrelic-lambda "
132+
"integrations install` command to install the New Relic log ingestion "
133+
"function and `newrelic-lambda subscriptions install` to create the log "
134+
"subscription filter." % (runtime, config["Configuration"]["FunctionName"])
135+
)
136+
137+
if input.enable_extension and utils.supports_lambda_extension(runtime):
129138
update_kwargs["Environment"]["Variables"][
130139
"NEW_RELIC_LAMBDA_EXTENSION_ENABLED"
131140
] = "true"

newrelic_lambda_cli/utils.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,39 @@
77

88
NEW_RELIC_ARN_PREFIX_TEMPLATE = "arn:aws:lambda:%s:451483290750"
99
RUNTIME_CONFIG = {
10-
"dotnetcore3.1": {},
11-
"java11": {},
12-
"java8.al2": {},
13-
"nodejs10.x": {"Handler": "newrelic-lambda-wrapper.handler"},
14-
"nodejs12.x": {"Handler": "newrelic-lambda-wrapper.handler"},
15-
"provided": {},
16-
"provided.al2": {},
17-
"python2.7": {"Handler": "newrelic_lambda_wrapper.handler"},
18-
"python3.6": {"Handler": "newrelic_lambda_wrapper.handler"},
19-
"python3.7": {"Handler": "newrelic_lambda_wrapper.handler"},
20-
"python3.8": {"Handler": "newrelic_lambda_wrapper.handler"},
10+
"dotnetcore3.1": {"LambdaExtension": True},
11+
"java11": {"LambdaExtension": True},
12+
"java8.al2": {"LambdaExtension": True},
13+
"nodejs10.x": {
14+
"Handler": "newrelic-lambda-wrapper.handler",
15+
"LambdaExtension": True,
16+
},
17+
"nodejs12.x": {
18+
"Handler": "newrelic-lambda-wrapper.handler",
19+
"LambdaExtension": True,
20+
},
21+
"nodejs14.x": {
22+
"Handler": "newrelic-lambda-wrapper.handler",
23+
"LambdaExtension": True,
24+
},
25+
"provided": {"LambdaExtension": True},
26+
"provided.al2": {"LambdaExtension": True},
27+
"python2.7": {
28+
"Handler": "newrelic_lambda_wrapper.handler",
29+
"LambdaExtension": False,
30+
},
31+
"python3.6": {
32+
"Handler": "newrelic_lambda_wrapper.handler",
33+
"LambdaExtension": False,
34+
},
35+
"python3.7": {
36+
"Handler": "newrelic_lambda_wrapper.handler",
37+
"LambdaExtension": True,
38+
},
39+
"python3.8": {
40+
"Handler": "newrelic_lambda_wrapper.handler",
41+
"LambdaExtension": True,
42+
},
2143
}
2244

2345

@@ -115,3 +137,7 @@ def parse_arn(arn):
115137
else:
116138
result["resourcetype"], result["resource"] = elements[5].split("/")
117139
return result
140+
141+
142+
def supports_lambda_extension(runtime):
143+
return RUNTIME_CONFIG.get(runtime, {}).get("LambdaExtension", False)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="newrelic-lambda-cli",
9-
version="0.5.4",
9+
version="0.5.5",
1010
python_requires=">=3.3",
1111
description="A CLI to install the New Relic AWS Lambda integration and layers.",
1212
long_description=README,

tests/test_layers.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_add_new_relic(aws_credentials, mock_function_config):
7171
is True
7272
)
7373

74-
config = mock_function_config("python3.6")
74+
config = mock_function_config("python3.7")
7575
config["Configuration"]["Layers"] = [{"Arn": get_arn_prefix("us-east-1")}]
7676
assert (
7777
_add_new_relic(
@@ -90,7 +90,7 @@ def test_add_new_relic(aws_credentials, mock_function_config):
9090

9191
with patch("newrelic_lambda_cli.layers.index") as mock_index:
9292
mock_index.return_value = []
93-
config = mock_function_config("python3.6")
93+
config = mock_function_config("python3.7")
9494
assert (
9595
_add_new_relic(
9696
layer_install(
@@ -120,7 +120,7 @@ def test_add_new_relic(aws_credentials, mock_function_config):
120120
}
121121
},
122122
]
123-
config = mock_function_config("python3.6")
123+
config = mock_function_config("python3.7")
124124
with pytest.raises(UsageError):
125125
_add_new_relic(
126126
layer_install(
@@ -134,7 +134,7 @@ def test_add_new_relic(aws_credentials, mock_function_config):
134134
nr_license_key=None,
135135
)
136136

137-
config = mock_function_config("python3.6")
137+
config = mock_function_config("python3.7")
138138
_add_new_relic(
139139
layer_install(
140140
session=session,
@@ -152,7 +152,7 @@ def test_add_new_relic(aws_credentials, mock_function_config):
152152
in config["Configuration"]["Environment"]["Variables"]
153153
)
154154

155-
config = mock_function_config("python3.6")
155+
config = mock_function_config("python3.7")
156156
config["Configuration"]["Environment"]["Variables"]["NEW_RELIC_FOO"] = "bar"
157157
config["Configuration"]["Layers"] = [{"Arn": get_arn_prefix("us-east-1")}]
158158
update_kwargs = _add_new_relic(
@@ -170,6 +170,24 @@ def test_add_new_relic(aws_credentials, mock_function_config):
170170
assert "NEW_RELIC_FOO" in update_kwargs["Environment"]["Variables"]
171171
assert update_kwargs["Layers"][0] != get_arn_prefix("us-east-1")
172172

173+
config = mock_function_config("python3.6")
174+
update_kwargs = _add_new_relic(
175+
layer_install(
176+
session=session,
177+
aws_region="us-east-1",
178+
nr_account_id=12345,
179+
enable_extension=True,
180+
enable_extension_function_logs=True,
181+
upgrade=True,
182+
),
183+
config,
184+
"foobarbaz",
185+
)
186+
assert (
187+
update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_EXTENSION_ENABLED"]
188+
== "false"
189+
)
190+
173191

174192
@mock_lambda
175193
def test_remove_new_relic(aws_credentials, mock_function_config):
@@ -205,7 +223,7 @@ def test_remove_new_relic(aws_credentials, mock_function_config):
205223
is True
206224
)
207225

208-
config = mock_function_config("python3.6")
226+
config = mock_function_config("python3.7")
209227
config["Configuration"]["Handler"] = "what is this?"
210228
assert (
211229
_remove_new_relic(
@@ -262,7 +280,7 @@ def test_install(aws_credentials, mock_function_config):
262280
assert install(layer_install(session=mock_session), "foobarbaz") is True
263281

264282
mock_client.get_function.reset_mock(return_value=True)
265-
config = mock_function_config("python3.6")
283+
config = mock_function_config("python3.7")
266284
mock_client.get_function.return_value = config
267285
assert (
268286
install(
@@ -304,7 +322,7 @@ def test_uninstall(aws_credentials, mock_function_config):
304322
assert uninstall(layer_uninstall(session=mock_session), "foobarbaz") is True
305323

306324
mock_client.get_function.reset_mock(return_value=True)
307-
config = mock_function_config("python3.6")
325+
config = mock_function_config("python3.7")
308326
mock_client.get_function.return_value = config
309327
assert uninstall(layer_uninstall(session=mock_session), "foobarbaz") is False
310328

tests/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
parse_arn,
1010
validate_aws_profile,
1111
catch_boto_errors,
12+
supports_lambda_extension,
1213
)
1314

1415

@@ -69,3 +70,24 @@ def _no_region_error():
6970

7071
with pytest.raises(Exit):
7172
_no_region_error()
73+
74+
75+
def test_supports_lambda_extension():
76+
assert all(
77+
supports_lambda_extension(runtime)
78+
for runtime in (
79+
"dotnetcore3.1",
80+
"java11",
81+
"java8.al2",
82+
"nodejs10.x",
83+
"nodejs12.x",
84+
"nodejs14.x",
85+
"provided",
86+
"provided.al2",
87+
"python3.7",
88+
"python3.8",
89+
)
90+
)
91+
assert not any(
92+
supports_lambda_extension(runtime) for runtime in ("python2.7", "python3.6")
93+
)

0 commit comments

Comments
 (0)