-
-
Save kylenstone/dd8a0c8dfa1a7440101bf5e23f58c6f8 to your computer and use it in GitHub Desktop.
Lambda calling lambda with trace id
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
// Lambda 1 - arv-foo | |
// Synchronous fn that will back API Gateway endpoint. | |
const AWS = require('aws-sdk'); | |
const lambda = new AWS.Lambda(); | |
const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max)); | |
exports.handler = async (event, context) => { | |
console.log("event:", event) | |
const traceId = (event.headers || {})["x-amzn-trace-id"] || getRandomInt(999999); | |
const barPayload = { | |
FunctionName: "arv-bar", | |
Payload: JSON.stringify({ | |
caller: "Called from arv-foo", | |
headers: { | |
"x-amzn-trace-id": traceId, | |
}, | |
}), | |
InvocationType: "Event", | |
} | |
const barResponse = await lambda.invoke(barPayload).promise(); | |
console.log('arv-bar response:', barResponse); | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify({ | |
message: 'Lambda arv-foo returning response', | |
traceId, | |
}), | |
}; | |
return response; | |
}; | |
// Lambda 2 - arv-bar | |
// Asynchronous worker fn that will do some hrd work. | |
exports.handler = async (event) => { | |
console.log('arv-bar event:', event); | |
await new Promise(r => setTimeout(r, 2000)); | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('Lambda arv-bar finished.'), | |
}; | |
return response; | |
}; | |
// Lambda 2 clouldwatch logs example: | |
2020-09-08T14:21:18.219Z 62b1bc63-2862-43a6-b0e6-44f1633b2e64 INFO arv-bar event: { | |
caller: 'Called from arv-foo', | |
headers: { 'x-amzn-trace-id': 917093 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment