Need an example of calling AWS Lambda from JavaScript

You just need an example of calling AWS Lambda from JavaScript running in a browser, and the result of displaying a function in the JavaScript console. Incredibly, I cannot find examples on Google or from the AWS documentation.

My use case is that I have an HTML form. When the form is submitted, I want to use Lambda to process the form input. Assuming the Lambda function ends without errors, I want the user to launch a thank you page.

Please provide a complete HTML example, not just a code snippet.

+4
source share
3 answers

I see that people used the AWS SDK for Javascript, but this is not specifically required, since you need to create an Amazon Cognito authentication pool with access that is available for unauthorized identifiers (Atleast for beginners like me). Below code works fine for me -

<html> <head> <script> function callAwsLambdaFunction() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("myDiv").innerHTML = this.responseText; } }; xhttp.open("GET", "https://test123.ap-south-1.amazonaws.com/dev", true); xhttp.send(); } </script> <title>Hello World!</title> </head> <body> <h1>Hello world!</h1> <h1>Click below button to call API gatway and display result below!</h1> <h1><div id="myDiv"></div></h1> <button onclick="callAwsLambdaFunction()">Click me!</button><br> Regards,<br/> Aniket </body> </html> 

Above is an example of index.html, which I added to my S3 bucket and made a static site. A couple of points for notes -

  • Make your index.html open from the outside if you are using S3 for static website hosting.
  • Make sure you enable CORS for your API gateway if your website domain does not match the domain of the API gateway. Otherwise, you can get -

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://test123.ap-south-1.amazonaws.com/dev. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Source

+4
source

Since you need to run Lambda from a browser, you have two options that you can achieve.

  • Use the AWS Javascript SDK , configure it using a static configuration, or Cognito using IAM Permissions on Lambda . You can also consider subscribing to Lambda on the SNS Topic and launch Lambda by sending a message to the topic. This SNS approach will also require that you save and retrieve the send state through a separate call.

  • Use the AWS API Gateway to create a RESTful endpoint with the correct CORS configuration that you can execute from a browser using AJAX.

Both options have their pros and cons. Additional information about your use case will be needed in order to properly evaluate which one suits you.

+6
source

I would use the AWS SDK for Javascript, below the steps

  • Link to js file

    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.100.0.min.js"></script>

  • Initialize / Configure SDK

    AWS.config.update({region: 'REGION'}); AWS.config.credentials = new AWS.CognitoIdentityCredentials({IdentityPoolId: 'IdentityPool'});

  • Create a service lambda object, etc.

you can see the next steps in this link

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/browser-invoke-lambda-function-example.html

0
source

Source: https://habr.com/ru/post/1243533/


All Articles