Min Yoongi
Min Yoongi

Reputation: 466

Can we run an AWS lambda locally without deployment?

I'm quite new to AWS and not able to understand one thing. I've installed aws cli, and now I'll start using AWS to code. But all the tutorials online shows creating instances, some deployments etc. Is there any way I can run it locally? Because it's an enterprise network and I don't want to cause any unreasonable payment charges.

P.S.- is there anything similar how we work with node application? I build app using vscode, run locally and test. Once all good, i Deploy in production.

Upvotes: 7

Views: 18491

Answers (2)

Gustavo Tavares
Gustavo Tavares

Reputation: 2805

You don't need Serverless framework or SAM to run it locally.

The function is a normal nodeJs code.
For the Lambda Function, you normally export the function handler, like:

import { Callback, Context } from 'aws-lambda';
export function handler(event: any, context: Context, callback: Callback): void {}

You can just import this file inside another file or test case and run it passing the event, the context, and the callback as parameters.

For the Context and Callback, you can use the @types/aws-lambda to see their definition.
To the event, you'll need to craft a JSON object in the format that the Lambda Function will accept.
In @types/aws-lambda you will find all possible event types used by the AWS platform.

Upvotes: 13

Caldazar
Caldazar

Reputation: 3757

You can use Serverless framework or AWS SAM (serverless application model) to create your Lambda functions. In that case, you have the option to run your Lambda function locally.

AWS SAM is an official AWS framework for the creation of serverless services (Lambda, DynamoDB, etc) and Serverless Framework is 3rd party solution, although pretty popular.

There are probably some other solutions also that can help you run a Lambda function locally, but since you're talking about enterprise, it would be expected that you use one of these 2 solutions to create your serverless infrastructure.

Upvotes: 4

Related Questions