Call AWS Lambda function from React-Native - amazon-web-services

I have build a simple function in AWS Lambda which sends sms using Twilio service. I now want to call that function from my React-Native app.
Do you have any suggestion to that?

There are two ways to invoke the AWS Lambda from your React-Native application
Direct invocation using AWS browser SDK
You can use lambda#invoke API to invoke your Lambda function from your React-Native app. Catch here is that you'll have to ship AWS credentials with your app. These credential will have permission to invoke the Lambda function.
Indirect onvocation using API Gateway
You can gate your Lambda function behind an API Gateway (API Gateway + Lambda integration). Then you can use standard JavaScript HTTP utilities to make REST calls to you API Gateway resource. This API Gateway resource will be responsible for invoking your Lambda function.
I prefer second method because API Gateway provides throttling support and we don't have to ship credentials with the app.

Related

How to use aws lambda without HTTP api?

Since there is aditional costs for using HTTP and REST apis on AWS lambda, i would like to know if i could make AWS Lambda receive gets and posts without the need of these HTTP API services.
In this example it seems to be possible:
https://github.com/serverless/examples/tree/master/aws-node-simple-http-endpoint
You will need to use the API Gateway to expose your lambda. Your example is actually using an API Gateway, because the endpoint is execute-api.us-east-1.amazonaws.com and that is the Amazon API Gateway Data Plane.
Just to be clear; if you need to expose the Lambda externally you need to use the API Gateway. If the Lambda needs to be invoked internally then you don't need the API GW.
Best regards
Lambda also exposes a client API in all languages. Therefore, you can invoke a Lambda function by using the client API (not use API Gateway if you prefer). For example, assume you want the ability to invoke a Lambda function from a Java web app. In this situation, you can use the LambdaClient object to do so. You can find an example here:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/lambda/src/main/java/com/example/lambda/LambdaInvoke.java

Can AWS SQS Service trigger API Gateway Proxy?

I working on a project and I need to trigger a proxy API whenever a message is received by the AWS SQS Service. I went through couple of articles and found that API to SQS is possible but did not find anything related to SQS triggering API Gateway. Can somebody please guide.
It is not possible to invoke a Lambda function asynchronously with a proxy integration. You can do this with Lambda non-proxy integration.
Set up asynchronous invocation of the backend Lambda function - https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html
Another workaround is to use another function that is invoked synchronously by the API Gateway API and have that function invoke the function that is part of the Spring boo application asynchronously using the Lambda Invoke API or SDK equivalent.
https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
For instance, you want Function A to be invoked asynchronously. To achieve this you would have the API Gateway API invoke Function B synchronously and have Function B invoke Function A asynchronously with the Invoke API.
Step Functions also cant be used as a workaround to invoke the REST APIs as this would not help. The Lambda functions are still invoked by the API Gateway API so you would still run into the limitation of a proxy integrations not being able to invoke Lambda asynchronously. You then decided that ECS would better suit your case as Lambda is not a good fit.

Create an api in api gateway to invoke a lambda function

I am working on a project and trying to use API Gateway to invoke a lambda function. The lambda function is used to update a DynamoDB item. The DynamoDB table is used to keep a running count of visitors to a web page. I need to create an API to invoke the lambda function but I'm not sure how to create the API. Any assistance is appreciated.
General steps would be:
Create AWS_PROXY integration between API Gateway and your Lambda function. The example of this is in the AWS tutorials: Set up Lambda proxy integrations in API Gatewa and in Tutorial: Build a REST API with HTTP proxy integration
Add/amend execution role to your function allowing it to access DynamoDB. This is exemplified in the AWS tutorial: Using AWS Lambda with Amazon DynamoDB.
Test the API. It can be done directly in API gateway console, or using external tools such as curl or Postman.
I figured out my issue. In my lamdba function, I needed to change the output to a JSON object. Once I made the change, I was able to get my API working. Here is a link to the fix.

Can I use client certificate generated in API Gateway to validate at Lambda?

I have created self client certificate within API Gateway. I would like my lambda to validate before processing the request from API Gateway (Configure Backend to Authenticate API).
API Gateway allows us to copy the certificate to clipboard. Which we can save as var or file to be read within Nodejs Lambda function, authenticate and proceed further.
Do we have examples?
API Gateway is invoking your AWS Lambda function via the the Lambda Invoke method in the AWS API. Your Lambda function isn't a web server, so it isn't receiving a direct HTTPS request from API Gateway, so it isn't going to receive the HTTPS client certificate.
I would question the need for this anyway. Your API Gateway should be using an IAM role to invoke the Lambda function. That's the mechanism you would use to make sure only API Gateway has access to invoke your Lambda function. The client-certificate is for web servers running behind API Gateway that don't use IAM for authentication.

Multiple AWS API Gateway APIs as trigger to the same Lambda function

I already had an API Gateway API as the trigger for my AWS Lambda function. However when I tried to add another API as a trigger to the same AWS Lambda, it threw an error saying that
There was an error creating the trigger: An integration is already present on this method.
Even when I delete the trigger already present from the configuration window of Lambda, it still shows that the trigger is present.
How can I add multiple API Gateway APIs as triggers for the same lambda function ?
You can setup it via API Gateway console.
Create the Lambda function via Lambda without providing a trigger
Go to API Gateway Console
Create an API.
Create a resource and method
Select the Lambda function you want to trigger by the method
Create an other API/method
Select the Lambda function you want to trigger by the method
Since you are creating the trigger/integration via API Gateway Console, API Gateway will setup the proper permission to allow API Gateway to invoke your Lambda function on multiple APIs/methods.
In the API Gateway, we cannot make entries with the same resource name. When you have created a trigger it's already created and again you are trying to create another one. So we have to clear the previous one and then try again or else we can update it going into the API Gateway interface.