i have written lambda code tracks login aws console, , send notification user email it.
the initial code have written in java, , worked. after converting code scala, came following code:
class snshandler { private val creds: awscredentials = new basicawscredentials("xxx", "999/xyz12345") private val eventtype: string = "consolelogin" private val topicarn: string = "arn:aws:sns:us-east-1:1111111111:ctinterestingevents" def processloginrecord(loginrecord: string, lambdalogger: lambdalogger) = { val username = jsonpath.read(loginrecord.asinstanceof[object], "$.useridentity.type").asinstanceof[string] match { case "root" => "root" case _ => jsonpath.read(loginrecord.asinstanceof[object], "$.useridentity.username") } val accountid = jsonpath.read(loginrecord.asinstanceof[object], "$.useridentity.accountid") new amazonsnsclient(creds).publish(topicarn, "this auto notification message.\nuser " + username + " has logged in aws account id " + accountid + ".\n receiving email because has subscribed your" + " email address event.") } def processcloudtrailbulk(event: string, logger: lambdalogger) = { jsonpath.read(event.asinstanceof[object], "$.records[?(@.eventname == '" + eventtype + "' && @.responseelements.consolelogin == 'success')]"). asinstanceof[java.util.list[string]].asscala.map(loginrecord => processloginrecord(loginrecord, logger)) } def processs3file(bucketname: string, file: string, logger: lambdalogger) = { source.frominputstream(new gzipinputstream(new amazons3client(creds). getobject(new getobjectrequest(bucketname, file)).getobjectcontent),"utf-8").getlines(). foreach(line => processcloudtrailbulk(line,logger)) } def processsnsrecord(notification: snsrecord, logger: lambdalogger) = { val bucketname: string = jsonpath.read(notification.getsns.getmessage.asinstanceof[object], "$.s3bucket") logger.log("notifications arrived.\nbucket: " + bucketname) jsonpath.read(notification.getsns.getmessage.asinstanceof[object], "$.s3objectkey[*]").asinstanceof[java.util.list[string]]. asscala.map(file => processs3file(bucketname,file,logger)) } def handlecloudtrailincoming(event: snsevent, context: context) = { event.getrecords.asscala.map(record => processsnsrecord(record,context.getlogger)) } } now, addition of .asinstanceof[object] first param of every 'read' call wasn't there initially, had famous compiler error of ambiguous reference overloaded function, , after taking at: ambiguous reference overloaded definition, java library added it, , indeed code compiles.
the problem in runtime, read fails detect fields, , following error:
property ['s3bucket'] not found in path $: com.jayway.jsonpath.pathnotfoundexception com.jayway.jsonpath.pathnotfoundexception: property ['s3bucket'] not found in path $ @ com.jayway.jsonpath.internal.token.propertypathtoken.evaluate(propertypathtoken.java:41) ........
Comments
Post a Comment