Problem writing Lambda logs to CloudWatch, what could be wrong?











up vote
0
down vote

favorite












I'm with some problems trying to write my Lambda function logs to CloudWatch Logs. It seems it is not a problem of authorization as you can see below:



PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- logs:CreateLogGroup
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvent
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${FunctionName}:*


I've tried a broader policy as well with arn:aws:logs:*:*:* as resource like some examples I found.



The template works fine on CloudFormation and the resources are created as appropriate, when I execute the lambda function as test everything works fine, but the logs are not written. It creates the log group and the log stream, but nothing is inside the log stream.



This is not a problem with the Lambda function as well. I deployed the function manually before creating the template and the CloudWatch Logs works fine in this case. I copied the role and policy exactly as it is written in the manual approach to create the template.



Is it some kind of known bug in CloudFormation/SAM/Lambda? Or am I doing something wrong I didn't notice?



Please tell me if you need more information to understand the context. Thanks!










share|improve this question
























  • Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
    – Mrk Fldig
    Nov 18 at 19:50












  • Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
    – leonardo.teteo
    Nov 19 at 2:02












  • So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
    – Mrk Fldig
    Nov 19 at 19:45










  • The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
    – leonardo.teteo
    Nov 19 at 23:14










  • Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
    – K Mo
    Nov 20 at 16:08















up vote
0
down vote

favorite












I'm with some problems trying to write my Lambda function logs to CloudWatch Logs. It seems it is not a problem of authorization as you can see below:



PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- logs:CreateLogGroup
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvent
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${FunctionName}:*


I've tried a broader policy as well with arn:aws:logs:*:*:* as resource like some examples I found.



The template works fine on CloudFormation and the resources are created as appropriate, when I execute the lambda function as test everything works fine, but the logs are not written. It creates the log group and the log stream, but nothing is inside the log stream.



This is not a problem with the Lambda function as well. I deployed the function manually before creating the template and the CloudWatch Logs works fine in this case. I copied the role and policy exactly as it is written in the manual approach to create the template.



Is it some kind of known bug in CloudFormation/SAM/Lambda? Or am I doing something wrong I didn't notice?



Please tell me if you need more information to understand the context. Thanks!










share|improve this question
























  • Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
    – Mrk Fldig
    Nov 18 at 19:50












  • Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
    – leonardo.teteo
    Nov 19 at 2:02












  • So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
    – Mrk Fldig
    Nov 19 at 19:45










  • The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
    – leonardo.teteo
    Nov 19 at 23:14










  • Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
    – K Mo
    Nov 20 at 16:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm with some problems trying to write my Lambda function logs to CloudWatch Logs. It seems it is not a problem of authorization as you can see below:



PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- logs:CreateLogGroup
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvent
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${FunctionName}:*


I've tried a broader policy as well with arn:aws:logs:*:*:* as resource like some examples I found.



The template works fine on CloudFormation and the resources are created as appropriate, when I execute the lambda function as test everything works fine, but the logs are not written. It creates the log group and the log stream, but nothing is inside the log stream.



This is not a problem with the Lambda function as well. I deployed the function manually before creating the template and the CloudWatch Logs works fine in this case. I copied the role and policy exactly as it is written in the manual approach to create the template.



Is it some kind of known bug in CloudFormation/SAM/Lambda? Or am I doing something wrong I didn't notice?



Please tell me if you need more information to understand the context. Thanks!










share|improve this question















I'm with some problems trying to write my Lambda function logs to CloudWatch Logs. It seems it is not a problem of authorization as you can see below:



PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- logs:CreateLogGroup
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvent
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${FunctionName}:*


I've tried a broader policy as well with arn:aws:logs:*:*:* as resource like some examples I found.



The template works fine on CloudFormation and the resources are created as appropriate, when I execute the lambda function as test everything works fine, but the logs are not written. It creates the log group and the log stream, but nothing is inside the log stream.



This is not a problem with the Lambda function as well. I deployed the function manually before creating the template and the CloudWatch Logs works fine in this case. I copied the role and policy exactly as it is written in the manual approach to create the template.



Is it some kind of known bug in CloudFormation/SAM/Lambda? Or am I doing something wrong I didn't notice?



Please tell me if you need more information to understand the context. Thanks!







aws-lambda amazon-cloudformation amazon-cloudwatchlogs aws-sam






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 17:04









marc_s

566k12610921245




566k12610921245










asked Nov 18 at 14:06









leonardo.teteo

4326




4326












  • Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
    – Mrk Fldig
    Nov 18 at 19:50












  • Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
    – leonardo.teteo
    Nov 19 at 2:02












  • So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
    – Mrk Fldig
    Nov 19 at 19:45










  • The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
    – leonardo.teteo
    Nov 19 at 23:14










  • Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
    – K Mo
    Nov 20 at 16:08


















  • Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
    – Mrk Fldig
    Nov 18 at 19:50












  • Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
    – leonardo.teteo
    Nov 19 at 2:02












  • So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
    – Mrk Fldig
    Nov 19 at 19:45










  • The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
    – leonardo.teteo
    Nov 19 at 23:14










  • Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
    – K Mo
    Nov 20 at 16:08
















Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
– Mrk Fldig
Nov 18 at 19:50






Hrmm that's an odd one, how are you writing the logs? Have you got a github repo I could have a look at? I can share my github user name if you do.
– Mrk Fldig
Nov 18 at 19:50














Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
– leonardo.teteo
Nov 19 at 2:02






Thank you for the answer @MrkFldig . I do have, here is the link. Feel free to ask any question since it is not well documented yet. It is in Java and I use log4j appender to log.
– leonardo.teteo
Nov 19 at 2:02














So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
– Mrk Fldig
Nov 19 at 19:45




So shot in the dark here because I had the same problem Bunyan, whats the log level set at for log4j? You look like you're using INFO mainly, if the default for Log4j is a log level above that you won't see them? Check this out: docs.aws.amazon.com/lambda/latest/dg/… - try the system.out method OR prove what I'm saying change an info statement to error.
– Mrk Fldig
Nov 19 at 19:45












The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
– leonardo.teteo
Nov 19 at 23:14




The log level, as you can see in the log4j2.xml of the project is INFO. I used precisely the example you linked to to create my log4j2 appender, it is exactly the same, I didn't change anything. And I've said in the question, the CloudWatch Logs works perfectly fine when I deploy it manually, this problem only happens when I deploy it through CloudFormation.
– leonardo.teteo
Nov 19 at 23:14












Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
– K Mo
Nov 20 at 16:08




Are you deploying the Lambda as part of the same CloudFormation template? If so, have you attached a role that allows it to write to CloudWatch?
– K Mo
Nov 20 at 16:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










In the end I was able to solve the problem but removing the CloudWatch Logs permissions from the policy I wrote and using the Manage Policy from AWS:

ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"

With that the logs were wrote to the LogGroup normally.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361764%2fproblem-writing-lambda-logs-to-cloudwatch-what-could-be-wrong%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    In the end I was able to solve the problem but removing the CloudWatch Logs permissions from the policy I wrote and using the Manage Policy from AWS:

    ManagedPolicyArns:
    - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"

    With that the logs were wrote to the LogGroup normally.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      In the end I was able to solve the problem but removing the CloudWatch Logs permissions from the policy I wrote and using the Manage Policy from AWS:

      ManagedPolicyArns:
      - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"

      With that the logs were wrote to the LogGroup normally.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        In the end I was able to solve the problem but removing the CloudWatch Logs permissions from the policy I wrote and using the Manage Policy from AWS:

        ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"

        With that the logs were wrote to the LogGroup normally.






        share|improve this answer












        In the end I was able to solve the problem but removing the CloudWatch Logs permissions from the policy I wrote and using the Manage Policy from AWS:

        ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"

        With that the logs were wrote to the LogGroup normally.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 14 hours ago









        leonardo.teteo

        4326




        4326






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361764%2fproblem-writing-lambda-logs-to-cloudwatch-what-could-be-wrong%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga