Skip to content

Commit def6aad

Browse files
author
Dipjyoti Metia
committed
serverless dynamodb with api gateway
1 parent b3f96fd commit def6aad

File tree

11 files changed

+506
-0
lines changed

11 files changed

+506
-0
lines changed

my-service/package-lock.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serverless-dynamodb/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless

serverless-dynamodb/handler.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports.hello = async (event, context) => {
4+
return {
5+
statusCode: 200,
6+
body: JSON.stringify({
7+
message: 'Go Serverless v1.0! Your function executed successfully!',
8+
input: event,
9+
}),
10+
};
11+
12+
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
13+
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
14+
};

serverless-dynamodb/package-lock.json

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serverless-dynamodb/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "serverless-dynamodb",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "handler.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Dipjyoti Metia",
10+
"license": "ISC",
11+
"dependencies": {
12+
"uuid": "^2.0.3",
13+
"aws-sdk": "2.350.0"
14+
}
15+
}

serverless-dynamodb/pets/create.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const uuid = require('uuid');
4+
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
5+
6+
const dynamoDb = new AWS.DynamoDB.DocumentClient();
7+
8+
module.exports.create = async (event, context, callback) => {
9+
const timestamp = new Date().getTime();
10+
const data = JSON.parse(event.body);
11+
if (typeof data.petName !== 'string' || typeof data.petBreed !== 'string') {
12+
console.error('Validation Failed');
13+
callback(new Error('Couldn\'t create the pet item.'));
14+
return;
15+
}
16+
17+
const params = {
18+
TableName: process.env.DYNAMODB_TABLE,
19+
Item: {
20+
id: uuid.v1(),
21+
petName: data.petName,
22+
petBreed: data.petBreed,
23+
createdAt: timestamp,
24+
updatedAt: timestamp,
25+
},
26+
};
27+
28+
dynamoDb.put(params, (error) => {
29+
if (error) {
30+
console.error(error);
31+
callback(new Error('Couldn\'t create the pet item.'));
32+
return;
33+
}
34+
const response = {
35+
statusCode: 200,
36+
body: JSON.stringify(params.Item),
37+
};
38+
callback(null, response);
39+
});
40+
};

0 commit comments

Comments
 (0)