Link to home
Start Free TrialLog in
Avatar of Jack Strycoff
Jack Strycoff

asked on

aws lambda function with nodejs project

I have a Node project written in TypeScript where the application takes in user's input arguments and does some calculation and prints out the results. Take the example of a calculator the user inputs value of two numbers and it prints out the addition of two numbers. for example npm run calc 3 5. The application prints out 8.

The application is working but i want to make this an aws lambda function and deploy it in aws. The examples i see everywhere is hello lambda function. Can anyone help me how i can make a handler and deploy it to aws?

How do i convert the below hello function to a calc lambda function and deploy it to aws?
export const hello: Handler = (event: APIGatewayEvent, context: Context, cb:             
Callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless Webpack (Typescript) v1.0! Your function executed 
successfully!',
  input: event,
}),
};

cb(null, response);
}

Open in new window


This is my addition class

export class Calc {
public static add(){

    console.log( this.addition( (process.argv[0] + process.argv[1] ));

}
public static addition(num1:number, num2:number)
{
    return num1 + num2;
}

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I have not done this but I would imagine it is something like this
import {Handler, Context, Callback } from 'aws-lambda';
import { Calc } from '../wherever/calc/class/is';

const hello: Handler = (event: any, context: Context, cb: Callback) => {
   // Update event.num1 and event.num2 to match your incoming values
   let answer = Calc.addition(event.num1, event.num2);
   cb(null, answer);
}
// finally export your function
export { hello }

Open in new window

Avatar of Jack Strycoff
Jack Strycoff

ASKER

Doesn't work. Thanks for your reply.
It wasn't posted as a solution - just a guideline. I have no way of testing it.

On EE the standard practice when something does not work is to supply more information. What you implemented, what errors you received (if any).

Your last response does not allow us to take this forward.
When I invoke the handler , it doesn't recognize the Calc class and it gives the compiler errors. One of them is ";" is missing even when the syntax is correct.
If it is reporting errors then there are errors - you can't argue with the compiler - you will lose.

Are you importing the class correctly - is the path correct?
Yes. It is not a coding error.
It is not a coding error
Then what sort of error is it - if it is not in code how do you expect to fix it?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.