newrelic / newrelic-lambda-cli

A CLI to install the New Relic AWS Lambda integration and layers.
https://newrelic.com/products/serverless-aws-lambda
Apache License 2.0
47 stars 53 forks source link

AttributeError Using `--function installed` Flag with `--upgrade` in `newrelic-lambda layers install` #285

Closed keegoid-nr closed 4 months ago

keegoid-nr commented 4 months ago

Description

Attempting to upgrade New Relic Lambda layers with the --upgrade flag using the --function installed filter results in an AttributeError. This occurs across different environments and Python versions, pointing to a possible bug in the newrelic-lambda-cli tool.

Expected Behavior

Running the command newrelic-lambda layers install --function installed --nr-account-id <account_id> --upgrade should upgrade the New Relic Lambda layers for all functions where the layer is already installed.

Current Behavior

The command execution ends with the error:

AttributeError: 'NoneType' object has no attribute 'client'

This occurs both on a local Mac environment and AWS CloudShell, but not when specifying a specific function by name.

Steps to Reproduce

  1. Run newrelic-lambda layers install --function installed --nr-account-id <account_id> --upgrade on both a local Mac and within AWS CloudShell.
  2. Error returned in both cases with AttributeError.

Environment

Validating AWS Lambda functions are Installed

(.venv312)$ newrelic-lambda functions list
Function Name                           Runtime          Installed
--------------------------------------  ---------------  -----------
kmullaney-tf-demo-python                python3.10       Yes
aws-controltower-NotificationForwarder  python3.9        No
kmullaney-sam-esm-nodejs18x             nodejs18.x       Yes

Validating AWS Credentials and Permissions

Below are the results of executing AWS CLI commands to confirm that my AWS credentials are valid and the permissions for my assumed role are correct:

{
    "UserId": "ASDFHJKLASDFHJKLASDF:user123",
    "Account": "123456789012",
    "Arn": "arn:aws:sts::123456789012:assumed-role/AWSReservedSSO_1234567/user123"
}
{
    "Functions": [
        {
            "FunctionName": "kmullaney-tf-demo-python",
            "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:kmullaney-tf-demo-python",
            // ... other function details ...
        }
        // ... potentially more functions ...
    ]
}
{
    "AttachedPolicies": [
        {
            "PolicyName": "AmazonEC2FullAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
        },
        {
            "PolicyName": "PowerUserAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/PowerUserAccess"
        },
        {
            "PolicyName": "AWSCloudFormationFullAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/AWSCloudFormationFullAccess"
        }
    ]
}

The above outputs confirm that my credentials are correctly configured, and I have the necessary permissions to list and potentially upgrade Lambda functions.

Logs

(.venv312)$ newrelic-lambda layers install --function installed --nr-account-id 1234567 --upgrade
Traceback (most recent call last):
  File "/Users/kmullaney/.venv312/bin/newrelic-lambda", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/newrelic_lambda_cli/cli/__init__.py", line 26, in main
    cli()
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/newrelic_lambda_cli/cli/layers.py", line 114, in install
    functions = get_aliased_functions(input)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/newrelic_lambda_cli/functions.py", line 82, in get_aliased_functions
    for function in list_functions(input.session, alias):
  File "/Users/kmullaney/.venv312/lib/python3.12/site-packages/newrelic_lambda_cli/functions.py", line 17, in list_functions
    client = session.client("lambda")
             ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'client'

Additional Context

The upgrade works as expected when specifying an individual function name instead of using the --function installed.

Possible Solution

(No suggestions, awaiting insights from newrelic-lambda-cli maintainers.)

workato-integration[bot] commented 4 months ago

https://new-relic.atlassian.net/browse/NR-276062

chaudharysaket commented 4 months ago

Released v0.9.2 which solves the above scenario.