question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Logger is not defined

See original GitHub issue

Expected Behaviour

Logger to be used as always have been (same usagee as in below code snippet done in dozens of other lambdas)

Current Behaviour

Error from Lambda logs: [ERROR] NameError: name 'Logger' is not defined Traceback (most recent call last): File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/var/task/lambda.py", line 11, in <module> LOGGER = Logger(sampling_rate=float(os.environ["POWERTOOLS_LOGGER_SAMPLE_RATE"]))

Code snippet

# -*- coding: utf-8 -*-
"""Stop EC2 with Schedule tag name."""
import os
from typing import Any, Dict

import boto3
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

LOGGER = Logger(sampling_rate=float(os.environ["POWERTOOLS_LOGGER_SAMPLE_RATE"]))
REGION = os.environ["REGION"]
TAG_NAME = os.environ["TAG_NAME"]
TAG_VALUE = os.environ["TAG_VALUE"]
FILTERS = [{"Name": f"tag:{TAG_NAME}", "Values": [TAG_VALUE]}]
CLIENT = boto3.client("ec2", region_name=REGION)


# pylint: disable=W0613
@LOGGER.inject_lambda_context(log_event=True)
def lambda_handler(event: Dict[str, Any], context: LambdaContext) -> None:
    """Starting lambda."""
    LOGGER.debug(f"Event: {event}")

    reservations = CLIENT.describe_instances(Filters=FILTERS)
    for r in reservations["Reservations"]:
        for i in r["Instances"]:
            CLIENT.stop_instances(InstanceIds=[i["InstanceId"]])
            LOGGER.debug(f"Stopped {i['InstanceId']} successfully")

Possible Solution

No response

Steps to Reproduce

Deploy powertools 1.26.1 to lambda layer, lambda code to lambda service

AWS Lambda Powertools for Python version

1.26.1

AWS Lambda function runtime

3.9

Packaging format used

Lambda Layers

Debugging logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
macdrorepocommented, Jul 4, 2022

Can confirm my custom layer is working, AWS lately is mehhh. Thx to all for reaching out!

1reaction
macdrorepocommented, Jul 4, 2022

Here you are whole powertools lib content 😃

aws_lambda_powertools.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

NameError: global name 'logger' is not defined - Stack Overflow
In your class you are using logger but you have not defined it. Either set logger like: logger = logging.getLogger(__name__).
Read more >
" logger is not defined" on sfdx force:user:display output? [BUG]
This sounds like a bug in the CLI that you should report it via initiating the case . ... logger is not defined\\\\n...
Read more >
Trying to log during Python finalization with NameError: name ...
-- I pushed a change in Python 3.10 which reduces the risk of "NameError: name 'open' is not defined" when logging really late...
Read more >
Integration with Bug Tracking Systems / Issue ... - TortoiseSVN
This property defines if the bug-ID is appended (true) to the end of the log message or inserted (false) at the start of...
Read more >
ReferenceError: log is not defined - PlayCanvas Forum
There is likely to be error in the project that it was trying to catch. Except the catcher code is bugged as shown...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found