Create simple CRUD Operation Using Serverless Framework
Install Serverless framework globally
npm install serverless -g
Create simple Lambda Service
serverless create --template aws-nodejs --path myService
Go to the myService Directory it should contain
All Serverless service configuration is managed by serverless.yml
Change its contents to define CRUD service.
Example serverless.yml file
service: serverless-crud
provider:
name: aws
runtime: nodejs4.3
region: us-east-1
stage: dev
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:us-east-1:*:*"
functions:
create:
handler: handler.create
events:
- http:
path: todos
method: post
cors: true
readAll:
handler: handler.readAll
events:
- http:
path: todos
method: get
cors: true
readOne:
handler: handler.readOne
events:
- http:
path: todos/{id}
method: get
cors: true
update:
handler: handler.update
events:
- http:
path: todos/{id}
method: put
cors: true
delete:
handler: handler.delete
events:
- http:
path: todos/{id}
method: delete
cors: true
resources:
Resources:
TodosDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: id
AttributeType: S
KeySchema:
-
AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: 'todos'
This file define
'use strict';
const todosCreate = require('./todos-create.js');
const todosReadAll = require('./todos-read-all.js');
const todosReadOne = require('./todos-read-one.js');
const todosUpdate = require('./todos-update.js');
const todosDelete = require('./todos-delete.js');
module.exports.create = (event, context, callback) => {
todosCreate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*"
},
body: JSON.stringify(result),
};
context.succeed(response);
});
};
module.exports.readAll = (event, context, callback) => {
todosReadAll(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*"
},
body: JSON.stringify(result),
};
context.succeed(response);
});
};
module.exports.readOne = (event, context, callback) => {
todosReadOne(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*"
},
body: JSON.stringify(result),
};
context.succeed(response);
});
};
module.exports.update = (event, context, callback) => {
todosUpdate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*"
},
body: JSON.stringify(result),
};
context.succeed(response);
});
};
module.exports.delete = (event, context, callback) => {
todosDelete(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*"
},
body: JSON.stringify(result),
};
context.succeed(response);
});
};
Then you have to create new files for define your CRUD functions
Create these files
Then define these functions in each file.
for todos-create.js
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const uuid = require('uuid');
module.exports = (event, callback) => {
const data = JSON.parse(event.body);
data.id = uuid.v1();
data.updatedAt = new Date().getTime();
const params = {
TableName: 'todos',
Item: data
};
return dynamoDb.put(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, params.Item);
});
};
For todos-read-all.js
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (event, callback) => {
const params = {
TableName: 'todos',
};
return dynamoDb.scan(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, data.Items);
});
};
For todos-read-one.js <br>
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (event, callback) => {
const params = {
TableName: 'todos',
Key: {
id: event.pathParameters.id
}
};
return dynamoDb.get(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, data.Item);
});
};
For todos-update.js
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (event, callback) => {
const data = JSON.parse(event.body);
data.id = event.pathParameters.id;
data.updatedAt = new Date().getTime();
const params = {
TableName : 'todos',
Item: data
};
return dynamoDb.put(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, params.Item);
});
};
For todos-delete.js
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (event, callback) => {
const params = {
TableName : 'todos',
Key: {
id: event.pathParameters.id
}
};
return dynamoDb.delete(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, params.Key);
});
};
For the run these application you need install npm dependencies
npm init
npm initializationnpm install aws-sdk --save
install aws-sdknpm install uuid --save
Deployment
Now you can deploy these project
cd myService
verify you are in project directory then you can deploy your code
serverless deploy
Use End Point
If you successfully deployed you view api gateway end pont names in your console.
Test Create End Point
curl -X POST https://XXXX.execute-api.region.amazonaws.com/dev/todos --data '{ "body" : "Learn Serverless" }'
Test for Read End Point (Read All)
curl https://XXXX.execute-api.region.amazonaws.com/dev/todos
Test for Read End Point (Read One)
curl https://XXXX.execute-api.region.amazonaws.com/dev/todos/<id>
Test for Update End Point
curl -X PUT https://XXXX.execute-api.region.amazonaws.com/dev/todos/<id> --data '{ "body" : "Understand Serverless" }'
Test for Delete End Point
`curl -X DELETE https://XXXX.execute-api.region.amazonaws.com/dev/todos/<id`>