|
164 | 164 | "Type": "AWS::Lambda::Function",
|
165 | 165 | "Properties": {
|
166 | 166 | "Code": {
|
167 |
| - "ZipFile": "// source: https://github.com/aws-samples/aws-bookstore-demo-app/blob/master/functions/setup/uploadBooks.js\n\"use strict\";\n\nconst https = require(\"https\");\nconst url = require(\"url\");\n\nvar AWS = require(\"aws-sdk\");\nlet documentClient;\nlet s3Client;\nif (process.env.LOCALSTACK_HOSTNAME) {\n const localStackS3Config = {\n endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT}`,\n s3ForcePathStyle: true,\n accessKeyId: 'test',\n secretAccessKey: 'test',\n region: 'us-east-1',\n };\n s3Client = new AWS.S3(localStackS3Config);\n\n documentClient = new AWS.DynamoDB.DocumentClient({\n endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT}`,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n // Use the default AWS configuration\n s3Client = new AWS.S3();\n documentClient = new AWS.DynamoDB.DocumentClient();\n}\n\n// UploadBooks - Upload sample set of books to DynamoDB\nexports.handler = function(event, context, callback) {\n getBooksData().then(function(data) {\n var booksString = data.Body.toString(\"utf-8\");\n console.log(\"received booksString\");\n var booksList = JSON.parse(booksString);\n console.log(\"parsing bookslist\");\n uploadBooksData(booksList);\n console.log(\"uploaded books\");\n }).catch(function(err) {\n console.log(err);\n var responseData = { Error: \"Upload books failed\" };\n console.log(responseData.Error);\n });\n\n return;\n};\nfunction uploadBooksData(book_items) {\n var items_array = [];\n for (var i in book_items) {\n var book = book_items[i];\n console.log(book.id)\n var item = {\n PutRequest: {\n Item: book\n }\n };\n items_array.push(item);\n }\n\n // Batch items into arrays of 25 for BatchWriteItem limit\n var split_arrays = [], size = 25;\n while (items_array.length > 0) {\n split_arrays.push(items_array.splice(0, size));\n }\n\n split_arrays.forEach( function(item_data) {\n putItem(item_data)\n });\n}\n\n// Retrieve sample books from aws-bookstore-demo S3 Bucket\nfunction getBooksData() {\n var params = {\n Bucket: process.env.S3_BUCKET, // aws-bookstore-demo\n Key: process.env.FILE_NAME // data/books.json\n };\n return s3Client.getObject(params).promise();\n}\n\n\nfunction putItem(items_array) {\n var tableName = process.env.TABLE_NAME;\n var params = {\n RequestItems: {\n [tableName]: items_array\n }\n };\n documentClient.batchWrite(params, function(err, data) {\n if (err) console.log(err);\n else console.log(data);\n });\n}\n" |
| 167 | + "ZipFile": "// source: https://github.com/aws-samples/aws-bookstore-demo-app/blob/master/functions/setup/uploadBooks.js\n\"use strict\";\n\nconst https = require(\"https\");\nconst url = require(\"url\");\n\nvar AWS = require(\"aws-sdk\");\nlet documentClient;\nlet s3Client;\nif (process.env.AWS_ENDPOINT_URL) {\n const localStackS3Config = {\n endpoint: process.env.AWS_ENDPOINT_URL,\n s3ForcePathStyle: true,\n accessKeyId: 'test',\n secretAccessKey: 'test',\n region: 'us-east-1',\n };\n s3Client = new AWS.S3(localStackS3Config);\n\n documentClient = new AWS.DynamoDB.DocumentClient({\n endpoint: process.env.AWS_ENDPOINT_URL,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n // Use the default AWS configuration\n s3Client = new AWS.S3();\n documentClient = new AWS.DynamoDB.DocumentClient();\n}\n\n// UploadBooks - Upload sample set of books to DynamoDB\nexports.handler = function(event, context, callback) {\n getBooksData().then(function(data) {\n var booksString = data.Body.toString(\"utf-8\");\n console.log(\"received booksString\");\n var booksList = JSON.parse(booksString);\n console.log(\"parsing bookslist\");\n uploadBooksData(booksList);\n console.log(\"uploaded books\");\n }).catch(function(err) {\n console.log(err);\n var responseData = { Error: \"Upload books failed\" };\n console.log(responseData.Error);\n });\n\n return;\n};\nfunction uploadBooksData(book_items) {\n var items_array = [];\n for (var i in book_items) {\n var book = book_items[i];\n console.log(book.id)\n var item = {\n PutRequest: {\n Item: book\n }\n };\n items_array.push(item);\n }\n\n // Batch items into arrays of 25 for BatchWriteItem limit\n var split_arrays = [], size = 25;\n while (items_array.length > 0) {\n split_arrays.push(items_array.splice(0, size));\n }\n\n split_arrays.forEach( function(item_data) {\n putItem(item_data)\n });\n}\n\n// Retrieve sample books from aws-bookstore-demo S3 Bucket\nfunction getBooksData() {\n var params = {\n Bucket: process.env.S3_BUCKET, // aws-bookstore-demo\n Key: process.env.FILE_NAME // data/books.json\n };\n return s3Client.getObject(params).promise();\n}\n\n\nfunction putItem(items_array) {\n var tableName = process.env.TABLE_NAME;\n var params = {\n RequestItems: {\n [tableName]: items_array\n }\n };\n documentClient.batchWrite(params, function(err, data) {\n if (err) console.log(err);\n else console.log(data);\n });\n}\n" |
168 | 168 | },
|
169 | 169 | "Environment": {
|
170 | 170 | "Variables": {
|
|
296 | 296 | "Type": "AWS::Lambda::Function",
|
297 | 297 | "Properties": {
|
298 | 298 | "Code": {
|
299 |
| - "ZipFile": "// source adapted from https://github.com/aws-samples/aws-bookstore-demo-app\n\n\"use strict\";\n\nconst AWS = require(\"aws-sdk\");\nlet dynamoDb;\nif (process.env.LOCALSTACK_HOSTNAME) {\n dynamoDb = new AWS.DynamoDB.DocumentClient({\n endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT}`,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n dynamoDb = new AWS.DynamoDB.DocumentClient();\n}\n// GetBook - Get book informaton for a given book id\nexports.handler = (event, context, callback) => {\n\n // Return immediately if being called by warmer\n if (event.source === \"warmer\") {\n return callback(null, \"Lambda is warm\");\n }\n\n const params = {\n TableName: process.env.TABLE_NAME, // [ProjectName]-Books\n // 'Key' defines the partition key of the item to be retrieved\n // - 'id': a unique identifier for the book (uuid)\n Key: {\n id: event.pathParameters.id\n }\n };\n dynamoDb.get(params, (error, data) => {\n // Set response headers to enable CORS (Cross-Origin Resource Sharing)\n const headers = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Credentials\" : true\n };\n\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved item on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Item)\n };\n callback(null, response);\n });\n}\n" |
| 299 | + "ZipFile": "// source adapted from https://github.com/aws-samples/aws-bookstore-demo-app\n\n\"use strict\";\n\nconst AWS = require(\"aws-sdk\");\nlet dynamoDb;\nif (process.env.AWS_ENDPOINT_URL) {\n dynamoDb = new AWS.DynamoDB.DocumentClient({\n endpoint: process.env.AWS_ENDPOINT_URL,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n dynamoDb = new AWS.DynamoDB.DocumentClient();\n}\n// GetBook - Get book informaton for a given book id\nexports.handler = (event, context, callback) => {\n\n // Return immediately if being called by warmer\n if (event.source === \"warmer\") {\n return callback(null, \"Lambda is warm\");\n }\n\n const params = {\n TableName: process.env.TABLE_NAME, // [ProjectName]-Books\n // 'Key' defines the partition key of the item to be retrieved\n // - 'id': a unique identifier for the book (uuid)\n Key: {\n id: event.pathParameters.id\n }\n };\n dynamoDb.get(params, (error, data) => {\n // Set response headers to enable CORS (Cross-Origin Resource Sharing)\n const headers = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Credentials\" : true\n };\n\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved item on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Item)\n };\n callback(null, response);\n });\n}\n" |
300 | 300 | },
|
301 | 301 | "Environment": {
|
302 | 302 | "Variables": {
|
|
405 | 405 | "Type": "AWS::Lambda::Function",
|
406 | 406 | "Properties": {
|
407 | 407 | "Code": {
|
408 |
| - "ZipFile": "// source adapted from https://github.com/aws-samples/aws-bookstore-demo-app\n\n\"use strict\";\n\nconst AWS = require(\"aws-sdk\");\nlet dynamoDb;\nif (process.env.LOCALSTACK_HOSTNAME) {\n dynamoDb = new AWS.DynamoDB.DocumentClient({\n endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT}`,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n dynamoDb = new AWS.DynamoDB.DocumentClient();\n}\n// ListBooks - List all books or list all books in a particular category\nexports.handler = (event, context, callback) => {\n\n // Return immediately if being called by warmer\n if (event.source === \"warmer\") {\n return callback(null, \"Lambda is warm\");\n }\n\n // Set response headers to enable CORS (Cross-Origin Resource Sharing)\n const headers = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Credentials\" : true\n };\n\n // Query books for a particular category\n if (event.queryStringParameters) {\n const params = {\n TableName: process.env.TABLE_NAME, // [ProjectName]-Books\n IndexName: \"category-index\",\n // 'KeyConditionExpression' defines the condition for the query\n // - 'category = :category': only return items with matching 'category' index\n // 'ExpressionAttributeValues' defines the value in the condition\n // - ':category': defines 'category' to be the query string parameter\n KeyConditionExpression: \"category = :category\",\n ExpressionAttributeValues: {\n \":category\": event.queryStringParameters.category\n }\n };\n dynamoDb.query(params, (error, data) => {\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved items on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Items)\n };\n callback(null, response);\n });\n }\n\n // List all books in bookstore\n else {\n const params = {\n TableName: process.env.TABLE_NAME // [ProjectName]-Books\n };\n\n dynamoDb.scan(params, (error, data) => {\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved items on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Items)\n };\n callback(null, response);\n });\n }\n}\n" |
| 408 | + "ZipFile": "// source adapted from https://github.com/aws-samples/aws-bookstore-demo-app\n\n\"use strict\";\n\nconst AWS = require(\"aws-sdk\");\nlet dynamoDb;\nif (process.env.AWS_ENDPOINT_URL) {\n dynamoDb = new AWS.DynamoDB.DocumentClient({\n endpoint: process.env.AWS_ENDPOINT_URL,\n region: 'us-east-1', // Change the region as per your setup\n }\n );\n} else {\n dynamoDb = new AWS.DynamoDB.DocumentClient();\n}\n// ListBooks - List all books or list all books in a particular category\nexports.handler = (event, context, callback) => {\n\n // Return immediately if being called by warmer\n if (event.source === \"warmer\") {\n return callback(null, \"Lambda is warm\");\n }\n\n // Set response headers to enable CORS (Cross-Origin Resource Sharing)\n const headers = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Credentials\" : true\n };\n\n // Query books for a particular category\n if (event.queryStringParameters) {\n const params = {\n TableName: process.env.TABLE_NAME, // [ProjectName]-Books\n IndexName: \"category-index\",\n // 'KeyConditionExpression' defines the condition for the query\n // - 'category = :category': only return items with matching 'category' index\n // 'ExpressionAttributeValues' defines the value in the condition\n // - ':category': defines 'category' to be the query string parameter\n KeyConditionExpression: \"category = :category\",\n ExpressionAttributeValues: {\n \":category\": event.queryStringParameters.category\n }\n };\n dynamoDb.query(params, (error, data) => {\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved items on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Items)\n };\n callback(null, response);\n });\n }\n\n // List all books in bookstore\n else {\n const params = {\n TableName: process.env.TABLE_NAME // [ProjectName]-Books\n };\n\n dynamoDb.scan(params, (error, data) => {\n // Return status code 500 on error\n if (error) {\n const response = {\n statusCode: 500,\n headers: headers,\n body: error\n };\n callback(null, response);\n return;\n }\n\n // Return status code 200 and the retrieved items on success\n const response = {\n statusCode: 200,\n headers: headers,\n body: JSON.stringify(data.Items)\n };\n callback(null, response);\n });\n }\n}\n" |
409 | 409 | },
|
410 | 410 | "Environment": {
|
411 | 411 | "Variables": {
|
|
0 commit comments