aws-samples / hostname-as-target-for-elastic-load-balancer

MIT No Attribution
16 stars 17 forks source link

TypeError: not all arguments converted during string formatting #1

Open cep21 opened 3 years ago

cep21 commented 3 years ago

The lambda includes the following code:

https://github.com/aws-samples/hostname-as-target-for-elastic-load-balancer/blob/main/source/elb_hostname_as_target.py#L191

    except Exception as e:
        logger.error("ERROR:", e)

This causing a python error if triggered

[ERROR] TypeError: not all arguments converted during string formattingTraceback (most recent call last):  File "/var/task/elb_hostname_as_target.py", line 191, in lambda_handler    logger.error("ERROR:", e)  File "/var/lang/lib/python3.7/logging/__init__.py", line 1407, in error    self._log(ERROR, msg, args, **kwargs)  File "/var/lang/lib/python3.7/logging/__init__.py", line 1514, in _log    self.handle(record)  File "/var/lang/lib/python3.7/logging/__init__.py", line 1524, in handle    self.callHandlers(record)  File "/var/lang/lib/python3.7/logging/__init__.py", line 1586, in callHandlers    hdlr.handle(record)  File "/var/lang/lib/python3.7/logging/__init__.py", line 894, in handle    self.emit(record)  File "/var/runtime/bootstrap.py", line 243, in emit    msg = self.format(record)  File "/var/lang/lib/python3.7/logging/__init__.py", line 869, in format    return fmt.format(record)  File "/var/lang/lib/python3.7/logging/__init__.py", line 608, in format    record.message = record.getMessage()  File "/var/lang/lib/python3.7/logging/__init__.py", line 369, in getMessage    msg = msg % self.args

I think you want to use this instead

logger.exception("error")
pmankad96 commented 3 years ago

Interesting, I had tested it and it seemed to work fine. I will change the code to the following:

except Exception as e:
    logger.exception(f"ERROR: {e}")
    logger.error("ERROR: Invocation Failed")
    return (1)
DaveQB commented 2 years ago

Thanks for this great code.

I just set this up for our organisation and hit this very error. Thanks for the fix.

I got here by following this very helpful blog post --> https://aws.amazon.com/blogs/networking-and-content-delivery/hostname-as-target-for-network-load-balancers/ The post points the user to a zip file in this git repo that has this error (for me anyway).

https://github.com/aws-samples/hostname-as-target-for-elastic-load-balancer/tree/main/source https://github.com/aws-samples/hostname-as-target-for-elastic-load-balancer/blob/main/source/ElbHostnameAsTarget.zip

mtitus83 commented 2 weeks ago

Ran into this error today and solved by changing line 191 to

logger.error("ERROR: " + str(e))