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
source share