A sample for creating AWS Lambda NodeJS handlers that require external dependencies.
This project uses returns the value of the environment variable HELLO
. The value
is defined in the .env
file. The file is read using the dotenv
NPM module.
The value is returned via the Lambda context object.
This section helps you just get this project into AWS Lambda and run it.
These steps assume you've already cloned the project locally.
-
Init the project
npm install
-
Copy sample.env to .env
cp sample.env .env
-
Build the package. This basically involves packaging the relevant files into a
package.zip
file.You can use the provided npm script:
npm start package
Or run this zip command in a terminal window:
zip -r -q package.zip index.js .env node_modules/*
-
Connect to AWS Lambda
-
Create a new Lambda function If you don't have existing Lamdba functions defined, click Get Start New on the AWS Lambda launch screen. If you do have an existing Lamdba function, click ** on the ....
- The first step is to select a blueprint.
- Search for "node".
- Select the "hello-world" start AWS Lambda function project.
-
Configure the function
- Provide a Name for the function. For example "myHandler".
- Under the Lambda function code section, selec the "Upload a .ZIP file" option.
- Click the Upload button and upload the
package.zip
file create above. - Leave the Handler as index.handler. This tells Lambda to run the
exports.handler
function defined in theindex.js
file. - Under Role, select 'lambda_basic_execution'.
- Leave the other settings as default values.
- Click Next.
-
On the Review screen, validate the settings then click Create Function You should then get to the myHandler function page. You should also see the message "Congratulations! Your Lambda function "myHandler" has been successfully created."
-
To test the function, click Test This will open the "Input test event" dialog box. In the future, you can provide any kind of data you want in here to test yoru function. This example does not use any of these values, so you can ignore them and just click Save and test
-
Review results After a few seconds, the function should run. The output appears below the tabs. Look for "Execution result: succeeded (Logs)". In this section, you should see the text "Hello, World!"
The app simply returns an environment variable that is defined in the file .env
which is read by the npm module, dotenv.
HELLO=Hello, World!
The lamdba function is found in index.js
.
const
dotenv = require('dotenv').load();
exports.handler = function (event, context) {
context.succeed(process.env.HELLO);
}
When configuring the AWS Lambda function, you told it that the lambda function was "index.handler".
index
matched to the file index.js
handler
matched to the exports.handler
in index.js
.