Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda@Edge not logging

I have viewer-request and origin-response Lambda functions deployed to a CloudFront distribution, which are firing, but not logging to CloudWatch. I have spent a considerable amount of time researching this topic, and have run through all advice from other posts including:

  • Checking all regions for logs, as I know that they CloudWatch logs will be created in the region which the labmda@edge function runs. No logs in any of them.
  • I have checked that the AWSServiceRoleForCloudFrontLogger role exists.

Interestingly when I purposefully code in an error into one of Lambda functions, I do get logs created within a group named /aws/cloudfront/LambdaEdge/<cloudfront distribution id> containing error logs, however there is no output from the console.log statements here.

For the life of me I can't work out how I can enable logging of ALL requests, both successes and failures, to CloudWatch, containing my debug statements using console.log().

The AWSServiceRoleForCloudFrontLogger contains a single policy AWSCloudFrontLogger:

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

EDIT:

Below is the AWS role suggested by AWS support. I can confirm this worked and resolved the issue.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:*:*:*"
            ]
        }
    ]
}```
like image 966
mustdobetter Avatar asked Jul 11 '19 12:07

mustdobetter


People also ask

How do I view Lambda edge logs?

You can access the log files using the CloudWatch console or the CloudWatch Logs API. Lambda creates CloudWatch Logs log streams in the AWS Regions closest to the location where the function is executed. The log group name is formatted as: /aws/lambda/us-east-1 .

How does Lambda edge work?

Lambda@Edge runs your code in response to events generated by the Amazon CloudFront content delivery network (CDN). Just upload your code to AWS Lambda, which takes care of everything required to run and scale your code with high availability at an AWS location closest to your end user.

How do I enable Lambda logging?

Open the Functions page of the Lambda console. Choose a function. Choose Monitor. Choose View logs in CloudWatch.

How long does Lambda edge take to deploy?

Behavior defines how the Amazon CloudFront acts when the request hits the service. That is where Lambda@Edge functions are also defined. NOTE: Deployments and removals can take up to 30 minutes due to the CloudFront CDN propagation.


1 Answers

The issue most probably is that Lambda does not have the permissions to output the logs into CloudWatch.

Can you double check the Lambda function execution role permissions?

Related Link : Can't get AWS Lambda function to log (text output) to CloudWatch

Explanation

So there are two kinds of logs here, hence you have to provide permissions to CloudWatch at two different places.

  1. Logs that you put in your Lambda function (using console.log), since these logs are to be published by the function to CloudWatch, function execution role should have the permission to CloudWatch. This is true irrespective of who triggers the Lambda function.
  2. Now comes L@E, sometimes you might end up modifying request/response in a way that is not valid as per CloudFront. In these scenarios only ClodFront has the knowledge that you messed up(your Lambda function doesn't know this) and it publishes this knowledge in form of logs to CloudWatch. Now since this is a different entity, it needs it own permissions to push the logs to CloudWatch(which you had provided via AWSServiceRoleForCloudFrontLogger).
like image 91
vipulbansal91 Avatar answered Oct 05 '22 08:10

vipulbansal91