0

I've created an AWS Lambda based Spring Cloud Function with multiple functions and it works fine when I define exactly one "spring_cloud_function_definition" environment variable in the AWS Lambda Configuration panel. These functions' input and output is a Message<?>, so the payload and http headers can be accessed runtime as well.

I'd like to use this AWS Lambda function in different API Gateway methods, so for example:

  • /item [POST] -> saveItem should be called
  • /item [DELETE] -> deleteItem should be called
  • ... and so on...

https://cloud.spring.io/spring-cloud-static/spring-cloud-function/3.0.0.RC1/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures

I found this documentation on Spring.io which says it's feasible:

If the input argument is of type Message<?>, you can communicate routing instruction by setting one of spring.cloud.function.definition or spring.cloud.function.routing-expression Message headers. For more static cases you can use spring.cloud.function.definition header which allows you to provide the name of a single function (e.g., …​definition=foo) or a composition instruction (e.g., …​definition=foo|bar|baz)

So the following test event should run in AWS Lambda without any failure:

{ "method": "POST", "body": "{ "key" : "value" }", "headers": { "spring.cloud.function.definition": "saveItem" } }

but of course it throws an exception:

 No function defined: java.lang.IllegalStateException
 java.lang.IllegalStateException: No function defined
    at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.apply(AbstractSpringFunctionAdapterInitializer.java:187)
    at org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:51)
    at org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler.handleRequest(SpringBootApiGatewayRequestHandler.java:140)
    at org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler.handleRequest(SpringBootApiGatewayRequestHandler.java:43)

I've tried many configurations (application.properties, AWS Lambda env property, header, etc.) but nothing happened:

  • spring.cloud.function.routing-expression=headers.function_name
  • setting spring.cloud.function.definition=saveItem

What could be the problem? What is the best configuration in this case? Any help would be appreciated!

Used technologies:

  • Cloud: AWS
  • Cloud components: Lambda + API Gateway
  • AWS Lambda handler class: org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler
  • AWS Lambda input and output events: APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent
  • Java: 8
  • Spring Cloud Function version: 3.0.6.RELEASE
peter.szrnka
  • 43
  • 1
  • 10
  • Hey, sorry for the delay. It is a bit difficult to determine what is going on even though your explanation seem to be full of details. I'll take a look and see if I can spot something, meanwhile, can you please try [this example](https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-aws-routing). This will help to establish the base – Oleg Zhurakousky Jan 03 '22 at 10:13

1 Answers1

1

I've updated to v3.1.3 and switched to FunctionInvoker and I've added the function definition to the request template mapping header, and it worked fine.

Anyways, thanks for the help. :)

peter.szrnka
  • 43
  • 1
  • 10