Serverless Framework - sample CRUD app using Node.js and Express.js

npm install
sls deploy
curl -X GET ‘https://<domena>/books’
curl -X GET ‘https://<domena>/books/<id>’
# Returns all books saved in DynamoDB or a book with the given id
curl -X POST ‘https://<domena>/books’ \
— header ‘Content-Type: application/json’ \
— data-raw ‘{
“bookId”: “1”,
“bookName”: “The Hobbit”,
“author”: “Tolkien”
}’
# Saves the book to DynamoDB
curl -X PUT ‘https://<domena>/books/<id>’ \
— header ‘Content-Type: application/json’ \
— data-raw ‘{
“bookName”: “The Lord of the Rings”,
“author”: “Tolkien”
}’
# Updates the book with the given id
curl -X DELETE ‘https://<domena>/books/<id>’
# Deletes the book from DynamoDB
sls deploy -f books
# books is the function name
sls offline
sls remove
service: serverless-books
plugins:
- serverless-offline

custom:
tableName: books
tableKey: bookId
serverless-offline:
useChildProcesses: true
provider:
name: aws
runtime: nodejs12.x
region: eu-west-1
tags:
project: cloud-box.pl
tracing:
lambda: true
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: 'arn:aws:dynamodb:eu-west-1:*:*'
environment:
TABLE_NAME: ${self:custom.tableName}
BUCKET_NAME: 'serverless-books-dev-bucket-name'
functions:
books:
handler: functions/books.handler
memorySize: 512
timeout: 10
events:
- httpApi:
method: "*"
path: /books
- httpApi:
method: "*"
path: /books/{params}

resources:
- ${file(resources/dynamoDb.yml)}
- ${file(resources/s3.yml)}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Artur Bartosik

Artur Bartosik

6 Followers

Developer, Engineer, and Cloud Enthusiastic. AWS, GCP, and K8S certified. Polish home page: https://cloud-box.pl