skip to Main Content

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.

2

Answers


  1. Lambda logs are in Cloudwatch. You can follow this AWS guide to access them. You can also use SAM but that works a little differently. AWS also has a page for Lambda logging in Go.

    You can view logs in Cloudwatch in two ways, from the Console or from command line. If you use Console, refer to this guide by AWS. On command line, you can modify this bash script to take parameters.

    #!/bin/bash
    aws lambda invoke --function-name my-function --cli-binary-format raw-in-base64-out --payload '{"key": "value"}' out
    sed -i'' -e 's/"//g' out
    sleep 15
    aws logs get-log-events --log-group-name /aws/lambda/my-function --log-stream-name stream1 --limit 5
    
    Login or Signup to reply.
  2. And please note that AWS has not yet updated all docs.
    The recommended runtime for GO has changed:
    https://aws.amazon.com/de/blogs/compute/migrating-aws-lambda-functions-from-the-go1-x-runtime-to-the-custom-runtime-on-amazon-linux-2/
    And more things AWS did not tell you:
    https://www.go-on-aws.com/lambda-go/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search