DEV Community

Cover image for Creating an AWS Lambda Function Using Serverless Framework
aryan015
aryan015

Posted on

Creating an AWS Lambda Function Using Serverless Framework

πŸ”₯ What is AWS Lambda?

AWS Lambda is a serverless compute service that lets you run code without managing servers. You simply upload your code, and Lambda automatically handles the infrastructure, scaling, and execution.


πŸ› οΈ Benefits of Using Serverless Framework

The Serverless Framework is a powerful open-source tool that simplifies the process of creating and managing AWS Lambda functions.

βœ… Key Advantages

  • Simplifies Deployment: Easily deploy Lambda functions with simple commands.
  • Infrastructure as Code: Use the serverless.yml file to define infrastructure and configuration.
  • Supports Multiple Providers: AWS, Azure, Google Cloud, etc.
  • Built-in Support: Manages API Gateway, DynamoDB, S3, and other AWS services.

πŸ”₯ 1. Install Serverless Framework

First, install the Serverless CLI globally using npm:

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

serverless --version
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ 2. Create a New Serverless Project

Generate a new Serverless project with AWS as the provider:

serverless create --template aws-nodejs --path my-lambda-function
Enter fullscreen mode Exit fullscreen mode
  • aws-nodejs β†’ AWS Lambda with Node.js runtime.
  • my-lambda-function β†’ Your project folder name.

Go into the project directory:

cd my-lambda-function
Enter fullscreen mode Exit fullscreen mode

πŸ”§ 3. Install Dependencies

Install the necessary dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

🌐 4. Configure AWS Credentials

If you haven’t already, configure your AWS credentials:

serverless config credentials --provider aws --key <AWS_ACCESS_KEY> --secret <AWS_SECRET_KEY>
Enter fullscreen mode Exit fullscreen mode

βœ… Get your access key and secret key from AWS IAM.


πŸ› οΈ 5. Edit serverless.yml Configuration

Open the serverless.yml file and define your Lambda function:

service: my-lambda-service

provider:
  name: aws
  runtime: nodejs18.x
  region: us-east-1

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation:

  • service: Name of your Lambda service.
  • provider: AWS settings, including the region and runtime.
  • functions: The Lambda function definition.
  • events: Triggering the Lambda through an HTTP request.

πŸ”₯ 6. Write the Lambda Function

Open the handler.js file and add the following code:

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: "Hello from Lambda!",
      input: event,
    }),
  };
};
Enter fullscreen mode Exit fullscreen mode

βœ… This function returns a simple JSON response.


πŸš€ 7. Deploy the Lambda Function

Deploy your Lambda function to AWS:

serverless deploy
Enter fullscreen mode Exit fullscreen mode

Once deployed, you’ll see the API Gateway endpoint:

Service deployed to: https://<your-api-id>.execute-api.<region>.amazonaws.com/dev/hello
Enter fullscreen mode Exit fullscreen mode

🌐 8. Test the Lambda Function

You can test the function using:

curl https://<your-api-id>.execute-api.<region>.amazonaws.com/dev/hello
Enter fullscreen mode Exit fullscreen mode

Or:

serverless invoke -f hello
Enter fullscreen mode Exit fullscreen mode

βš™οΈ 9. Cleaning Up

To remove the Lambda function and free up resources:

serverless remove
Enter fullscreen mode Exit fullscreen mode

βœ… Key Takeaways

  1. Serverless Framework simplifies AWS Lambda deployment.
  2. Automatic Scaling: No need to manage infrastructure.
  3. Cost-Efficient: You only pay for what you use.
  4. Quick Deployment: Easily create and deploy Lambda functions with minimal configuration.

πŸš€ Next Steps

  • Add more Lambda functions.
  • Integrate with AWS services like S3, DynamoDB, and API Gateway.
  • Optimize and secure your serverless functions.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video πŸ“ΉοΈ

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay