Since you need to run Lambda from the browser, you have two options you can achieve it.
Use
AWS Javascript SDK, set it up with user via static configuration orCognitowithIAM Permissionsto yourLambda. You can also consider subscribing yourLambdafunctions toSNS Topicand run theLambdaby sending a message to the topic. This SNS approach would also require you to store and retrieve the submission state via separate call.Use
AWS API Gatewayto create RESTful endpoint with proper CORS configuration that you can ping from the browser using AJAX.
Both options have their pros and cons. More information about your use-case would be necessary to properly evaluate which one suits you best.
Answer from adamkonrad on Stack OverflowNeed example of calling AWS Lambda from JavaScript - Stack Overflow
Why would I use Node.js in Lambda? Node main feature is handling concurrent many requests. If each request to lambda will spawn a new Node instance, whats the point?
Finding an AWS JavaScript Lambda module regardless of whether the lambda is hot or cold
../src/common
Remember that these are Unix file paths.
Also, if possible, minify your JS/TS before deployment.
More on reddit.comInvoking a lambda function from the Javascript SDK vs API Gateway?
I think API gateway, you can set it up to function exactly like a normal back end service. Define an endpoint signature, handle authentication, and use any http client you want.
More on reddit.comVideos
Since you need to run Lambda from the browser, you have two options you can achieve it.
Use
AWS Javascript SDK, set it up with user via static configuration orCognitowithIAM Permissionsto yourLambda. You can also consider subscribing yourLambdafunctions toSNS Topicand run theLambdaby sending a message to the topic. This SNS approach would also require you to store and retrieve the submission state via separate call.Use
AWS API Gatewayto create RESTful endpoint with proper CORS configuration that you can ping from the browser using AJAX.
Both options have their pros and cons. More information about your use-case would be necessary to properly evaluate which one suits you best.
I see people have used AWS SDK for Javascript but it is not required specially since you need to create Amazon Cognito identity pool with access enabled for unauthenticated identities (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 sample index.html that I have added to my S3 bucket and made a static site. Couple of points to note -
- Make your index.html open from outside if you are using S3 for static site hosting.
- Make sure you turn on CORS for your API gateway if your website domain is not same as API gateway domain. Else you might 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).
Maybe I'm missing something here, from an architectural point of view, I can't wrap my head on using node inside a lambda. Let's say I receive 3 requests, a single node instance would be able to handle this with ease, but if I use lambda, 3 lambdas with Node inside would be spawned, each would be idle while waiting for the callback.
Edit: Many very good answers. I will for sure discuss this with the team next week. Very happy with this community. Thanks and please keep them coming!