Skip to content

Latest commit

 

History

History

function-sample-aws-routing

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

This example demonstrates routing capabilities of spring-cloud-function when deployed as AWS Lambdas.

Usually when you deploy configuration (non-custom runtime) with a single function it is automatically recognized and bound as AWS Lambda by org.springframework.cloud.function.adapter.aws.FunctionInvoker (FunctionInvoker).

However when you have multiple functions present in your configuration you need to tell FunctionInvoker the target function definition.

You can do so in two different ways.

  1. You can provide spring_cloud_function_definition environment variable setting its value to the desired function definition, which could also be composition (e.g., spring_cloud_function_definition=foo|bar).

Note
Keep in mind though that since AWS does not allow dots . and/or hyphens`-` in the name of the environment variable, you can benefit from boot support and simply substitute dots with underscores and hyphens with camel case. So for example spring.cloud.function.definition becomes spring_cloud_function_definition and spring.cloud.function.routing-expression becomes spring_cloud_function_routingExpression.
  1. A more dynamic and recommended approach would be to fallback on auto routing capabilities of spring-cloud function’s in AWS environment. Basically every time you have more then one function in your configuration, the framework will bind [Routing Function](https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/spring-cloud-function.html#_function_routing_and_filtering) as AWS Lambda, and all you need to to is provide a routing instruction via Message headers or environment variables. The instructions could themselves be very dynamic, since we support both SpEL and registering a callback interface. For more details on routing mechanisms please refer to [Function Routing and Filtering](https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/spring-cloud-function.html#_function_routing_and_filtering) section.

In this example we have configuration with two functions; uppercase and reverse. When executing from AWS Lambda functions dashboard you can simply provide one of the mentioned properties as environment variables via Configuration tab. For example, you can set spring_cloud_function_routingExpression environment variable with the value of literal; SpEL expression 'uppercase' (not the single quotes).

As for API Gateway, you can also pass routing instructions as Message headers by proving them as HTTP headers. You can test it with API Gateway dashboard or (once deployed), you can for example POST to it via curl. Here is the example of curl command

curl -X POST  https://[. . .].execute-api.eu-west-3.amazonaws.com/route/aws-routing-gw -H "spring.cloud.function.definition: uppercase" -H "Content-Type: application/json"  -d '"foo"'