Maksim Sorokin
Maksim Sorokin

Reputation: 2404

Multiple functions in Spring Cloud Function on AWS lambda

I have a Spring Cloud Function application with two functions:

@Component
public class MyFunctionOne implements Function<Object, Boolean> {

    @Override
    public Boolean apply(Object o) {
        return true;
    }
}

@Component
public class MyFunctionTwo implements Function<Object, Boolean> {

    @Override
    public Boolean apply(Object o) {
        return true;
    }
}

I have both spring-cloud-starter-function-web and spring-cloud-function-adapter-aws in my project dependencies.

I would like to call both MyFunctionOne and MyFunctionTwo separately.

I can achieve this running locally in two ways. I can do this either calling function directly (since I am using spring-cloud-starter-function-web) by calling localhost:8080/myFunctionOne. Or I can use the Function Routing functionality by calling localhost:8080/functionRouter and supplying myFunctionOne in spring.cloud.function.definition http header. This works fine and I can both trigger MyFunctionOne and MyFunctionTwo separately.

I have deployed the module to AWS Lambda. How do I supply spring.cloud.function.definition dynamically? According to documentation I can use org.springframework.cloud.function.adapter.aws.FunctionInvoker as a AWS Lambda handler. Or, alternatively I can define my own SpringBootStreamHandler. However, it seems that it is not possible to define spring.cloud.function.definition dynamically.

Is there a way to choose a function in Spring Cloud Function deployed to AWS lambda?

Upvotes: 0

Views: 1195

Answers (2)

Tom Gregory
Tom Gregory

Reputation: 124

Yes, you can configure the function to use by adding a SPRING_CLOUD_FUNCTION_DEFINITION environment variable to the AWS Lambda function.

e.g. SPRING_CLOUD_FUNCTION_DEFINITION=myFunctionOne

In the AWS Console, you do this under Configuration > Environment variables.

During execution Spring reads the environment variable and routes the event accordingly.

Upvotes: 0

Oleg Zhurakousky
Oleg Zhurakousky

Reputation: 6126

So, we have a bit of an issue with routing on AWS and it is being fixed as we speak. You can follow this issue - https://github.com/spring-cloud/spring-cloud-function/issues/698.

In any event, we'll have a new release early next week (3.1.3-RELEASE) which will include the fixes, samples and docs to address the issue you're describing

Upvotes: 2

Related Questions