2
Answers

Write logs in AWS Log

Photo of Ritu

Ritu

1y
696
1

Hi

I want to write logs in AWS cloudwatch using lambda
I'm using ILambdalogger but getting error in logs

 

public string FunctionHandler(string input, ILambdaContext context)
    {
        var logger = context.Logger;
        sendLog(logger);       

        return input.ToUpper();
    }

    public static void sendLog(ILambdaLogger lambdaLogger)
    {
        resultToFile.errors = new List<ErrorModel>();
        resultToFile.errors.Add(new ErrorModel
        {
            CrDatTime = CommonHelper.ConvertDateTime_UTCtoEST(),
            LogLevel = LogLevel.Information.ToString(),
            MsgDetails = "1. AWS FunctionHandler Initialized",
            SourceMethodName = "FunctionHandler"
        });      

        if (resultToFile.errors != null)
        {
            lambdaLogger.LogInformation("Data executed successfully");
        }
        else
        {
            lambdaLogger.LogError("Error");
        }
    }

 

Please help.

Answers (2)

1
Photo of Deepak Rawat
122 15k 847.5k 1y

To write logs to AWS CloudWatch using Lambda and ILambdaLogger, you need to make sure your Lambda function has the necessary permissions to write logs to CloudWatch. Here are the steps you can follow to resolve the issue:

  1. Ensure your Lambda function has an IAM role attached that grants it permissions to write logs to CloudWatch. The role should have the AWSLambdaBasicExecutionRole policy attached, which includes the necessary permissions for writing logs.

  2. Confirm that you have configured the appropriate CloudWatch log group and log stream for your Lambda function. You can do this through the AWS Management Console or by using the AWS CLI.

  3. Check if you have included the necessary NuGet package in your Lambda function project. To use ILambdaLogger with CloudWatch logs, you need to include the AWS.Logger.AspNetCore package. Make sure it is added to your project's dependencies.

  4. Verify that you are using the correct logging methods provided by ILambdaLogger. In your code, you are using LogInformation and LogError, which are not part of ILambdaLogger. Instead, use LogLine to write log messages.

Here's an updated version of your code that addresses the above points:

 

public string FunctionHandler(string input, ILambdaContext context)
{
    var logger = context.Logger;
    sendLog(logger);

    return input.ToUpper();
}

public static void sendLog(ILambdaLogger lambdaLogger)
{
    try
    {
        resultToFile.errors = new List<ErrorModel>();
        resultToFile.errors.Add(new ErrorModel
        {
            CrDatTime = CommonHelper.ConvertDateTime_UTCtoEST(),
            LogLevel = LogLevel.Information.ToString(),
            MsgDetails = "1. AWS FunctionHandler Initialized",
            SourceMethodName = "FunctionHandler"
        });

        if (resultToFile.errors != null)
        {
            lambdaLogger.LogLine("Data executed successfully");
        }
        else
        {
            lambdaLogger.LogLine("Error");
        }
    }
    catch (Exception ex)
    {
        lambdaLogger.LogLine($"Error writing logs: {ex.Message}");
    }
}

By using LogLine instead of LogInformation and LogError, you will be able to write log messages to CloudWatch. Additionally, the code now includes error handling to log any exceptions that may occur during the logging process.

Make sure to deploy the updated Lambda function and check the CloudWatch logs for any errors or messages logged by your function.

Accepted
2
Photo of Amit Mohanty
16 52.2k 6.1m 1y
  1. Open the AWS Management Console and go to the AWS Lambda service.
  2. Select your Lambda function.
  3. Scroll down to the "Permissions" section and click on the role name under the "Execution role" column. This will open the IAM console in a new tab.
  4. In the IAM console, click on the "Attach policies" button.
  5. Search for and select the "AWSLambdaBasicExecutionRole" policy. This policy includes the necessary permissions for writing logs to CloudWatch Logs.
  6. Click on the "Attach policy" button to attach the policy to your execution role.
  7. Go back to the Lambda function tab and save your changes.

Once you have the necessary permissions, you can modify your code to write logs to CloudWatch using the ILambdaLogger interface.

public string FunctionHandler(string input, ILambdaContext context)
{
    var logger = context.Logger;
    sendLog(logger);

    return input.ToUpper();
}

public static void sendLog(ILambdaLogger lambdaLogger)
{
    try
    {
        resultToFile.errors = new List<ErrorModel>();
        resultToFile.errors.Add(new ErrorModel
        {
            CrDatTime = CommonHelper.ConvertDateTime_UTCtoEST(),
            LogLevel = LogLevel.Information.ToString(),
            MsgDetails = "1. AWS FunctionHandler Initialized",
            SourceMethodName = "FunctionHandler"
        });

        if (resultToFile.errors != null)
        {
            lambdaLogger.Log("Data executed successfully");
        }
        else
        {
            lambdaLogger.Log("Error");
        }
    }
    catch (Exception ex)
    {
        lambdaLogger.Log($"Error: {ex.Message}");
    }
}