Last active
August 8, 2023 10:44
-
-
Save mjzone/9052783e7520bee0b69907ae71863a52 to your computer and use it in GitHub Desktop.
AWS Lambda Monitoring - Synchronous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const products = [ | |
{ id: "1", name: "Product 1", description: "This is an amazing product 1", price: 99 }, | |
{ id: "2", name: "Product 2", description: "This is an amazing product 2", price: 199 }, | |
{ id: "3", name: "Product 3", description: "This is an amazing product 3", price: 299 }, | |
{ id: "4", name: "Product 4", description: "This is an amazing product 4", price: 399 }, | |
{ id: "5", name: "Product 5", description: "This is an amazing product 5", price: 499 }, | |
]; | |
function delay(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
module.exports.handler = async (event) => { | |
const { id } = event.pathParameters; | |
// Check if the id is not numeric | |
if (isNaN(id)) { | |
return { | |
statusCode: 400, | |
body: JSON.stringify({ message: "Invalid request. ID should be numeric." }), | |
}; | |
} | |
const product = products.find((p) => p.id === id); | |
// Introduce 0.5s delay | |
await delay(500); | |
if (!product) { | |
return { | |
statusCode: 404, | |
body: JSON.stringify({ message: "Product not found" }), | |
}; | |
} | |
return { | |
statusCode: 200, | |
body: JSON.stringify(product), | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "lambda-monitoring-sync", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"directories": { | |
"test": "tests" | |
}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^1.4.0", | |
"sharp": "^0.32.1" | |
}, | |
"devDependencies": { | |
"esbuild": "^0.17.19", | |
"serverless-esbuild": "^1.46.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
async function getProductData() { | |
const productIds = [1, 2, 3, 4, 5]; | |
const promises = productIds.map((id) => | |
axios.get(`https://<apigw_id>.execute-api.us-east-1.amazonaws.com/dev/products/${id}`) | |
); | |
try { | |
const responses = await Promise.all(promises); | |
responses.forEach((response, index) => { | |
console.log(`Product ${productIds[index]}:`, response.data); | |
}); | |
} catch (error) { | |
console.error(`Error in API call: ${error}`); | |
} | |
} | |
getProductData(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: lambda-monitoring-sync | |
provider: | |
name: aws | |
runtime: nodejs16.x | |
stage: dev | |
region: us-east-1 | |
plugins: | |
- serverless-esbuild | |
custom: | |
esbuild: | |
bundle: true | |
minify: false | |
functions: | |
getProductsById: | |
handler: functions/getProductsById.handler | |
events: | |
- http: | |
path: products/{id} | |
method: get | |
cors: true | |
request: | |
parameters: | |
paths: | |
id: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment