Skip to content

Commit 2a98500

Browse files
Add tag to Lambda to enable APM Lambda Mode (#331)
* add tags for APM Mode * update CLI version * add tags success message * add test case * rectify test case * update test conditions
1 parent 15b092e commit 2a98500

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

newrelic_lambda_cli/layers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ def install(input, function_arn):
304304

305305
try:
306306
res = client.update_function_configuration(**update_kwargs)
307+
if input.apm:
308+
client.tag_resource(
309+
Resource=config["Configuration"]["FunctionArn"],
310+
Tags={
311+
"NR.Apm.Lambda.Mode": "true",
312+
},
313+
)
314+
success("Successfully added APM tag to the function")
307315
except botocore.exceptions.ClientError as e:
308316
failure(
309317
"Failed to update configuration for '%s': %s"

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.9.8",
9+
version="0.9.9",
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,85 @@ def test_add_new_relic_apm_lambda_mode(aws_credentials, mock_function_config):
263263
)
264264

265265

266+
def test_install_apm(aws_credentials, mock_function_config):
267+
mock_session = MagicMock()
268+
mock_session.region_name = "us-east-1"
269+
expected_tags_after_tagging = {
270+
"NR.Apm.Lambda.Mode": "true",
271+
}
272+
273+
with patch(
274+
"newrelic_lambda_cli.layers._get_license_key_outputs"
275+
) as mock_get_license_key_outputs:
276+
mock_client = mock_session.client.return_value
277+
278+
mock_client.get_function.reset_mock(return_value=True)
279+
280+
config = mock_function_config("python3.12")
281+
mock_client.get_function.return_value = config
282+
283+
mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
284+
285+
try:
286+
install(
287+
layer_install(
288+
session=mock_session,
289+
aws_region="us-east-1",
290+
nr_account_id=12345,
291+
apm=True,
292+
),
293+
"APMLambda",
294+
)
295+
except UsageError as e:
296+
print(f"UsageError: {e}")
297+
298+
mock_client.get_function.reset_mock()
299+
config = mock_function_config("python3.12")
300+
mock_client.get_function.return_value = config
301+
mock_client.list_tags.return_value = {"Tags": expected_tags_after_tagging}
302+
assert (
303+
install(
304+
layer_install(nr_account_id=12345, session=mock_session), "APMLambda"
305+
)
306+
is True
307+
)
308+
309+
mock_client.assert_has_calls([call.get_function(FunctionName="APMLambda")])
310+
mock_client.assert_has_calls(
311+
[
312+
call.update_function_configuration(
313+
FunctionName="arn:aws:lambda:us-east-1:5558675309:function:aws-python3-dev-hello", # noqa
314+
Environment={
315+
"Variables": {
316+
"EXISTING_ENV_VAR": "Hello World",
317+
"NEW_RELIC_ACCOUNT_ID": "12345",
318+
"NEW_RELIC_LAMBDA_HANDLER": "original_handler",
319+
"NEW_RELIC_LAMBDA_EXTENSION_ENABLED": "false",
320+
"NEW_RELIC_APM_LAMBDA_MODE": "True",
321+
}
322+
},
323+
Layers=ANY,
324+
Handler="newrelic_lambda_wrapper.handler",
325+
)
326+
]
327+
)
328+
329+
mock_client.assert_has_calls(
330+
[
331+
call.tag_resource(
332+
Resource="arn:aws:lambda:us-east-1:5558675309:function:aws-python3-dev-hello",
333+
Tags={
334+
"NR.Apm.Lambda.Mode": "true",
335+
},
336+
)
337+
]
338+
)
339+
340+
tags_from_list_tags = mock_client.list_tags(Resource="APMLambda")["Tags"]
341+
342+
assert tags_from_list_tags == expected_tags_after_tagging
343+
344+
266345
@mock_aws
267346
def test_add_new_relic_dotnet(aws_credentials, mock_function_config):
268347
session = boto3.Session(region_name="us-east-1")

0 commit comments

Comments
 (0)