UNPKG

11.3 kBMarkdownView Raw
1# node-lambda
2
3![node-lambda](../master/node-lambda.png?raw=true)
4
5Command line tool to locally run and deploy your node.js application to [Amazon Lambda](http://aws.amazon.com/lambda/).
6
7[![BuildStatus](https://travis-ci.org/motdotla/node-lambda.svg?branch=master)](https://travis-ci.org/motdotla/node-lambda)
8[![NPM version](https://badge.fury.io/js/node-lambda.svg)](http://badge.fury.io/js/node-lambda)
9
10```
11$ node-lambda run
12```
13
14## Installation
15
16```
17$ npm install -g node-lambda
18```
19
20## Example App
21
22The [node-lambda-template](https://github.com/RebelMail/node-lambda-template) example app makes it easy to get up and running.
23
24## Usage
25
26There are 4 available commands.
27
28```
29$ node-lambda setup
30$ node-lambda run
31$ node-lambda package
32$ node-lambda deploy
33```
34
35### Commands
36
37#### setup
38
39Initializes 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).
40
41```
42$ node-lambda setup --help
43
44Usage: setup [options]
45
46Sets up the .env file.
47
48
49Options:
50 -h, --help output usage information
51 -j, --eventFile [event.json] Event JSON File
52 -x, --contextFile [context.json] Context JSON File
53```
54
55After running setup, it's a good idea to gitignore the generated `event.json` and `.env` files, as well as `.lambda`.
56
57```
58$ echo -e ".env\ndeploy.env\nevent.json\n.lambda" >> .gitignore
59```
60
61##### Deploy env variables
62
63```
64AWS_ENVIRONMENT // (default: '')
65CONFIG_FILE // (default: '')
66EVENT_SOURCE_FILE // (default: '')
67EXCLUDE_GLOBS // (default: '')
68AWS_ACCESS_KEY_ID // (default: not set!)
69AWS_SECRET_ACCESS_KEY // (default: not set!)
70AWS_PROFILE = // (default: '')
71AWS_SESSION_TOKEN = // (default: '')
72AWS_REGION = // (default: 'us-east-1,us-west-2,eu-west-1')
73AWS_FUNCTION_NAME // (default: package.json.name or 'UnnamedFunction')
74AWS_HANDLER // (default: 'index.handler')
75AWS_ROLE_ARN || AWS_ROLE // (default: 'missing')
76AWS_MEMORY_SIZE // (default: 128)
77AWS_TIMEOUT // (default: 60)
78AWS_RUN_TIMEOUT // (default: 3)
79AWS_DESCRIPTION // (default: package.json.description or '')
80AWS_RUNTIME // (default: 'nodejs6.10')
81AWS_PUBLISH // (default: false)
82AWS_FUNCTION_VERSION // (default: '')
83AWS_VPC_SUBNETS // (default: '')
84AWS_VPC_SECURITY_GROUPS // (default: '')
85AWS_TRACING_CONFIG // (default: '')
86AWS_LOGS_RETENTION_IN_DAYS // (default: '')
87EVENT_FILE // (default: 'event.json')
88PACKAGE_DIRECTORY // (default: not set)
89CONTEXT_FILE // (default: 'context.json')
90PREBUILT_DIRECTORY // (default: '')
91SRC_DIRECTORY // (default: '')
92DEPLOY_TIMEOUT // (default: '120000')
93DOCKER_IMAGE // (default: '')
94DEPLOY_ZIPFILE // (default: '')
95AWS_DLQ_TARGET_ARN // (default: not set)
96```
97
98#### run
99
100Runs your Amazon Lambda index.js file locally. Passes `event.json` data to the Amazon Lambda event object.
101
102```
103$ node-lambda run --help
104
105Usage: run|execute [options]
106
107Run your Amazon Lambda application locally
108
109
110Options:
111 -h, --help output usage information
112 -H, --handler [index.handler] Lambda Handler {index.handler}
113 -j, --eventFile [event.json] Event JSON File
114 -u, --runtime [nodejs6.10] Lambda Runtime
115 -t, --timeout [3] Lambda Timeout
116 -f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")
117 -x, --contextFile [context.json] Context JSON File
118 -M, --enableRunMultipleEvents [true] Enable run multiple events
119```
120
121#### package
122
123Bundles your application into a local zip file.
124
125```
126$ node-lambda package --help
127
128Usage: package|zip [options]
129
130Create zipped package for Amazon Lambda deployment
131
132
133Options:
134 -h, --help output usage information
135 -A, --packageDirectory [build] Local Package Directory
136 -I, --dockerImage [] Docker image for npm install
137 -n, --functionName [node-lambda] Lambda FunctionName
138 -H, --handler [index.handler] Lambda Handler {index.handler}
139 -e, --environment [development] Choose environment {dev, staging, production}
140 -x, --excludeGlobs [event.json] Space-separated glob pattern(s) for additional exclude files (e.g. "event.json dotenv.sample")
141 -D, --prebuiltDirectory [] Prebuilt directory
142```
143
144#### deploy
145
146Bundles and deploys your application up to Amazon Lambda.
147
148```
149$ node-lambda deploy --help
150
151Usage: deploy [options]
152
153Deploy your application to Amazon Lambda
154
155
156Options:
157 -h, --help output usage information
158 -e, --environment [development] Choose environment {dev, staging, production}
159 -a, --accessKey [your_key] AWS Access Key
160 -s, --secretKey [your_secret] AWS Secret Key
161 -P, --profile [] AWS Profile
162 -k, --sessionToken [] AWS Session Token
163 -r, --region [us-east-1] AWS Region
164 -n, --functionName [node-lambda] Lambda FunctionName
165 -H, --handler [index.handler] Lambda Handler {index.handler}
166 -o, --role [your_amazon_role] Amazon Role ARN
167 -m, --memorySize [128] Lambda Memory Size
168 -t, --timeout [3] Lambda Timeout
169 -d, --description [missing] Lambda Description
170 -u, --runtime [nodejs6.10] Lambda Runtime
171 -p, --publish [false] Lambda Publish
172 -L, --lambdaVersion [] Lambda Function Version
173 -b, --vpcSubnets [] Lambda Function VPC Subnets
174 -g, --vpcSecurityGroups [] Lambda VPC Security Group
175 -K, --kmsKeyArn [] Lambda KMS Key ARN
176 -Q, --deadLetterConfigTargetArn [] Lambda DLQ resource
177 -T, --tracingConfig [] Lambda tracing settings
178 -R, --retentionInDays [] CloudWatchLogs retentionInDays settings
179 -A, --packageDirectory [build] Local Package Directory
180 -G, --sourceDirectory [] Path to lambda source Directory (e.g. "./some-lambda")
181 -I, --dockerImage [] Docker image for npm install
182 -f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")
183 -S, --eventSourceFile [] Path to file holding event source mapping variables (e.g. "event_sources.json")
184 -x, --excludeGlobs [event.json] Space-separated glob pattern(s) for additional exclude files (e.g. "event.json dotenv.sample")
185 -D, --prebuiltDirectory [] Prebuilt directory
186 -T, --deployTimeout [120000] Deploy Timeout
187 -z, --deployZipfile [] Deploy zipfile
188 -y, --proxy [] Proxy server
189```
190
191## Custom Environment Variables
192
193AWS 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
194
195## Node.js Runtime Configuration
196
197AWS Lambda now supports Node.js 6.10 and Node.js 4.3. Please also check the [Programming Model (Node.js)](http://docs.aws.amazon.com/lambda/latest/dg/programming-model.html) page.
198
199## Post install script
200When 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.
201
202Example `post_install.sh`:
203```
204printf "\n\n###### Post install script ###### \n"
205ENV="production";
206if [ ! -z $1 ]
207 then
208 ENV=$1;
209fi
210cp -v "config_$ENV.js" "config.js" \
211&& printf "###### DONE! ###### \n\n"
212```
213
214## Post invoke script (example)
215If you wish to invoke your deployed AWS Lambda function, you can add the following as a `script` to your `package.json`:
216
217```
218"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"
219```
220
221## Prebuilt packages
222The `--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.
223
224## Handling `npm link` and Dependencies With Local Paths
225Perhaps 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.
226
227## ScheduleEvents
228#### Optional Parameter
229When 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.
230
231#### Note on ScheduleState for ScheduleEvents
232When 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.
233
234#### Known Issue
235###### Duplicate ScheduleEvent Triggers
236If 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.
237
238## Other AWS Lambda Tools Projects
239
240+ [lambdaws](https://github.com/mentum/lambdaws)
241+ [lambdaws-instant-api](https://github.com/mentum/lambdaws-instant-api)
242
243## Contributing
244
2451. Fork it
2462. Create your feature branch (`git checkout -b my-new-feature`)
2473. Commit your changes (`git commit -am 'Added some feature'`)
2484. Push to the branch (`git push origin my-new-feature`)
2495. Create new Pull Request
250
251### Coding style
252This project uses [JavaScript Standard Style](https://standardjs.com/).
253
254[![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
255
256## Running tests
257
258```
259$ npm install
260$ npm test
261```