Last active
March 19, 2019 22:31
-
-
Save garthk/830c4375a757e32d823949830ef51a89 to your computer and use it in GitHub Desktop.
Generate an AWS CloudWatch Logs event as if an AWS Lambda executed
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 uuid = require("uuid"); | |
const zlib = require("zlib"); | |
const LAMBDA = "your-lambda-function"; | |
const VERSION = "$LATEST"; | |
const DURATION = 4321; | |
const OWNER = "123456789012"; | |
function makeCloudWatchLambdaEvent() { | |
const when = Date.now() - DURATION - 1; | |
const logGroup = `/aws/lambda/${LAMBDA}`; | |
const streamNonce = uuid | |
.v4() | |
.replace(/-/g, "") | |
.slice(0, 32); | |
const logStream = `${new Date() | |
.toISOString() | |
.slice(0, 10)}/[${VERSION}]${streamNonce}`; | |
const requestId = uuid.v4(); | |
const begin = when - DURATION - 1; | |
console.error(`Expect ${requestId} starting ${new Date(when).toISOString()}`); | |
const rounded = Math.ceil(DURATION / 100) * 100; | |
const end = when + DURATION; | |
const logEvents = [ | |
{ | |
id: uuid.v4(), | |
timestamp: begin, | |
message: `START RequestId: ${requestId} Version: ${VERSION}` | |
}, | |
{ | |
id: uuid.v4(), | |
timestamp: end, | |
message: `END RequestId: ${requestId}` | |
}, | |
{ | |
id: uuid.v4(), | |
timestamp: end + 1, | |
message: `REPORT RequestId: ${requestId} Duration: ${DURATION.toFixed( | |
0 | |
)}.00 ms Billed Duration: ${rounded.toFixed( | |
0 | |
)} ms Memory Size: 1024 MB Max Memory Used: 84 MB` | |
} | |
]; | |
const payload = { | |
messageType: "DATA_MESSAGE", | |
owner: OWNER, | |
logGroup, | |
logStream, | |
subscriptionFilters: [], | |
logEvents | |
}; | |
const stringified = JSON.stringify(payload); | |
const compressed = zlib.gzipSync(new Buffer(stringified, "utf8")); | |
const data = compressed.toString("base64"); | |
return { awslogs: { data } }; | |
} | |
module.exports = makeCloudWatchLambdaEvent; |
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
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added “unlicense” license.