Skip to content

Instantly share code, notes, and snippets.

@dfbq91
Created October 2, 2020 16:06
Show Gist options
  • Save dfbq91/da80607e23330ca082c30d6574c74fb1 to your computer and use it in GitHub Desktop.
Save dfbq91/da80607e23330ca082c30d6574c74fb1 to your computer and use it in GitHub Desktop.
Lambda Functions with Nodejs CRUD with DynamoDB
/* Here, there are GET, POST, PUT and DELETE implementations for Lambda functions with Nodejs CRUD using Dynamo DB.
Before the implementation using API Gateway, I used a passed parameter. This implementations are commented */
// GET doesn't change if we are using API Gateway
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const params = {
TableName: "Products"
};
try {
const data = await documentClient.scan(params).promise();
responseBody = JSON.stringify(data.Items);
statusCode = 200;
} catch (err) {
responseBody = `Unable to get Products: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
// HardCoded DELETE
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const params = {
TableName: "Products",
Key: {
id: '12345'
}
};
try {
const data = await documentClient.delete(params).promise();
responseBody = JSON.stringify(data);
statusCode = 204;
} catch (err) {
responseBody = `Unable to delete Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
// Hardcoded PUT (post)
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const params = {
TableName: "Products",
Item: {
id: '12345',
productName: "Solar Panels"
}
};
try {
const data = await documentClient.put(params).promise();
responseBody = JSON.stringify(data);
statusCode = 201;
} catch (err) {
responseBody = `Unable to put Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
// Hardcoded UPDATE (put)
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const params = {
TableName: "Products",
Key: {
id: '12345'
},
UpdateExpression: "set productName = :n",
ExpressionAttributeValues: {
":n": "Water pumps"
},
ReturnValues: "UPDATED_NEW"
};
try {
const data = await documentClient.update(params).promise();
responseBody = JSON.stringify(data);
statusCode = 204;
} catch (err) {
responseBody = `Unable to update Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
/* -------------- */
/* Now, using API Gateway. It uses event parameter */
// PUT (post)
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const { id, productName } = JSON.parse(event.body);
const params = {
TableName: "Products",
Item: {
id: id,
productName: productName
}
};
try {
const data = await documentClient.put(params).promise();
responseBody = JSON.stringify(data);
statusCode = 201;
} catch (err) {
responseBody = `Unable to put Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
// UPDATE (put)'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const { id, productName } = JSON.parse(event.body);
const params = {
TableName: "Products",
Key: {
id: id
},
UpdateExpression: "set productName = :n",
ExpressionAttributeValues: {
":n": productName
},
ReturnValues: "UPDATED_NEW"
};
try {
const data = await documentClient.update(params).promise();
responseBody = JSON.stringify(data);
statusCode = 204;
} catch (err) {
responseBody = `Unable to update Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
// DELETE
'use strict';
const AWS = require('aws-sdk'); // Load the AWS SDK for Node.js
exports.handler = async (event, context) => {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const { id } = event.pathParameters;
const params = {
TableName: "Products",
Key: {
id: id
}
};
try {
const data = await documentClient.delete(params).promise();
responseBody = JSON.stringify(data);
statusCode = 204;
} catch (err) {
responseBody = `Unable to delete Product: ${err}`;
statusCode = 403;
}
const response = {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: responseBody
};
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment