Creating a Lambda Function
Begin by searching for the Lambda service in the AWS Management Console search bar.
- Author from scratch: Write code entirely from scratch.
- Use a blueprint: Choose from AWS-provided blueprints for common tasks (e.g., processing an S3 upload, configuring an API Gateway trigger, etc.).
- Container image: Package your application as a container image if preferred.





The Default Lambda Function Code
When your Lambda function is created, it comes with a default handler. The basic code is as follows:Testing the Lambda Function
You can test your Lambda function directly using the built-in test functionality without needing an external trigger. Click the Test button to configure a test event. Initially, you will be provided with a sample JSON event similar to this:event to your Lambda function. Initially, your function returns a static message. To utilize the event data in your code, update your function as follows:
"Product": "TV"). The log output will display details such as request IDs, duration, memory usage, and more, which are viewable in CloudWatch Logs.
Adding an API Gateway Trigger
To allow external HTTP requests to invoke your function, add an API Gateway trigger:- In the Lambda console, click Add trigger.
- Select API Gateway.
- Choose Create a new API and configure its settings. For the demo, you can leave the security settings open.
- Click Add.


Incorporating Third-Party Libraries
For more advanced functionality, you may need to use third-party libraries. In this example, we’ll integrate the Node.js library bcryptjs to hash a user-supplied password. On your local development machine, install bcryptjs by running:node_modules is created, which contains the bcryptjs code.
Now, modify your Lambda function code to import and utilize bcryptjs:
password property (for example, "password": "123") and run the test. If you encounter an error like:
node_modules folder was not included when deploying your function.
To resolve this issue, ensure that you bundle your
node_modules folder with your Lambda function.Bundling Dependencies with Your Code
To prevent errors like the package not found error, bundle your code along with all required third-party dependencies. One way to do this is to zip your project directory—including your code file and thenode_modules folder—and then upload the zip file directly to AWS Lambda. This approach ensures that AWS Lambda has access to bcryptjs and other dependencies.
After uploading the zip file, deploy your changes and run your test again. Your function should now return a successful response with the hashed password.
Using Lambda Layers
An alternative solution for managing dependencies is to use Lambda layers. Layers allow you to package libraries separately and share them across multiple functions. To create a Lambda layer for bcryptjs:- Prepare the folder structure required by Lambda. For Node.js, create a folder named
Node.js(ornodejsas per the documentation) and move yournode_modulesfolder inside it. - Zip the
Node.jsfolder. For example, name the zip filehash-layer.zip. - In the Lambda console, navigate to Layers and click Create layer.
- Name your layer (e.g., “hash”) and upload the zip file.
- Select the supported runtime (Node.js) and create the layer.
- Open your function in the Lambda console.
- In the Layers section, click Add a layer.
- Choose Custom layers and select your “hash” layer.
- Add the layer and deploy your function.


Monitoring Your Lambda Function
AWS Lambda integrates seamlessly with Amazon CloudWatch to monitor your function’s performance. In the Monitoring tab, check metrics such as invocation count, duration, errors, and throttling. For more detailed information, click View logs in CloudWatch. Example log snippet: