Command line tool to locally run and deploy your node.js application to Amazon Lambda
Command line tool to locally run and deploy your node.js application to Amazon Lambda
If you found this useful, consider also checking out dotenvx. Thank you!
Command line tool to locally run and deploy your node.js application to Amazon Lambda.
$ node-lambda runWith npx:
$ npx node-lambda@latest [COMMAND]Globally installed:
$ npm install -g node-lambdaExample apps make it easy to get up and running
There are 4 available commands.
$ npx node-lambda@latest setup
$ npx node-lambda@latest run
$ npx node-lambda@latest package
$ npx node-lambda@latest deployInitializes the event.json, context.json, .env, deploy.env files, and event_sources.json files. event.json is where you mock your event. context.json is where you can add additional mock data to the context passed to your lambda function. .env is where you place your deployment configuration. deploy.env has the same format as .env, but is used for holding any environment/config variables that you need to be deployed with your code to Lambda but you don't want in version control (e.g. DB connection info). event_sources.json is used to set the event source of the Lambda function (Not all event sources available in Lambda are supported).
$ npx node-lambda@latest setup --help
Usage: setup [options]
Sets up the .env file.
Options:
-h, --help output usage information
-j, --eventFile [event.json] Event JSON File
-x, --contextFile [context.json] Context JSON FileAfter running setup, it's a good idea to gitignore the generated event.json and .env files, as well as .lambda.
$ echo -e ".env\ndeploy.env\nevent.json\n.lambda" >> .gitignore…Runs your Amazon Lambda index.js file locally. Passes event.json data to the Amazon Lambda event object.
…Bundles your application into a local zip file.
…Bundles and deploys your application up to Amazon Lambda.
…If you are deploying to a custom endpoint you may also need to pass in an access key/secret. For localstack these can be anything, but cannot be blank:
npx node-lambda@latest deploy --endpoint http://localhost:4574 --accessKey '1234' --secretKey '1234'AWS Lambda will let you set environment variables for your function. Use the sample deploy.env file in combination with the --configFile flag to set values which will be added to the lambda configuration upon deploy. Environment variables will also be set when running locally using the same flag
AWS Lambda now supports Node.js 24, Node.js 22 and Node.js 20. Please also check the Lambda runtimes page.
Use the command line argument --deployUseS3 or -B. (This option is true/false.)
Example: % npx node-lambda@latest deploy -B
You can also set the environment variable DEPLOY_USE_S3.
Example: DEPLOY_USE_S3=true
Use the environment variable to set the bucket name or S3 key prefix.
The environment variable name is different for each region. Please set it to the environment variable you want to deploy.
It can be set in .env.
Example:
# S3_<region>_BUCKET
S3_US_WEST_1_BUCKET=test_aaa
# S3_<region>_PREFIX
S3_US_WEST_1_PREFIX=bbbIn this case, the S3 key:
test_aaa/bbb/deploy-package-${FunctionName}.zip
IMAGE_URI or --imageUri option, and deploy.When running node-lambda deploy if you need to do some action after npm install --production and before deploying to AWS Lambda (e.g. replace some modules with precompiled ones or download some libraries, replace some config file depending on environment) you can create post_install.sh script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Environment string is passed to script as first parameter so you can use it if needed. Make sure that the script is executable.
Example post_install.sh:
printf "\n\n###### Post install script ###### \n"
ENV="production";
if [ ! -z $1 ]
then
ENV=$1;
fi
cp -v "config_$ENV.js" "config.js" \
&& printf "###### DONE! ###### \n\n"If you wish to invoke your deployed AWS Lambda function, you can add the following as a script to your package.json:
"invoke:remote": "aws lambda invoke --function-name myLambdaFnName --payload fileb://fixtures/hi.json invoked.json --log-type Tail | jq -r '.LogResult' | base64 --decode && rm invoked.json"The --prebuiltDirectory flag is useful for working with Webpack for example. It skips npm install --production and post_install.sh and simply packages the specified directory.
npm link and Dependencies With Local PathsPerhaps the easiest way to handle these cases is to bundle the code using Webpack and use the --prebuiltDirectory flag to package the output for deployment.
When using the eventSourceFile flag (-S or --eventSourceFile) to set a ScheduleEvent trigger, you can pass an optional ScheduleDescription key into the ScheduleEvent object with a custom description for the CloudWatch event rule you are defining. By default, node-lambda generates a ScheduleDescription for you based on the ScheduleName and ScheduleExpression of the rule.
When setting ScheduleState to ENABLED or DISABLED for ScheduleEvents, it is useful to note that this sets the state of the CloudWatch Event rule but DOES NOT set the state of the trigger for the Lambda function you are deploying; ScheduleEvent triggers are enabled by default in the Lambda console when added using the eventSourceFile flag.
If you are adding a trigger via the eventSourceFile for the first time, remove preexisting triggers from the Lambda console before deploying. Deploying a Lambda with the --eventSourceFile flag will NOT overwrite the same triggers created from the AWS console and may result in a duplicate triggers for the same rule.
git checkout -b my-new-feature)git commit -am 'Added some feature')git push origin my-new-feature)This project uses JavaScript Standard Style.
$ npm install
$ npm testNo open issues yet, or sync has not completed.