# Why Developers Are Avoiding AWS Lambda? (And How to Master It!)

AWS Lambda has revolutionised the way developers approach server-less computing, offering scalable and cost-effective solutions for event-driven applications. However, despite its advantages, many developers hesitate to adopt Lambda for various reasons.

In this blog, I’ll try to cover some common challenges developers face with AWS Lambda and provide a step-by-step guide to deploying a simple Node function that calculates Fibonacci numbers, demonstrating how to overcome these hurdles.

## Developers Aren’t Embracing AWS Lambda?

While AWS Lambda offers numerous benefits, several challenges deter developers from fully embracing it. I tried finding some challenges and opinions on Stack Overflow and other websites about the main hassles of using it.

1. **Complexity in Configuration and Deployment**: Setting up and deploying Lambda functions, especially with multiple integrations, can become complex and time-consuming. Here, Ben jotted down the [five AWS Lambda pitfalls most developers don’t know about](https://torvo.com.au/articles/5-aws-lambda-pitfalls-most-developers-dont-know-about).
    
2. **Complexity in Testing and Debugging**: Simulating the Lambda environment locally for testing and debugging can be challenging, leading to potential issues during deployment.
    
3. People have debated using this instead of a web framework for various use cases where execution time is minimal, such as an image optimiser or a basic interceptor. I recommend checking out this sub-reddit — "[Why or why not use AWS Lambda instead of a web framework for your REST APIs?](https://www.reddit.com/r/Python/comments/1092py3/why_or_why_not_use_aws_lambda_instead_of_a_web/)" — to get a bird's-eye view of what AWS Lambda can and cannot achieve.
    
4. **Resource Limitations**: There are constraints on memory, storage, and concurrent executions, which can pose challenges for resource-intensive applications.
    

Besides all of this, I know from experience that getting started with AWS Lambda for a specific use case can be challenging, especially without a personalised doc or setup guide. (Don’t worry, I’ve got you covered!)

**Ps:** I would love to hear from you! What challenges have you faced when considering or implementing AWS Lambda in your projects? Share your experiences and insights in the comments below.

## Making AWS Lambda Easy!

To demystify the process, let's walk through deploying a simple Node function on AWS Lambda that calculates Fibonacci numbers. I’ll try to cover all the technicalities related to Lambda here (hope so).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739365810405/d97a710c-53ae-46ee-9eb1-733dc75feb1d.png align="center")

### Step 1: Set Up Your Environment

Before getting started, ensure that you have an **active AWS account**, as it is essential for accessing and utilising AWS services. If you don’t have one, you can create an account on the [AWS website](https://aws.amazon.com/) and follow the necessary setup steps. Additionally, make sure that Node is installed on your local machine, as it is required for running JavaScript-based applications. You can verify your Node installation by running `node -v` in your terminal.

### Step 2: Write the Fibonacci Function

Now, we will use the [**fibonacci**](https://www.npmjs.com/package/fibonacci) `npm package` instead of writing our own recursive function. This will also demonstrate *how to package third-party dependencies for AWS Lambda*.

#### **1\. Initialise a Node Project**

Navigate to your project directory and initialise a new Node project:

```bash
>>> mkdir lambda-fibonacci
>>> cd lambda-fibonacci
>>> npm init -y
```

#### **2\. Install the** `fibonacci` Package

Run the following command to install the `fibonacci` package:

```bash
npm install fibonacci
```

#### **3\. Create the Lambda Function**

Now, create an `index.mjs` file and use the `fibonacci` package:

```javascript
// index.mjs file.
const fibonacci = require("fibonacci");

exports.handler = async (event) => {
    // Parse the input number from the event object (assumed to be passed in the request).
    const number = parseInt(event.number);
    
    // Validate the input: Ensure it is a non-negative integer.
    if (isNaN(number) || number < 0) {
        return {
            statusCode: 400, // HTTP status code for bad request.
            body: JSON.stringify({ error: 'Please provide a valid non-negative integer.' }),
        };
    }

    // Use the "fibonacci" npm package to compute the Fibonacci number at the given position.
    // `iterate(number).number` returns the Fibonacci number at the specified position.
    // Visit: https://www.npmjs.com/package/fibonacci
    const result = fibonacci.iterate(number).number;

    return {
        statusCode: 200,
        body: JSON.stringify({ number, result }),
    };
};
```

This function leverages the `fibonacci` package to efficiently compute Fibonacci numbers.

### Step 3: Package and Upload Your Lambda Function

Since we are using an external `npm package`, we need to bundle all dependencies before uploading the function to AWS Lambda.

#### **1\. Zip the Required Files**

To include all dependencies, make sure you zip your `index.mjs` file along with the `node_modules` directory:

```bash
zip -r function.zip index.mjs node_modules package.json package-lock.json
```

#### **2\. Create a Lambda Function on AWS Console**

* **Log in to AWS Lambda** and go to the AWS Lambda console. Click on **Create a function**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739520872218/0ef35ddb-7023-4037-aac8-759f7d2d0087.png align="center")

* You will land on a page where you can see various options.
    
    * **You have three ways to create a function:**
        
        * **Author from scratch**: Start with an empty Lambda function and manually configure it.
            
        * **Use a blueprint**: Select a pre-configured template with sample code and AWS settings.
            
        * **Container image**: Deploy your Lambda function using a Docker container.
            
        
        For most cases, **choosing "Author from scratch"** is best, especially if you're building a custom function.
        
    * **Basic Information Section:**
        
        * **Function name**
            
            * Choose a unique name for your function.
                
            * It must be 1 to 64 characters and can only contain **letters, numbers, hyphens (-), and underscores (\_).**
                
        * **Runtime**
            
            * This specifies the language environment for your function.
                
            * Options include **Node,** **Python, Java, Go, Ruby, and more.**
                
            * Choose the appropriate runtime based on your function’s programming language.
                
            * For now, we will go with `Node.js 22.x`
                
        * **Architecture**
            
            * **x86\_64** (Default): Best for general-purpose computing.
                
            * **arm64**: More cost-efficient and energy-saving but may not support all libraries.
                
* For now, you can ignore the other configurations and simply click on **Create Function** at the bottom.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739521863406/7fc0be1b-3c38-47fc-813e-7d62ba5a89c9.png align="center")

#### **3\. Upload the Lambda function.**

After AWS creates the function, you will be redirected to the **function dashboard**. Here, you will see:

* Function name
    
* ARN (Amazon Resource Name)
    
* Runtime
    
* Memory and timeout settings
    
* Triggers and permissions
    
* A **code editor** to edit / upload the code.
    

Since we are using an external `npm package` (`fibonacci`), we need to **package our function** and upload it manually.

1. Go to the **Code** tab in the Lambda function dashboard.
    
2. Click **Upload from → .zip file**.
    
3. Select your `function.zip` file.
    
4. Click **Save and Deploy**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739522672006/a5f6e378-3610-47ea-940d-200698776fca.png align="center")

### Step 4: Test the Function

1. Click **"Test"** at the top of the Lambda editor.
    
2. **Create a new test event**:
    
    1. Set the test event name to **FibonacciTest**.
        
    2. Add the following JSON payload:
        
        ```json
        {
            "number": "10"
        }
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739523665869/75f13dab-5868-4c2e-a8ee-0237eb2607dc.png align="center")
        
    3. **Run the test** and check the response.
        
        ```json
        {
            "number": 10,
            "result": 55
        }
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739523760878/240c8549-448d-4c03-9507-54339dc6c865.png align="center")
        

Hooray! You have successfully created an AWS Lambda function with third-party dependencies, bundled it, uploaded it, and tested it!

## More on the code and context.

In our `index.js` file, the function begins with:

```javascript
exports.handler = async (event) => {
```

This is the **entry point** for AWS Lambda. When the function is invoked, AWS passes an `event` object to this handler, containing all the necessary data for processing.

Let's dive deeper into the `event` object and other parameters that AWS Lambda provides.

### **1\. The** `event` Object

The `event` parameter contains **input data** passed to the Lambda function. The structure of this object **depends on the trigger** (API Gateway, S3, DynamoDB, etc.).

#### **API Gateway Trigger (for an HTTP request)**

If your Lambda function is triggered via API Gateway, the `event` object looks like this:

*(Don’t worry, there will be a separate blog where I will help you configure this Lambda with AWS API Gateway.)*

```json
{
    "resource": "/fibonacci",
    "path": "/fibonacci",
    "httpMethod": "POST",
    "headers": { "Content-Type": "application/json" },
    "queryStringParameters": null,
    "body": "{\"number\": \"10\"}",
    "isBase64Encoded": false
}
```

* `httpMethod`: Specifies the request type (`GET`, `POST`, etc.).
    
* `headers`: Contains metadata like authentication tokens or content type.
    
* `body`: Holds the actual data sent to the function (e.g., `{"number": "10"}`).
    
* `path` & `resource`: Indicate which endpoint was accessed.
    

### **2\. The Context Parameter**

Besides `event`, AWS Lambda provides a `context` object, which includes metadata about the execution environment.

To use it, modify the handler function like this:

```javascript
exports.handler = async (event, context) => {
    console.log(context);
};
```

Key properties of `context` include:

* `functionName`: The name of the Lambda function.
    
* `functionVersion`: The deployed version of the function.
    
* `memoryLimitInMB`: The memory allocated to the function.
    
* `awsRequestId`: A unique identifier for the function invocation.
    
* `logGroupName` & `logStreamName`: Where the logs are stored in AWS CloudWatch.
    
* `getRemainingTimeInMillis()`: The remaining execution time before timeout.
    

Feel free to use their [official guide](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) if you want.

## In simple words… why AWS Lambda?

AWS Lambda is Amazon's server-less compute service that allows developers to run code without managing servers or containers. It automatically scales based on the workload, making it suitable for various applications such as data pipelines, responding to web requests, and sending emails. Developers can run Lambda functions locally during development and only pay for the compute time used, which can lead to significant cost savings compared to running virtual machines or containers. This flexibility and efficiency make AWS Lambda a versatile tool for executing code in the AWS cloud.

Visit: [https://docs.aws.amazon.com/lambda/latest/dg/welcome.html#features](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html#features)

## Up Next!

Don't worry, this is not the end! I am in the process of integrating this AWS Lambda with API Gateway to create a simple server-less API. AWS offers a wide range of services, but the best way to truly understand them is by using them. So, dive in, get hands-on experience, and you'll become an AWS expert in no time!

**Mehta :)**
