Private Serverless REST APIs With AWS Lambda Using SAM

Private Serverless REST APIs With AWS Lambda Using SAM

Quickly create serverless microservices using Lambdas. By using Amazon API Gateway, it's possible to quickly create serverless microservices backed by Lambda. We'll review one such deployment ...

By using Amazon API Gateway, it’s possible to quickly create serverless microservices backed by Lambda. We’ll review one such deployment and use the AWS Serverless Application Model (SAM) to deploy it.

Introduction

With ever-growing cloud systems, many companies have opted to move away from monoliths into a distributed architecture composed of microservices. AWS allows the development of such services without the need to manage the underlying server infrastructure.

We’ll go over an infrastructure-as-code (IaC) approach to deploying one such service using SAM. The deployment will assume that a virtual private cluster (VPC) is already in place and that only VMs on specific subnets within the cluster should be able to access the private service. We’ll also assume that the developer already has the necessary AWS SAM tools installed locally.

1. Files and Folders

my-microservice/
├── cmd/
│   ├── debug.sh
│   └── deploy.sh
├── src/
│   ├── index.js
│   └── package.js
├── .gitignore 
├── test-event.json
└── template.yaml

The cmd folder will contain our helper scripts for the deploying and local debugging of our function, as shown below:

debug.sh: Runs the function on the local computer using test-event.json as input.

#!/bin/bash

script_path=$(cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P)
sam local invoke \  
  --event ${script_path}/../test-event.json \  
  --template-file ${script_path}/../template.yaml

deploy.sh: Deploys the service to AWS.

#!/bin/bash

## exit when any command fails
set -e
script_path=$(cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P)
sam deploy \  
  --template-file ${script_path}/../template.yaml \  
  --stack-name my-microservice \  
  --capabilities CAPABILITY_IAM \  
  --guided

The src folder will contain Lambda’s source code. In this case, it consists of Node index.js and package.json files. However, the same technique that will be shown here would work with any of the other supported Lambda languages as well.

package.json

{  
  "name": "my-lambda-code",  
  "version": "1.0.0",  
  "description": "AWS Lambda function for microservice",  
  "main": "index.js",  
  "scripts": {    
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "author": "",  
  "license": "ISC",  
  "dependencies": {},  
  "devDependencies": {}
}

api programming aws serverless aws-lambda

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Serverless Express – Easy APIs On AWS Lambda & AWS HTTP API

Serverless Express enables you to easily host Express.js APIs on AWS Lambda and AWS HTTP API. Here is how to get started and deliver a Serverless Express.js based API with a custom domain, free SSL certificate and much more!

Build a Serverless API with AWS Gateway and Lambda

In this article, we explain general API implementation concepts using AWS Lambda and other AWS services. APIs are a crucial part of any web application and there are different techniques for development and design. Serverless is one approach gaining popularity, because of its cost-efficiency, scalability and relative simplicity. As a leading serverless provider, Amazon Web Services (AWS) has made a huge contribution to the world of serverless development, and in this article, we will explain general API implementation concepts using AWS Lambda and other AWS services.

How to Build an API in AWS - Using AWS Lambda and API Gateway

APIs, short for Application Programming Interfaces, are connection points that allow different (parts of) computer programs to exchange data. Using APIs for the delivery of a software service makes your code better organized and easily reusable. In this article, I am going through the basic steps of creating an API using AWS Lambda and API Gateway.

AWS Lambda Rest Api | Rest Api Using AWS Lambda, DynamoDB and Serverless Framework

In this video, we take a look at building a rest api using aws lambda and dynamodb using the serverless framework. Serverless architecture can seem a little ...

WebSockets on Demand With AWS Lambda, Serverless Framework, and Go

Lambda functions and WebSockets — how the opposites meet in the cloud. In this article, we describe how to implement a WebSocket server using AWS API Gateway and Lambda functions.