How to disable default log messages from lambda in python

会有一股神秘感。 提交于 2020-01-24 03:55:25

问题


I have an AWS Lambda function written in python, and i need only the messages I log in CloudWatch Logs. I have tried the example given in watchtower, but it still didn't work.

START RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5 Version: $LATEST
(randomiser) Hello from Lambda
END RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5
REPORT RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5
Duration: 0.44 ms Billed Duration: 100 ms Memory Size: 128 MB   Max Memory Used: 21 MB*

From the above I only need (randomiser) Hello from Lambda to be logged in CloudWatch, without the START, END and REPORT lines.


回答1:


If you have logs enabled, you are always going to get the default logs. No way you can disable them.

However there might be cases where you want one specific Lambda function to not send logs at all. You can solve this by creating a new role specifically for that Lambda function, and not have the logging permission there.

FWIW, if you need to toggle between logging and no logging frequently, you can have a policy file as the following.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": [ "arn:aws:logs:*:*:*" ] } ] }

and change the "Deny" to "Allow" when you require logging.




回答2:


There is no direct way to disable these logs. However, a simple workaround is to remove the CloudWatch Logs permission from the Lambda execution role. Lambda function uses this role to access other AWS services, if you remove CloudWatch permission it will not be able to push logs to CloudWatch.

Note: if you do this you will not able to push any logs from lambda to CloudWatch



来源:https://stackoverflow.com/questions/51285835/how-to-disable-default-log-messages-from-lambda-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!