0

Just started with using AWS Lambda's. I'm writing my functions in Java. I'd like to know if you can pass parameters to an AWS Lambda through the API Gateway? My lambda function basically makes a call to a webservice which returns JSON, create's POJO's from the JSON and then a CSV file which I upload to S3. Now this webservice you could pass productId if you wanted to, if you don't it just returns all products.

This would return the product with id of 123456 www.likssmark.com/test/api/getOrders?productId=123456

This would return all orders as JSON payload: www.likssmark.com/test/api/getOrders

How do I pass productId into my java lambda? The lambda is triggered via cloud watch on a schedule - I've been testing it using Postman.

Hope this makes sense?

Many thanks for any advice.

9
  • You want the function to be invoked via API Gateway for API requests but also from CloudWatch Events vis direct invocation, yes? They'd present parameters in different ways, but I don't see why you can't do this. For CW Events you can provide input data in JSON when configuring the Lambda as a target. Did you try that? Commented Oct 26, 2020 at 20:51
  • FYI if your Lambda function only does what you mentioned above (get JSON from a remote site and write a CSV to S3) then I wouldn't write it in Java. Commented Oct 26, 2020 at 22:37
  • Thanks @jarmod, but why wouldn't you write it in Java, I did originally do this in JS. Commented Oct 27, 2020 at 7:51
  • Java is slower to load in Lambda (higher cold start times), is generally not optimized for simple HTTP and webapp authoring, doesn't have native JSON handling, requires compilation, is heavyweight etc. Just my opinion. Commented Oct 27, 2020 at 10:44
  • Would you recommend Javascript mostly? Commented Oct 27, 2020 at 12:54

3 Answers 3

1

If you only need to use Cloudwatch, just pass a JSON string to your Lambda:

enter image description here

In your Lambda you can then pull out the data:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

...

public class ProductIdLambda implements RequestStreamHandler {

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(inputStream);

        String productId = rootNode.path("productId").asInt();

This pulls the productId out of the InputStream.

If you need both CloudWatch events and API Gateway integration you can either have two different Lambda's or, to the suggestion @f7o made, introspect the incoming stream for an API Gateway call. You could have something like:

String httpMethod = rootNode.path("httpMethod").asText();
if( httpMethod != null )  // then we were called by API Gateway

The input from API Gateway will include an optional parameter in the input JSON:

"queryStringParameters": {
    "productId": "12345"
},

that you can then get your productId from.

Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks, very useful, much appreciated @stdunbar It will only be cloudwatch calling it eventually. Can you dynamically put the product id value in as this would change?
@SteveGreen - yes, of course. You can update the Input through the AWS CLI or through the console. Alternatively, you can skip cloudwatch rules all together and call your Lambda directly with the CLI. See this references for more details.
1

You can use AWS API-Gateway to pass parameters to your AWS lambda service.
This AWS documentation describes it:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

In my German blog I wrote an article about how to implement an AWS lambda service with Java and Spring Boot. Here I am also passing parameters over API Gateway to AWS lambda service:
https://agile-coding.blogspot.com/2020/09/aws-lambda-services-mit-spring-boot.html

Comments

1

There are multiple ways to integrate lambda within an API gateway. For starters I would create a simple HTTP API with either a default route or a specific route. Attach a lambda integration to the route.

This should proxy the http request to your lambda function. You lambda handler will receive an event which contains information about the request as path, cookies, ... and also your query parameters. See the documentation for details on the passes json (https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html).

To determine who is actually calling the function (cloudwatch, api gateway) just test the content of the event for some fields before parsing/reading it to make sure you respond appropriate.

1 Comment

Thanks for this, will look into it. The lambda's are only going to get called via cloudwatch eventually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.