aws lambda function with nodejs project

Jack Strycoff
Jack Strycoff used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

Commented:
Doesn't work. Thanks for your reply.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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?

Author

Commented:
Yes. It is not a coding error.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial