Search code examples
amazon-web-servicesgologgingaws-lambda

Role to assign to Lambda function (was: Check the logs when building Lambda functions with Go)


I'm following the official Building Lambda functions with Go to its blank-go/ demo, from which you can see that the handleRequest function almost use log.Print in its whole process:

func handleRequest(ctx context.Context, event events.SQSEvent) (string, error) {
    // event
    eventJson, _ := json.MarshalIndent(event, "", "  ")
    log.Printf("EVENT: %s", eventJson)
    // environment variables
    log.Printf("REGION: %s", os.Getenv("AWS_REGION"))
    log.Println("ALL ENV VARS:")
    for _, element := range os.Environ() {
        log.Println(element)
    }
    // request context
    lc, _ := lambdacontext.FromContext(ctx)
    log.Printf("REQUEST ID: %s", lc.AwsRequestID)
    // global variable
    log.Printf("FUNCTION NAME: %s", lambdacontext.FunctionName)
    // context method
    deadline, _ := ctx.Deadline()
    log.Printf("DEADLINE: %s", deadline)
. . .

However, I didn't find where I can read those logs. Please help.

UPDATE:

I've ran/tested my blank-go/ function many times, but each time I click on the log link from my test:

enter image description here

I'm getting an empty view:

enter image description here

IE, "There are no log streams."

So I am checking the right place, thus now the question becomes, why there is no log despite that I've ran my test several times?


Solution

  • Please see the Lambda logging prerequisites information in the official documentation. Specifically, you need to assign a role to the Lambda function that has access to create logs in CloudWatch. Amazon provides a role named AWSLambdaBasicExecutionRole for Lambda functions that includes the necessary permissions to send logs to CloudWatch.