Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.handler = (event, context, callback) => {
// It changes to a boolean value with `!!`
context.callbackWaitsForEmptyEventLoop =
!!event.callbackWaitsForEmptyEventLoop
if (event.asyncTest) {
setTimeout(() => console.log('sleep 3500 msec'), 3500)
}
// https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-lambda.html
// For testing AWSXRay support
AWSXRay.captureFunc('annotations', (subsegment) => {
subsegment.addAnnotation('Name', 'name')
subsegment.addAnnotation('UserID', 'piyo')
})
/* eslint-disable no-eval */
eval(event.callbackCode)
}
var myFunction = function(event, context, callback) {
var sns = new AWS.SNS();
var chance = new Chance();
var userid = event.userid;
var name = chance.first();
AWSXRay.captureFunc('annotations', function(subsegment){
subsegment.addAnnotation('Name', name);
subsegment.addAnnotation('UserID', event.userid);
});
// Notify
var params = {
Message: 'Created randon name "' + name + '"" for user "' + userid + '".',
Subject: 'New user: ' + name,
TopicArn: process.env.TOPIC_ARN
};
sns.publish(params, function(err, data) {
if (err) {
console.log(err, err.stack);
callback(err);
}
else {
export function addTraceContextToXray(eventType: string, traceContext?: TraceContext) {
let val: Record = {
"event-source": eventType,
};
if (traceContext !== undefined) {
val = {
...val,
"parent-id": traceContext.parentID,
"sampling-priority": traceContext.sampleMode.toString(10),
"trace-id": traceContext.traceID,
};
}
captureFunc(xraySubsegmentName, (segment) => {
segment.addMetadata(xraySubsegmentKey, val, xraySubsegmentNamespace);
});
}