Cloudwatch logs

0

I have created a lambda function that connects to a database. Here is the code for the connection:

def lambda_handler(event, context): print("event", event) print("context", context) email = event["request"]["userAttributes"]["email"] trigger_source = event["triggerSource"] print(trigger_source) print("passed email", email)

client_id = event['callerContext'].get('clientId')
client = get_client_from_id(client_id)

if trigger_source == "PreSignUp_SignUp":
    try:
      conn = pymysql.connect(host=HOST, user=USER, passwd=PASSWORD, db=NAME, 
      connect_timeout=5)
    except pymysql.MySQLError as e:
        raise Exception("Database Error, Please try signup after some time")
return event

Now if the connection fails with the database then i receive logs for the same error 4 times. What is the reason and how can i limit it to 1.

asked 8 months ago215 views
3 Answers
0

Hello.

Have you checked the request ID in CloudWatch Logs?
If the request ID is different, there is a possibility that Lambda is retrying when an error occurs.
Therefore, it is thought that the same log is being output four times.
https://docs.aws.amazon.com/lambda/latest/dg/invocation-retries.html

When Lambda is executed, the following logs should be output to CloudWatch Logs, so please compare them.

START RequestId: yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Version: $LATEST

profile picture
EXPERT
answered 8 months ago
0

How is your function triggered? If it is triggered asynchronously and your invocation fails, Lambda will retry the invocation several times.

If you want to prevent that, don't raise an exception. This way the Lambda service will think that the invocation was successful, and will not retry it.

profile pictureAWS
EXPERT
Uri
answered 8 months ago
0

Consider using RDS Proxy to handle database connections from a lambda: https://repost.aws/knowledge-center/lambda-rds-database-proxy

profile picture
EXPERT
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions