Call Lambda function with proxy integration from another Lambda function without passing through the Api Gateway

Viewed 866

I have deployed two lambda functions with proxy integration on AWS. Each of them is referenced from its corresponding API Gateway entry using the proxy wildcard:

enter image description here

The idea is to have two APIs, one per function, let's suppose this is what they offer:

API 1 -> Lambda 1 -> ms-user

GET /users
GET /users/{userId}
POST /user

API 2 -> Lambda 2 -> ms-departments

GET /departments
POST /departments

Certain operations from Lambda 2 have to call Lambda 1, e.g: GET /departments needs to get user information, GET /users/{userId}.

I could make Lambda 2 invoke the API of Lambda 1, but I am wondering is there is a way to call that lambda directly with the AWS Java SDK without passing through the Gateway again. In that case how can I specify the endpoint of the target Lambda?

API 2 -> Lambda 2 -> Lambda 1

instead of

API 2 -> Lambda 2 -> API 1 -> Lambda 1

1 Answers

The key to achieve this is to pass a request containing certain expected fields, e.g:

{
    "path": "/users/1234D",
    "httpMethod": "GET",
    "headers":  null,
    "multiValueHeaders": {
        "Host": [
            "xxxx.execute-api.eu-west-1.amazonaws.com"
        ]
    }
}

The Java/Kotlin code would be:

        val region = Regions.fromName("eu-west-1")
        val builder = AWSLambdaClientBuilder.standard().withRegion(region)
        val client = builder.build()
        val req = InvokeRequest().withFunctionName("ms-user")
                .withPayload("""{
                                      "path": "/users/${req.userId}",
                                      "httpMethod": "GET",
                                      "headers":  null,
                                      "multiValueHeaders": {
                                        "Host": [
                                          "xxx.execute-api.eu-west-3.amazonaws.com"
                                        ]
                                      }
                                    }""")
        val result = client.invoke(req)
Related