Skip to main content

Create a Simple API Project

Overview

In this tutorial, we will design and deploy a simple Serverless CRUD application. A CRUD application is an application that supplies the 4 basic DB commands: Create, Read, Update and Delete. Most of the services in the world support at least part of these actions.

Our Serverless application will include the following resources:

  • API Gateway to receive our calls
  • A Lambda function to handle the requests
  • A DynamoDB table to store and retrieve our data

Let's get started!

Things you'll need for this tutorial

  1. An Altostra Account (Don't have one yet? Just login here)
  2. Altostra CLI installed (npm i -g @altostra/cli or see docs)
  3. Altostra extension for Visual Studio Code (VSCode Marketplace or see docs)
  4. A connected AWS cloud account ( Web Console settings or see docs)
  5. An Environment connected to your AWS Account (Web Console environments or see docs) - We'll call it Dev for berevity, but you can pick any of your environments

Create a new project

  1. In Visual Studio Code, open the directory in which you wish to create the project.
  2. Switch to the Altostra view
  3. Click "Initialize Project"
  4. In the Templates list, click simple-crud-service-nodejs

generate-scalable-webhook

The template is applied to the current project, and now you've got the architecture of a basic Serverless application, with basic code implementation.

scalable-webhook-architecture

Deploy the Project

Now that we have the project, first we push a new version:

alto push v0.1

A Version is basically your infrastructure and code before they've been adapted to a specific environment. A Version can be deployed to any environment. You can read more about Altostra versions here.

And then, we deploy the project from that version to the development environment:

alto deploy sampleDeployment:v0.1 --env Dev

A Stack is a live release of your version in a specific environment - in this case, the Dev environment.

For information about deploying projects, read the Deploy a Project guide.

That's it - the project is deployed.

deploy-scalable-webhook

Let's Test!

First, let's find out our API Gateway address:

Run alto console. It will take you directly to your project page in the Altostra's Web Console.

Click on the stack name sampleDeploymet - and you'll get to the current deployed stack.

Click on the arrow at the right end of the first line - this will send you directly to your AWS stack.

Click the ApiGateway, and select the resource endpoint under Prod stage and copy the "invoke URL"

api-gateway

Test

Now, lets send a sample request:

curl --location --request POST 'https://mhrqao6fl2.execute-api.us-east-1.amazonaws.com/Prod/resource' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "john doe",
"email": "john.doe@gmail.com"
}'

You should get this response:

HTTP/2 200
content-type: application/json
content-length: 0

Now you can go to your DynamoDB table and check for entries:

dynamoo-db

Now let's test our GET request:

curl --location --request GET 'https://mhrqao6fl2.execute-api.us-east-1.amazonaws.com/Prod/resource/{<YOUR RESOURCE PK>}'

You should get a similar response:

{
"id": "50a1e83dc2200a826a289fd27772fa28",
"email": "john.doe@gmail.com",
"name": "john doe"
}

Congratulation! You've got a full blown Serverless application!

Ready to Get Started?

Get Started for Free
© 2022 Altostra, Inc.