TacoVox
TacoVox

Reputation: 141

Micronaut AWS Lambda Injected Dependencies NULL

We created a Lambda with Micronaut AWS.

The Handler looks like this:

@Singleton
public class ValidationRequestHandler extends MicronautRequestHandler<S3Event, Integer> {
  @Inject private MessageService messageService;

  @Override
  public Integer execute(S3Event input) {
    messageService.init();
    [...]
  }
}

The MessageService class like this:

@Singleton
public class MessageService {

    private final SnsClient client;

    @Inject
    public MessageService(SnsClient client) {
        this.client = client;
    }

    public void init() {
       [...]
    }
}

Upon invoking the Lambda we get the following error:

java.lang.NullPointerException: java.lang.NullPointerException

Where the NullPointerException results from calling messageService.init(). Further debugging confirmed that messageService is indeed null.

So what are we missing here?

The project's gradle.build looks as follows:

plugins {
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.library") version "1.3.4"
}

version = "1.0.0"
group = "our.project.group"

repositories {
    mavenCentral()
}

ext {
    micronautAwsVersion = '2.4.0'
    junitVersion = '5.7.1'
    awsSdkVersion = '2.16.14'
    lambdaVersion = '1.2.1'
    lambdaEventsVersion = '3.7.0'
    mockitoVersion = '3.8.0'
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("our.project.group.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut.aws:micronaut-function-aws")
    implementation group: 'io.micronaut.aws', name: 'micronaut-aws-sdk-v2', version: micronautAwsVersion
    implementation group: 'com.amazonaws', name: 'aws-lambda-java-events', version: lambdaEventsVersion
    implementation group: 'software.amazon.awssdk', name: 'sns', version: awsSdkVersion

    testImplementation("io.micronaut:micronaut-http-client")
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
    testImplementation group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
    testImplementation group: 'org.mockito', name: 'mockito-core', version: mockitoVersion
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
    targetCompatibility = JavaVersion.toVersion("11")
}

jar {
    manifest {
        attributes (
                'Class-Path': configurations.compile.collect {it.getName()}.join(' ')
        )
    }
    from {
        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it) }
    }
    dirMode = 0777
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

Furthermore, the project uses lombok, as well as mapstruct. For simplicity, I omitted them from the gradle.build.

Upvotes: 1

Views: 1045

Answers (1)

GoodforGod
GoodforGod

Reputation: 54

Use should consider making injection through constructor, that is recommended way for GraalVM native applications.

@Singleton
public class ValidationRequestHandler extends MicronautRequestHandler<S3Event, Integer> {
  
  private final MessageService messageService;

  @Inject 
  public ValidationRequestHandler(MessageService messageService) {
     this.messageService = messageService;
  }

  @Override
  public Integer execute(S3Event input) {
    messageService.init();
    [...]
  }
}

Upvotes: 1

Related Questions