
We can use AWS lambda function to process using generated events by user application in the following two ways −
From AWS console, we will work with events and AWS Lambda. For this purpose, go to AWS console and create a lambda function.
Next, let us add the code for AWS Lambda −
exports.handler = (event, context, callback) => {
// TODO implement
console.log("Hello => "+ event.name);
console.log("Address =>"+ event.addr);
callback(null, 'Hello '+event.name +" and address is "+ event.addr);
};
Note that in the above code, we are printing name and address using event.
The details to the event will be given using the test event created as follows −
Now, save the event and test it.
The corresponding log output is as shown here −
We can invoke the above function using AWS CLI as follows −
aws lambda invoke --function-name "lambdauserevent" --log-type Tail -- payload file://C:\clioutput\input.txt C:\clioutput\outputfile.txt
The event details are given to payload and the output is stored at C:\clioutput\outputfile.txt. as follows −
input.txt
{"name":"Roy Singh", "addr":"Mumbai"}
On invoking the Lambda using AWS CLI, you can see the output is as follows −
Similarly, in case you want to test AWS Lambda for any other AWS service, you can do so using the test event in AWS console and AWS CLI. A sample event for SNS service is shown below −
{
"Records": [{
"EventVersion": "1.0",
"EventSubscriptionArn": "arnid",
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "1970-01-01T00:00:00.000Z",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "EXAMPLE",
"TopicArn": "topicarn",
"Subject": "TestInvoke"
}
}]
}
Let us add the sample event shown above and test it as shown −
In AWS Lambda, code will print the SNS message as shown in the example given below −
exports.handler = (event, context, callback) => {
// TODO implement
console.log(event.Records[0].Sns.Message);
callback(null, event.Records[0].Sns.Message);};
Let us invoke the same using AWS CLI. Let us save the event in a file and use that for payload using the command shown −
aws lambda invoke --function-name "lambdauserevent" --log-type Tail -- payload file://C:\clioutput\sns.txt C:\clioutput\snsoutput.txt