Writing your first AWS Serverless API

Home Writing your first AWS Serverless API

Writing your first AWS Serverless API

We will create a straightforward serverless API that responds with “Hello, Serverless!”. This involves writing a basic AWS Lambda function and setting up AWS API Gateway to trigger the Lambda function when a request is made to a specific URL in a web browser.

Key Concepts

What is Serverless Computing?

Serverless computing is a cloud-computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. In a serverless setup, developers write code in the form of functions, and the cloud provider runs these functions in response to events.

What is AWS Lambda?

It lets you run code without provisioning or managing servers. You only pay for the compute time you consume.

What is AWS API Gateway?

AWS API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs.

Prerequisites

Before getting started, you’ll need:

  • Basic knowledge of JavaScript and Node JS.
  • AWS CLI installed and configured on your local machine.
  • A text editor like VSCode.
  • Node JS to run serverless on your local

Step-by-Step Guide

Step 1: Set Up Your Environment

  • Install AWS CLI: Follow the official AWS CLI installation guide.
  • Configure AWS CLI: Run aws configure and provide your AWS Access Key, Secret Key, region, and output format.
  • Install Serverless Framework: Install it globally using npm, we are also installing serverless-offline plugin so we can run it later locally
npm install -g serverless serverless-offline

Step 2: Create a Serverless App

  • Create a new service: Navigate to your project directory and run:
serverless create --template aws-nodejs --path my-service
cd my-service
  • Install dependencies:
npm init -y
npm install --save aws-sdk

Step 3: Write Your Lambda Function

Open the handler.js file and write a simple function:

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Hello, Serverless!',
        input: event,
      },
      null,
      2
    ),
  };
};

Step 4: Define Your Serverless Configuration

Open serverless.yml and define your service configuration:

service: my-service

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

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

plugins:
  - serverless-offline

Step 5: Run and Test Your Application Local

serverless offline --httpPort 8080

Open your browser or use a tool like Postman to make a GET request to your endpoint URL (http://localhost:8080/hello). You should receive a response with the message “Hello, Serverless!”.

Step 6: Deploy Your Service

serverless deploy

Conclusion

AWS Serverless offers a powerful and scalable way to build modern applications without worrying about infrastructure management. By leveraging services like AWS Lambda and API Gateway, you can quickly deploy and manage applications with ease

Thankyou for reading, Happy Coding!

Get In Touch

Guru Gobind Singh Nagar, Jalandhar, Punjab, India

Quick Links

© 2024 Khaalis Technologies. All Rights Reserved. Designed by HTML Codex