AMAZON and serverless(5)Lambda Invoke and Log
AMAZONandserverless(5)LambdaInvokeandLog
Logging
Thereisaloggingpackagecalleddebug
>npminfodebugversion
3.1.0
https://github.com/visionmedia/debug#readme
importdebugfrom'debug';
constlogger=debug('acknowledgehandler');
constrecord=JSON.parse(parameters);
logger(record);
logger('Meetingcreationsucceeded');
ThenwecanconfiguretheDEBUG=*toshowallthelogging,orconfigureDEBUG=acknowldegehandlertoshowonlysomelogging.
LambdaDirectlyInvoke
import{APIGatewayEvent,Callback,Context}from'aws-lambda';
constaws_region=process.env.AWS_REGION;
constlambda=newAWS.Lambda({region:aws_region});
exportasyncfunctioninvokeLambda(functionName:string,request:any){
constparams={
FunctionName:functionName,
InvocationType:'RequestResponse',
LogType:'None',
Payload:JSON.stringify(request)
};
try{
console.log('invokinglambda:',params);
constresponse=awaitlambda.invoke(params).promise();
if(!response){
console.error(`failuregettingresultfrom${functionName}lambda`)
thrownewError(`failuregettingresultfrom${functionName}lambda`);
}
console.log('lambdaresponse:',response);
constresponseObj=JSON.parse(response.Payloadasstring);
conststatusCode=idx(responseObj,_=>_.statusCode);
conststatusMessage=idx(responseObj,_=>_.statusMessage);
if(statusCode<200||202<statusCode){
thrownewError(`BadstatusCodereturndby${functionName}lambda:${statusCode}-${statusMessage}`);
}
returnidx(responseObj,_=>_.body)||{};
}catch(err){
throwerr;
}
}
exportasyncfunctiongetExtension(){
returnawaitinvokeLambda(get_extension_lambda,{});
}
TypeScriptValidation
>npminfojoiversion
13.2.0
https://github.com/hapijs/joi
importJoi=require('joi’);
constmeetingCreateSchema=Joi.object().keys({
displayName:Joi.string()
.min(1)
.optional(),
description:Joi.string()
.min(1)
.optional(),
pin:Joi.number()
.min(1)
.optional(),
moderatorExtension:Joi.number()
.min(1)
.optional(),
ownerExtension:Joi.number()
.min(1)
.optional(),
lecturerExtension:Joi.number()
.min(1)
.optional(),
tempMeeting:Joi.boolean()
.optional(),
hiddenMeeting:Joi.boolean()
.optional()
});
constmeetingUpdateSchema=Joi.object().keys({
uuid:Joi.string()
.uuid({version:['uuidv4']})
.required(),
displayName:Joi.string()
.min(1)
.optional(),
description:Joi.string()
.min(1)
.optional(),
pin:Joi.number()
.min(1)
.optional(),
moderatorExtension:Joi.number()
.min(1)
.optional(),
ownerExtension:Joi.number()
.min(1)
.optional(),
lecturerExtension:Joi.number()
.min(1)
.optional(),
tempMeeting:Joi.boolean()
.optional(),
hiddenMeeting:Joi.boolean()
.optional()
});
constmeetingDeleteSchema=Joi.object().keys({
uuid:Joi.string()
.uuid({version:['uuidv4']})
.required(),
});
constvalidationMapByOperation={
'/meeting/create':meetingCreateSchema,
'/meeting/update':meetingUpdateSchema,
'/meeting/delete':meetingDeleteSchema
};
constschema=validationMapByOperation[uriSuffix];
constvalidationResult=Joi.validate(parameters,schema,{convert:true,allowUnknown:false});
if(validationResult.error){
console.log('validationerror:',validationResult);
consterrorMessage=idx(validationResult,_=>_.error.details[0].message);//returnsthefirstvalidationerror
returnerrorResponse(cb,401,`${uriSuffix}:${errorMessage}`);
}
References:
https://github.com/visionmedia/debug#readme
相关推荐
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算。以上实例中 7、5 和 12 是操作数。关系运算符用于计算结果是否为 true 或者 false。逻辑运算符用于测定变量或值之间的逻辑。