I've got two simple functions like this one. The o...
# http4k
a
I've got two simple functions like this one. The only difference is the request path.
Copy code
Lambda.Http().invokeFunction<VehicleInfo>(
    name = FunctionName.of(lambdaFunctionName),
    req = Request(GET, "/data/$vin"),
    autoMarshalling = configurableKotlinxSerialization,
)
One succeeds, other one gets error:
Copy code
Serializer for class 'MemoryRequest' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
: kotlinx.serialization.SerializationException
kotlinx.serialization.SerializationException: Serializer for class 'MemoryRequest' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
creating lambda with
Copy code
Lambda.Http()
and json:
Copy code
val configurableKotlinxSerialization = ConfigurableKotlinxSerialization(
    json = {
        ignoreUnknownKeys = true
        isLenient = false
        prettyPrint = true
        asConfigurable().withStandardMappings().done()
    },
)
a
If I understand your problem correctly, you're calling the http4k-connect Lambda
InvokeFunction
operation, passing in an http4k
Request
as the payload, and overriding the marshaller from Moshi to kotlinx-serialization. When you do this, you get a kotlinx-serialization error, because kotlinx serialization always requires serialization adapters to be generated at compile-time via the
@Serializable
annotation and the appropriate gradle plugin. You almost certainly would not have had the opportunity to annotate the built-in Http4k
Request
class. If you want to continue using kotlinx-serialization, you will need to come up with a DTO to encapsulate the
Request
, and mark that with the annotation to generate the kotlinx serialization adapter. You can likely get around this issue by continuing to use Moshi as is default, or perhaps even overriding with Jackson.
a
Thank you for the response Andrew. I think i got somewhere. The
req
in
invokeFunction
is type
Any
, earlier i passed the Request and it did no like it, but i changed the Request to String and the error changed to actual failed response, which is progress.
a
Yeah, so assuming your intention is send a representation of an HTTP request as the payload, then coming up with something like this should work, so long as you have the kotlinx.serialization gradle plugin applied correctly.
Copy code
@Serializable
data class RequestDto(
  val method: Method,
  val uri: String,
  val bodyBase64: String
)
a
my intensions are to call lambda from another lambda in private vpc, so i wont be hitting the api gateway
a
Well, FYI, there is such a thing as private gateways, but calling the lambda directly is also an option. Do you know what the exact payload format of the target function is?
a
Originally in the spring (which i migrate to pure kotlin), it was a Get request, i also know the path (for example : "/get/user") and the function name.
a
I just want to make sure you understand that lambdas don't actually respond to HTTP requests. You send it an input string and get an output string. If your target function was ever working with an API Gateway in front of it, then the HTTP request and response would have been represented by some JSON structured defined by AWS, and your Spring application would have been using an adapter to convert the JSON into a spring request/response. So sending an http4k
Request
as the payload is almost certainly not what you want to do here.
What you probably need to do is send the JSON structure that AWS was using for your API gateway integration. There's a few different versions of this structure, but AWS publishes a java library with all of them. https://github.com/aws/aws-lambda-java-libs?tab=readme-ov-file#java-objects-of-lambda-event-sources---aws-lambda-java-events You won't be able to use kotlinx-serialization to serialize them unless you copy and annotate the code, but moshi should be able to handle them fine.
I should note that at this point, your issue is about the fundamentals of Lambda payloads and triggers, and doesn't really have much to do with http4k. But you're welcome to discuss it here.
j
You may find these useful: https://github.com/http4k/http4k-connect/tree/master/amazon/lambda. - invoking lambdas with http4k https://www.http4k.org/guide/tutorials/serverless_http4k_with_aws_lambda/ - implementing lambdas with http4k