Call aws Lambda function without using API Gateway and EC2 Instance

后端 未结 3 807
不知归路
不知归路 2021-01-01 19:25

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?

相关标签:
3条回答
  • 2021-01-01 19:40

    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.

    0 讨论(0)
  • 2021-01-01 19:49

    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.

    0 讨论(0)
  • 2021-01-01 19:55

    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();
    
    0 讨论(0)
提交回复
热议问题