typelevel / feral

Feral cats are homeless, feral functions are serverless
Apache License 2.0
176 stars 41 forks source link

Can't print logs using `IO.println` #400

Closed keuhdall closed 1 year ago

keuhdall commented 1 year ago

It seems that I can't get logs from my lambda built with Feral. I've been reproducing with this simple example (using Scala 3.3.0 and Feral 0.2.3):

object Lambda extends IOLambda[Unit, Unit] {
  override def handler: Resource[IO, LambdaEnv[IO, Unit] => IO[Option[Unit]]] =
    for {
      _ <- IO.println("trying to log something").toResource
    } yield { (env: LambdaEnv[IO, Unit]) =>
      IO.println("trying to log something else") *> IO.unit.map(_.some)
    }
}

When executing sam local invoke LambdaFunction

I get:

Invoking hawk.Lambda::handler (java17)
Decompressing
/Users/me/Documents/path/to/my/uberjar.jar
Local image is up-to-date
Using local image: public.ecr.aws/lambda/java:17-rapid-arm64.

Mounting /private/var/folders/h8/1xxcb8j53q9gvtdvl0t674y00000gp/T/tmpsq7sev9x as
/var/task:ro,delegated, inside runtime container
START RequestId: 78653470-2d5a-4292-ab79-caf86ef6a0f6 Version: $LATEST
END RequestId: 78653470-2d5a-4292-ab79-caf86ef6a0f6
REPORT RequestId: 78653470-2d5a-4292-ab79-caf86ef6a0f6  Init Duration: 0.06 ms  Duration: 670.52 ms Billed Duration: 671 ms Memory Size: 1024 MB    Max Memory Used: 1024 MB
{}%

The jar was build using sbt assembly and the following SAM template was used:

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: hawk.Lambda::handler
      CodeUri: path/to/my/uberjar.jar
      Runtime: java17
      Architectures:
        - arm64
      MemorySize: 1024
      Events:
        S3Event:
          Type: S3
          Properties:
            Bucket:
              Ref: MyS3Bucket
            Events: s3:ObjectCreated:*
  MyS3Bucket:
    Type: AWS::S3::Bucket
Outputs:
  LambdaFunction:
    Description: Lambda function ARN
    Value: !GetAtt LambdaFunction.Arn
  S3Bucket:
    Description: S3 bucket name
    Value: !Ref MyS3Bucket

On the other hand, this minimal Java example works fine:

public class HelloWorldLambda implements RequestHandler<Void, Void> {
    @Override
    public Void handleRequest(Void input, Context context) {
        System.out.println("Hello world");
        return null;
    }
}

When executing sam local invoke HelloWorldLambdaFunction I get:

Invoking org.example.HelloWorldLambda::handleRequest (java17)
Decompressing /Users/me/Documents/path/to/my/uberjar.jar
Local image is up-to-date
Using local image: public.ecr.aws/lambda/java:17-rapid-arm64.

Mounting /private/var/folders/h8/1xxcb8j53q9gvtdvl0t674y00000gp/T/tmpc8ca3r5b as /var/task:ro,delegated, inside runtime container
START RequestId: e789cd07-f461-4566-a7c3-8e51c6e3481a Version: $LATEST
Hello world
END RequestId: e789cd07-f461-4566-a7c3-8e51c6e3481a
REPORT RequestId: e789cd07-f461-4566-a7c3-8e51c6e3481a  Init Duration: 0.13 ms  Duration: 129.56 ms Billed Duration: 130 ms Memory Size: 512 MB Max Memory Used: 512 MB
null%

The jar was build using mvn clean package and the following SAM template was used:

Resources:
  HelloWorldLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: HelloWorldLambdaFunction
      Handler: org.example.HelloWorldLambda::handleRequest
      Runtime: java17
      Architectures:
        - arm64
      CodeUri: path/to/my/uberjar.jar
      MemorySize: 512
      Timeout: 10
      Policies:
        - AWSLambdaBasicExecutionRole

Outputs:
  HelloWorldLambdaFunction:
    Description: HelloWorld Lambda Function ARN
    Value: !GetAtt HelloWorldLambdaFunction.Arn
armanbilge commented 1 year ago

Logging seems to work with my setup in https://github.com/armanbilge/sandbox/commit/cf909c5b73e832d45e78ddbf9567dfd90d99462f.

$ sam local invoke
Invoking MyLambda (java17)                                                                                                     
Decompressing /workspace/sandbox/lambda.jar                                                                                    
Local image is up-to-date                                                                                                      
Using local image: public.ecr.aws/lambda/java:17-rapid-x86_64.                                                                 

Mounting /tmp/tmpssns_67h as /var/task:ro,delegated, inside runtime container                                                  
START RequestId: 852ef67f-b7e7-475c-bcda-3730d292f621 Version: $LATEST
trying to log something
trying to log something else
END RequestId: 852ef67f-b7e7-475c-bcda-3730d292f621
REPORT RequestId: 852ef67f-b7e7-475c-bcda-3730d292f621  Init Duration: 0.12 ms  Duration: 797.27 ms     Billed Duration: 798 msMemory Size: 128 MB     Max Memory Used: 128 MB
{}
keuhdall commented 1 year ago

Turns out the issue was coming from Handler: hawk.Lambda::handler, which should either be just the main class (Handler: hawk.Lambda), or using handleRequest (Handler: hawk.Lambda::handleRequest)