Reputation: 450
I have an aws lambda function which runs some python code (which is calling an external api with some extra functions). I attached a trigger, an api gateway, which if I go to the url, the lambda functions runs correctly.
However, I want the lambda function only to be run if they click on a button somewhere on the 'website' of the api-url. Rephrased otherwise, I want the api-url page to have a button, which on click executes the lambda function. Im think that should be quite easy, however I can't figure out how. Due to the information overload I can't seem to find the right video, document example on how to do it.
I tried to add a button on the api-url page by, on 'method-execution' on the 'api-gateway resource' page, changing the mapping template to text/html instead of json. (a bit like in https://blog.it-playground.eu/display-html-page-using-only-api-gateway/) But then i can't figure out how to run the lambda function onClick of the button. ==> Is this the right start?
(Because its not really a coding issue, I can't really provide any code).
Also, is this question so simply that it shows I simply don't understand the basics enough (and should subsequently go over them again)?
Upvotes: 0
Views: 2196
Reputation: 14502
Of course this is possible. API Gateway exposes REST API. All you need to do is to create some resource and method in API Gateway such as
GET /posts
attach your lambda function to it, and hit that API endpoint with some ajax request from your front end (via fetch
, axios
, ...) that would be executed when the button is clicked. Something like:
button.addEventListener("click", () => {
fetch("https://my-api-gateway-url/posts").then() ...
}
You can process the response data in .then
part but don't forget that this is asynchronous coding so you need to handle it as such.
Upvotes: 1