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
- An Altostra Account (Don't have one yet? Just login here)
- Altostra CLI installed (
npm i -g @altostra/cli
or see docs) - Altostra extension for Visual Studio Code (VSCode Marketplace or see docs)
- A connected AWS cloud account ( Web Console settings or see docs)
- 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
- In Visual Studio Code, open the directory in which you wish to create the project.
- Switch to the Altostra view
- Click "Initialize Project"
- In the Templates list, click simple-crud-service-nodejs
The template is applied to the current project, and now you've got the architecture of a basic Serverless application, with basic code implementation.
Deploy the Project
Now that we have the project, first we push a new image:
$ alto push v0.1
An Image is basically your infrastructure and code before they've been adapted to a specific environment. An Image can be deployed to any environment. You can read more about Altostra images here.
And then, we deploy the project from that image to the development environment:
$ alto deploy sampleDeployment:v0.1 --env Dev
An Instance is a live release of your image 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.
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 instance 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"
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:
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!