Repository: newrelic/newrelic-lambda-cli

Issue:
[layers install] Add `NEW_RELIC_LAMBDA_EXTENSION_ENABLED=true` environment variable 
[NOTE]: # ( ^^ Provide a general summary of the request in the title above. ^^ )

## Summary

[NOTE]: # ( Provide a brief overview of what the new feature is all about. )

New Relic Lambda Layer required `NEW_RELIC_LAMBDA_EXTENSION_ENABLED=true` environment variable.  We have to add this variable manually because `laryers install` command doesn't add it.

https://docs.newrelic.co.jp/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/enable-serverless-monitoring-using-lambda-layer

## Desired Behaviour

[NOTE]: # ( Tell us how the new feature should work. Be specific. )
[TIP]:  # ( Do NOT give us access or passwords to your New Relic account or API keys! )

The `layers install` command will add `NEW_RELIC_LAMBDA_EXTENSION_ENABLED=true`.


## Possible Solution

[NOTE]: # ( Not required. Suggest how to implement the addition or change. )

How about add env var [here](https://github.com/newrelic/newrelic-lambda-cli/blob/7eb3075186dc2df9246676ea388e8b6542aa40c0/newrelic_lambda_cli/layers.py#L95-L99
)?

## Additional context

[TIP]:  # ( Why does this feature matter to you? What unique circumstances do you have? )


Comments:

Patch:
diff --git a/README.md b/README.md
index f386693..99329ca 100644
--- a/README.md
+++ b/README.md
@@ -154,6 +154,7 @@ newrelic-lambda layers install \
 | `--exclude` or `-e` | No | A function name to exclude while installing layers. Can provide multiple `--exclude` arguments. Only checked when `all`, `installed` and `not-installed` are used. See `newrelic-lambda functions list` for function names. |
 | `--layer-arn` or `-l` | No | Specify a specific layer version ARN to use. This is auto detected by default. |
 | `--upgrade` or `-u` | No | Permit upgrade to the latest layer version for this region and runtime. |
+| `--enable-extension` or `-x` | No | Enable the [New Relic Lambda Extension](https://github.com/newrelic/newrelic-lambda-extension). |
 | `--aws-profile` or `-p` | No | The AWS profile to use for this command. Can also use `AWS_PROFILE`. Will also check `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables if not using AWS CLI. |
 | `--aws-region` or `-r` | No | The AWS region this function is located. Can use `AWS_DEFAULT_REGION` environment variable. Defaults to AWS session region. |
 
diff --git a/newrelic_lambda_cli/cli/layers.py b/newrelic_lambda_cli/cli/layers.py
index 9807f0e..7a8ab3f 100644
--- a/newrelic_lambda_cli/cli/layers.py
+++ b/newrelic_lambda_cli/cli/layers.py
@@ -63,6 +63,12 @@ def register(group):
     help="Permit upgrade of function layers to new version.",
     is_flag=True,
 )
+@click.option(
+    "--enable-extension",
+    "-x",
+    help="Enable the New Relic Lambda Extension",
+    is_flag=True,
+)
 @click.pass_context
 def install(
     ctx,
@@ -74,6 +80,7 @@ def install(
     excludes,
     layer_arn,
     upgrade,
+    enable_extension,
 ):
     """Install New Relic AWS Lambda Layers"""
     session = boto3.Session(profile_name=aws_profile, region_name=aws_region)
@@ -92,6 +99,7 @@ def install(
                 layer_arn,
                 nr_account_id,
                 upgrade,
+                enable_extension,
                 ctx.obj["VERBOSE"],
             )
             for function in functions
diff --git a/newrelic_lambda_cli/layers.py b/newrelic_lambda_cli/layers.py
index ce3d47a..9215a24 100644
--- a/newrelic_lambda_cli/layers.py
+++ b/newrelic_lambda_cli/layers.py
@@ -19,7 +19,9 @@ def index(region, runtime):
     return layers_response.get("Layers", [])
 
 
-def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
+def _add_new_relic(
+    config, region, layer_arn, account_id, allow_upgrade, enable_extension
+):
     runtime = config["Configuration"]["Runtime"]
     if runtime not in utils.RUNTIME_CONFIG:
         failure(
@@ -36,7 +38,7 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
             "upgrade or reinstall to latest layer version."
             % config["Configuration"]["FunctionArn"]
         )
-        return False
+        return True
 
     existing_layers = [
         layer["Arn"]
@@ -96,10 +98,23 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
     if handler != runtime_handler:
         update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_HANDLER"] = handler
 
+    if enable_extension:
+        update_kwargs["Environment"]["Variables"][
+            "NEW_RELIC_LAMBDA_EXTENSION_ENABLED"
+        ] = "true"
+
     return update_kwargs
 
 
-def install(session, function_arn, layer_arn, account_id, allow_upgrade, verbose):
+def install(
+    session,
+    function_arn,
+    layer_arn,
+    account_id,
+    allow_upgrade,
+    enable_extension,
+    verbose,
+):
     client = session.client("lambda")
     config = get_function(session, function_arn)
     if not config:
@@ -108,7 +123,9 @@ def install(session, function_arn, layer_arn, account_id, allow_upgrade, verbose
 
     region = session.region_name
 
-    update_kwargs = _add_new_relic(config, region, layer_arn, account_id, allow_upgrade)
+    update_kwargs = _add_new_relic(
+        config, region, layer_arn, account_id, allow_upgrade, enable_extension
+    )
     if not update_kwargs:
         return False
 


Test patch:
diff --git a/tests/test_layers.py b/tests/test_layers.py
index 971702f..90ee31f 100644
--- a/tests/test_layers.py
+++ b/tests/test_layers.py
@@ -5,7 +5,7 @@ def test_add_new_relic(mock_function_config):
     config = mock_function_config("python3.7")
     assert config["Configuration"]["Handler"] == "original_handler"
 
-    update_kwargs = _add_new_relic(config, "us-east-1", None, "12345", False)
+    update_kwargs = _add_new_relic(config, "us-east-1", None, "12345", False, True)
     assert update_kwargs["FunctionName"] == config["Configuration"]["FunctionArn"]
     assert update_kwargs["Handler"] == "newrelic_lambda_wrapper.handler"
     assert update_kwargs["Environment"]["Variables"]["NEW_RELIC_ACCOUNT_ID"] == "12345"
@@ -13,6 +13,10 @@ def test_add_new_relic(mock_function_config):
         update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_HANDLER"]
         == config["Configuration"]["Handler"]
     )
+    assert (
+        update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_EXTENSION_ENABLED"]
+        == "true"
+    )
 
 
 def test_remove_new_relic(mock_function_config):