AWS Lambda created from assets in CDK throws Class not found exception

Viewed 671

I'm trying to create and upload a lambda using the AWS CDK for Java. The following code is what I use to create the lambda in the Stack. However this throws a java.lang.ClassNotFoundException when trying to run the lambda.

    private fun createUserLambda(): Function {
        val dirPath = System.getProperty("user.dir")
        val lambdaDir = File("$dirPath/src/com/test/lambda")
        val props = FunctionProps.builder()
            .code(Code.fromAsset(File(lambdaDir, "handler").toString()))
            .handler(AddUser::class.java.canonicalName + "::" + AddUser::handleRequest.name)
            .runtime(Runtime.JAVA_8)
            .build()
        return Function(this, "UserCreateLambda", props)
    }

I can see that the code exists which I download the zip file from the S3 bucket created which contains the assets. Not entirely sure what's missing here but any help would be greatly appreciated! Thanks

1 Answers

It looks like your Code.fromAsset() call creates an artifact of source files. Lambda requires a compiled jar for Java runtimes.

Related