0

Below is my Input form from which, I want to send 2 inputted numbers as POST to AWS Lambda and want them to be added and I want the response of the added numbers to be displayed on the browser of the client. Let us keep in note, that we are using AWS CodeStar, so the Lambda part cannot be disturbed by us.

Screenshot of our form

For this, I have written a code that goes:

app.js

        var express = require('express');
        var app = express();
        const bodyParser = require('body-parser');
        app.use(bodyParser.urlencoded({extended:true}));

        app.get("/", function(req, res) {
          res.sendFile(__dirname+ "/index.html");
        });

        app.post("/", function(req, res) {

        var a = Number(req.body.num1);
        var b = Number(req.body.num2);
        var c = a+b;
        res.send("The sum is" + c);

        });

        // Export your Express configuration so that it can be consumed by the Lambda handler
        module.exports = app

The Error which we are getting is : {"message":"Forbidden"}

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
  </head>
  <body>
    <form  action="/" method="post">
      <input type="text" name="num1" placeholder="Enter No. 1">
      <input type="text" name="num2" placeholder="Enter No. 2">
      <button type="submit" name="submit">CALCULATE</button>

    </form>
  </body>
</html>

index.js

'use strict';

const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
hrishikakkad
  • 43
  • 1
  • 9

1 Answers1

0

There are two ways to reach your compute application,
1. Using AWS SDK to invoke the Lambda Function
2. Making an HTTP call to an HTTP URL or an AWS API Gateway endpoint(it's normally this one)

You cannot call an AWS Lambda using it's URL, because it's a compute service and is usually complemented by API Gateway

1. How to invoke AWS Lambda using SDK -

let AWS = require('aws-sdk');
let util = require('util');

let test = async () => {

    try {
        let client = new AWS.Lambda({
            region: "us-east-1"
        });

        let payload = {
            "some_key" : "some_value"
        };

        payload = JSON.stringify(payload);
        console.log("Payload => ", payload);

        let params = {
            FunctionName: 'some_lambda_function_name',
            InvocationType: "RequestResponse", 
            Payload: payload
        };

        let clientInvoke = util.promisify(client.invoke).bind(client);
        let invokeResponse  = await clientInvoke(params);

        console.log('invokeResponse => ',invokeResponse);
    } catch (error) {
        console.log('error =>\n', error);
    }

};

test();

2. How to make an HTTP call to API gateway using axios library-

var axios = require('axios');

var headers = {
    "Content-Type": "application/json"
}

var data = {
    "some_key" : "some_value"
}

var url = "some_url_here";

axios({ method: 'POST', url: url, headers: headers, data: data })
.then((response) => {
    console.log(response.data);
})
.catch((error) => {
    console.log(error);
})
Dev1ce
  • 5,390
  • 17
  • 90
  • 150