- AWS Lambda - Additional Example
- Monitoring and TroubleShooting using Cloudwatch
- AWS Lambda@Edge with CloudFront
- Lambda Function with Custom User Applications
- Lambda Function with Amazon Kinesis
- Lambda Function with CloudTrail
- Lambda Function with Amazon SNS
- Lambda Function with Scheduled Events
- Lambda Function with Amazon DynamoDB
- Lambda Function with Amazon S3
- Working with Amazon API Gateway
- Deleting Lambda Function
- AWS Executing & Invoking Lambda Function
- Creating & Deploying using Serverless Framework
- Creating & Deploying using AWS CLI
- Creating & Deploying using AWS Console
- Configuring Lambda Function
- Function in C#
- Function in Go
- Function in Python
- Function in Java
- Function in NODEJS
- Building the Lambda function
- AWS Lambda - Introduction
- AWS Lambda - Environment Setup
- AWS Lambda - Overview
- AWS Lambda - Home
AWS Lambda Useful resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Using Lambda Function with Custom User Apppcations
We can use AWS lambda function to process using generated events by user apppcation in the following two ways −
Using AWS Console
Using AWS CLI
Using AWS Console
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 −
Using AWS CLI
We can invoke the above function using AWS CLI as follows −
aws lambda invoke --function-name "lambdauserevent" --log-type Tail -- payload file://C:cpoutputinput.txt C:cpoutputoutputfile.txt
The event details are given to payload and the output is stored at C:cpoutputoutputfile.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:cpoutputsns.txt C:cpoutputsnsoutput.txtAdvertisements