简体   繁体   中英

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.

PS- 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM