19

Can we call lambda function from outside aws without using API Gateway? I want to call lambda function directly from outside aws services is it possible?

Rukamesh Kumar
  • 354
  • 1
  • 2
  • 7
  • what do you mean when you said "outside aws services"? Are you trying to call aws lambda programmatically in some language? – kaushalparik27 May 05 '17 at 14:24
  • It would be very helpful if you were to include full information (eg that you want to use Python) in your initial question. For tips on writing questions, please see: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – John Rotenstein May 09 '17 at 07:25
  • Why do you wish to use HTTP(s) without using an SDK and without using API Gateway? They are the best ways to do it, so you'd need a really good reason to not use them. – John Rotenstein May 09 '17 at 07:26

3 Answers3

22

AWS Lambda functions can be triggered by:

  • Events happening on other AWS services (eg Object uploaded to an Amazon S3 bucket)
  • A message being sent to AWS API Gateway (eg a REST call)
  • A schedule in Amazon CloudWatch Events
  • A direct API call

From Supported Event Sources documentation:

In addition to invoking Lambda functions using event sources, you can also invoke your Lambda function on demand. You don't need to preconfigure any event source mapping in this case. However, make sure that the custom application has the necessary permissions to invoke your Lambda function.

For example, user applications can also generate events (build your own custom event sources). User applications such as client, mobile, or web applications can publish events and invoke Lambda functions using the AWS SDKs or AWS Mobile SDKs such as the AWS Mobile SDK for Android.

So, anything on the Internet can invoke a Lambda function, but it will need to use AWS credentials to authenticate.

John Rotenstein
  • 165,783
  • 13
  • 223
  • 298
  • 7
    I mean that Can we call lambda function from outside aws using http or https without using API Gateway? – Rukamesh Kumar May 08 '17 at 10:36
  • As per above, you can call a Lambda function via an API call. This call is sent over HTTPS, but you must use the AWS SDK to perform the call for you because the authentication syntax is complex. You didn't mention your preferred language, but as an example, [here is the call for Java](http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/lambda/AWSLambda.html#invoke-com.amazonaws.services.lambda.model.InvokeRequest-). – John Rotenstein May 08 '17 at 14:56
  • I want to do this in python and want to call lambda function without using AWS SDKS only with http or https. – Rukamesh Kumar May 09 '17 at 07:22
  • Then you need to use the API gateway. – paul jerman Jan 29 '18 at 20:39
  • 1
    Can we make some credentials that can only call a given Lambda function? – Petrus Theron Oct 14 '18 at 10:55
  • @PetrusTheron Please create a new question rather than asking a question in a comment on an old question. – John Rotenstein Oct 14 '18 at 10:57
  • My comment flows from the bolded text in your answer. If you amended your answer with details on how to expose a Lambda function with safe public credentials, your answer would be even better and solve the OP's problem. – Petrus Theron Oct 14 '18 at 11:01
  • 1
    Yes, you can make credentials that can only call a given Lambda function. Simply give `lambda:invoke` permissions with a resource equal to the Lambda functions' ARN. – John Rotenstein Oct 14 '18 at 11:20
1

You can also use an application load balancer to call the lambda. This option is useful when you have timeouts larger than 30 seconds. To use this option you need to add a trigger to the lambda function and select Application Load Balancer and then procede with the configuration which is not hard.

This is awful compare to using the API Gateway, because it creates a target group for each lambda but well... its sometimes useful.

roccolocko
  • 175
  • 1
  • 12
-3

This is how call AWS lambda function without API GateWay in Android.

Interface Class

 @LambdaFunction(functionName = "lambdafunctionName", invocationType = "RequestResponse")
 String lambdafunctionName(String str);

Android Code (Java)

  new AsyncTask<Response, Void, String>() {
   @Override
    protected String doInBackground(Response... params) {
    try{
        CognitoCachingCredentialsProvider cognitoProvider = new 
        CognitoCachingCredentialsProvider(MainActivity.this, "#identitypoolID", #Region);     
    // creates an invocation factory
         LambdaInvokerFactory factory = new LambdaInvokerFactory(MainActivity.this,
                                    #Region, cognitoProvider);

    // create a proxied object of lambdafunctionName
          MyInterface lambdaFuction = factory.build(MyInterface.class,
                                    new LambdaJsonBinder());

   // invoke it just like a regular method
         String AWSResponse = lambdaFuction.lambdafunctionName(#Parameter to AWS lambda) ;
                            return AWSResponse;

     }catch(Exception e){

     Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                          return null;
                        }

                    }
 @Override
  protected void onPostExecute(String result) {
   if (result == null) {
   Toast.makeText(MainActivity.this,"Error !", Toast.LENGTH_LONG).show();
   return;
   }
  Toast.makeText(MainActivity.this,"Response From AWS " +  result, Toast.LENGTH_LONG).show();
                    }
                }.execute();
Thisara Subath
  • 523
  • 4
  • 15