Write a sample AWS Lambda Function
What is AWS lambda
AWS Lambda is an event-driven computing cloud service from Amazon Web Services. It allows to program functions on a pay-per-use basis without having to provision storage or compute resources to support them. It gets its name from the Lambda function, the concept of an anonymous computing function, not bound to an identity.
AWS’s Function as a Service (FaaS) offering gives users the ability to develop, run, and manage application functionalities without the complexity of building and maintaining the infrastructure. This model is also known as ‘serverless’ architecture.
Features
- Event-driven architecture
- Easy development of small, on-demand applications and microservices
- Allows parallel processing , functions runs in parallel and processes each trigger individually
- Serverless Framework, Amazon manages the servers
- Easily scalable , automatically scales in response to each trigger.
- Controllable concurrency
- Supports Node.js, Python, Java, Go, Ruby and C# through .NET Core
- Each Lambda function runs in an isolated computing environment with its own resources and view of the file system
- Logging and monitoring through AWS CloudWatch
- Pay-per-use based on the number of requests served and the compute time needed to run the code, metered in increments of 100 milliseconds.
Steps to create a sample function
Step 1: Log in to your AWS account console and select the region in which you want to create your function. Select Lambda service.
Step 2: Click on create a function. Choose author from scratch. Fill name of your function. We’ll be using Python 2.7 for this sample.
Create an IAM role also known as execution role with the necessary permissions. You can choose an existing role or create new basic role or create roles from templates. Click on create the function.
Step 3: This is how your function dashboard will look. You can specify a handler (a method/function in your code) where AWS Lambda can begin executing your code. There is already a sample code present. You can click on test and see the results.
Step 4: Before writing the function we will configure test events. Select Configure Test Event from the drop-down menu called Select a test event. Type in an event name like OperationsOnNumbers. We are going to give two numbers. Create test event.
Step 5: Write code in editor. Don’t forget to click on save (top right)
Step 6: Click test. Upon successful execution, view the results in the console.
Step 7: Monitor using Cloudwatch metrics under the monitoring tab.
So, now you have created a sample lambda function. You can further explore sample codes and other templates.