Creating simple Serverless API
In today’s fast-paced development environment, the demand for scalable, cost-effective solutions is higher than ever. Serverless computing offers a way to build and deploy applications without the hassle of managing servers. In this article, we’ll walk you through creating a simple API using the Serverless Framework and AWS.
What is Serverless?
Serverless is a cloud-computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. With serverless, you focus on writing your code, and the cloud provider takes care of the infrastructure. AWS Lambda is a popular serverless compute service that lets you run code without provisioning or managing servers.
What is the Serverless Framework?
The Serverless Framework is an open-source framework that simplifies the process of deploying serverless applications on various cloud platforms, including AWS. It provides a powerful CLI (Command Line Interface) to help you manage and deploy your serverless functions, making it easier to focus on coding rather than managing infrastructure.
Prerequisites
Before we start, make sure you have the following:
- An AWS account
- AWS CLI
- Node.js and npm installed
- Serverless Framework installed (
npm install -g serverless
)
Step 1: Set Up the Serverless Framework
First, let’s set up a new Serverless project.
serverless create --template aws-nodejs --path my-serverless-api
cd my-serverless-api
npm init -y
This will create a new directory called my-serverless-api
with a basic Serverless configuration as shown below.
Step 2: Configure AWS Credentials
To allow the Serverless Framework to deploy resources to your AWS account, you need to configure your AWS credentials. Run the script below to configure the AWS CLI credentials
aws configure
Insert the value for the following: AWS Access Key ID
, AWS Secret Access Key
, Default region name
and Default output format
. (Please note that you need to generate access key from the AWS IAM)
Step 3: Define Your Serverless Configuration
Open the serverless.yml
file in your project directory. This file defines the resources and functions for your Serverless application.
This configuration specifies a single function hello that will be triggered by an HTTP GET request to the localhost:3000/dev/hello endpoint.
Step 4: Write Your Lambda Function
Create a handler.js
file in your project directory and add the following code:
This is a simple Lambda function that returns a JSON response with a "Go Serverless v1.0! Your function executed successfully!" message and the input with event object.
Step 5: Deploy Your Serverless Application
Now that everything is set up, you can deploy your application to AWS.
serverless deploy
The deployment process may take a few minutes. Once it’s complete, you should see output similar to this:
endpoints:
GET - https://xxxxxxxx.execute-api.ap-southeast-1.amazonaws.com/dev/hello
This URL is your API endpoint.
Step 6: Test Your API
Open your browser or use a tool like curl
or Postman to test your new API endpoint.
curl https://xxxxxxxx.execute-api..ap-southeast-1.amazonaws.com/dev/hello
You should receive a JSON response with message and the input with event object.
Next topic…
I am thinking what’s the next topic I will cover. Feel free to suggest in comment section what you want to learn.
Building a Simple API with Serverless Framework and AWS — Part 2