Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the AWS lambda name on runtime in Java

I really like the approach of calling AWS lambdas from Java described in this blog post.
However, if I have 3 environments (int/test/live), and on each of them the lambda has a slightly different name (created via cloudformation), I can't think of a way to have one interface, and call lambda with a different name depending on the environment.
I am using Spring, and so if I could do something like:

@Component
interface MyLambdas {
    @Value("${name}")
    String name;

    @LambdaFunction(name = name)
    String callMyLambda(String stuff);
}

//and then
service = LambdaInvokerFactory.build(MyLambdas.class, lambda);

But obviously I can't do this on an interface, this won't be a bean! Is there any way at all to do this? I feel like I hit a brick wall...


Right now I am calling lambda "the old way":

String readLambdaName = "My_Lambda";

ObjectMapper objectMapper = new ObjectMapper();
AWSLambdaClient lambda = new AWSLambdaClient();
lambda.configureRegion(Regions.EU_WEST_1);

String json = objectMapper.writeValueAsString(request);
InvokeRequest req = new InvokeRequest()
        .withFunctionName(readLambdaName)
        .withPayload(json);

InvokeResult res = lambda.invoke(req);
int result = objectMapper.readTree(res.getPayload().array()).asInt();

Obviously with some exception handling here and there. This is not as nice of a solution tho...


For anyone following this, I have submitted an issue and a solution on aws-sdk github. hopefully something similar to my solution will make it to the next release of the SDK...

like image 989
Daniel Gruszczyk Avatar asked Jan 14 '16 09:01

Daniel Gruszczyk


People also ask

How do I find my Lambda region name?

To get the AWS Region where your Lambda Function is running you will need to import the os module. Then from the os module, you need to get the value of AWS_REGION from the environ mapping variable. This will return the AWS Region where the Lambda Function is running.

How do you calculate Lambda runtime?

01 Sign in to the AWS Management Console. 02 Navigate to Amazon Lambda console at https://console.aws.amazon.com/lambda/. 03 In the left navigation panel, under AWS Lambda, choose Functions. 04 Click on the name (link) of the function that you want to examine.

What is AWS Lambda runtime?

Amazon Linux Lambda re-uses the execution environment from a previous invocation if one is available, or it can create a new execution environment. A runtime can support a single version of a language, multiple versions of a language, or multiple languages.

Can you identify the programming languages supported by the Lambda runtime?

Q: What languages does AWS Lambda support? AWS Lambda natively supports Java, Go, PowerShell, Node. js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions.


2 Answers

I resolved it by overriding the function name using "lambdaFunctionNameResolver".

//my lambda interface
public interface MyLambdaService {
    @LambdaFunction
    ApiGatewayProxyResponse execute(ApiGatewayRequest bit);
}

this is how to create the lambda client:

MyLambdaService lambdaService = LambdaInvokerFactory.builder().lambdaClient(AWSLambdaClientBuilder.defaultClient())
                    .lambdaFunctionNameResolver((method, annotation, config) -> "ENV_SPECIFIC_FUNCTION_NAME").build(MyLambdaService.class);

or

My method call "generalConfigHelper.getString("function_name"))" figure outs the correct function name according to the env (dev/qa/prod).

MyLambdaService lambdaService = LambdaInvokerFactory.builder().lambdaClient(AWSLambdaClientBuilder.defaultClient())
                .lambdaFunctionNameResolver((method, annotation, config) -> generalConfigHelper.getString("function_name")).build(MyLambdaService.class);

Official aws issue: https://github.com/aws/aws-sdk-java/pull/603

like image 123
meeza Avatar answered Oct 23 '22 17:10

meeza


Some people might find it useful, this functionality is now included in 1.10.51 with use of LambdaFunctionNameResolver

like image 21
Daniel Gruszczyk Avatar answered Oct 23 '22 16:10

Daniel Gruszczyk