Skip to content

Instantly share code, notes, and snippets.

@AndrewBestbier
Created June 13, 2020 12:56
Show Gist options
  • Save AndrewBestbier/6714b3a1007e398f97959f0982fe474a to your computer and use it in GitHub Desktop.
Save AndrewBestbier/6714b3a1007e398f97959f0982fe474a to your computer and use it in GitHub Desktop.
const AWS = require("aws-sdk");
const crypto = require("crypto");
// Generate unique id with no external dependencies
const generateUUID = () => crypto.randomBytes(16).toString("hex");
// Initialising the DynamoDB SDK
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async event => {
const { postCode } = JSON.parse(event.body);
const params = {
TableName: "post-codes",
Item: {
id: generateUUID(),
postCode: postCode
}
};
try {
const data = await documentClient.put(params).promise();
const response = {
statusCode: 200
};
return response; // Returning a 200 if the item has been inserted
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify(e)
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment