ScottG
ScottG

Reputation: 11111

Invoking an AWS Lambda multiple ways

I am following a demo and have created an Lambda in C# that is invoked via the AWSSDK. The Function handler looks like this:

  public async Task<bool> FunctionHandler(string fileName, ILambdaContext context)        

This works just fine. Now I want to see if I can call it using API Gateway. I understand that in order for that to work, I need to add APIGatewayProxyRequest to the function signature. Can I add that as a parameter or do I have to replace the variable 'fileName'?

How does this work when I want the function to be invoked both ways? Directly via the SDK or through APIGateway? Can I have one function that is invoked multiple ways?

Upvotes: 2

Views: 2123

Answers (1)

Lior Kirshner
Lior Kirshner

Reputation: 706

Lambda doesn't support function overloading, but only a specific function call. Furthermore, the main method expects to receive the content in the first argument, so it's either APIGatewayProxyRequest or filename in your example. It can be other object, based on the trigger origin, for example S3Event, if the Lambda function was invoked by S3.

You can define a generic method signature, like FunctionHandler(Stream inputStream, ILambdaContext context), that parse the argument inputStream to a APIGatewayProxyRequest object.
For example:

Amazon.Lambda.Serialization.Json.JsonSerializer jsonSerializer =
new Amazon.Lambda.Serialization.Json.JsonSerializer();

APIGatewayProxyRequest request = 
jsonSerializer.Deserialize<APIGatewayProxyRequest>(inputStream);

By that you'll be able to use the same method for several trigger types.

Upvotes: 6

Related Questions