Welcome to Serverless Computing
Lambda function is not about eliminating servers altogether but it's basically about focusing more on code writing and deployment rather than focusing on servers.
The server part and underlying infrastructure will be handled by the cloud provider itself your major concern will be only about writing effective code which increases your cost optimization and security.
UNDERSTANDING LAMBDA FUNCTION
AWS Lambda is a computing service that lets you run your code in response to events without the need to provision or manage servers. It automatically scales your applications based on incoming requests, so you don't have to worry about capacity planning or dealing with server maintenance.
Some key points:
Lambda functions are event-driven. They are executed when triggered by events from other AWS services like S3, DynamoDB, API Gateway, etc. This allows you to build serverless applications.
You only pay for the compute time you consume. There are no servers to manage and you don't pay when your code is not running. This lowers the operational costs.
Lambda automatically scales. It can handle thousands of requests per second by provisioning more computing resources. You don't have to worry about scaling.
Lambda handles all the infrastructure management for you. This includes provisioning compute resources, monitoring functions and logging output.
Lambda integrates well with other AWS services. You can easily connect Lambda functions to services like API Gateway, S3, DynamoDB, etc. This allows you to build full serverless applications.
Lambda functions are short-lived. They are meant to execute individual tasks and then terminate. This makes serverless architectures inherently distributed and scalable.
In summary, Lambda functions fit into the serverless world by:
Allowing you to run your code without managing infrastructure
Only charging you when your code is executing
Scaling automatically based on load
Integrating easily with other services to build full serverless applications
Functioning as individual tasks that execute and terminate quickly
This makes Lambda a natural fit for building event-driven, serverless applications where you only pay for the resources you consume.
Now, we will write our first Lambda Function:
- Login to your AWS account and search for "Lambda".
Now, click on "create function".
Now, you will see three options for how you want to write your code
Author from scratchStart (with a simple Hello World example)
Use a blueprint 9Build a Lambda application from sample code and configuration presets for common use cases)
Container image (Select a container image to deploy for your function)
Note: For learning purposes, we will select "Author from scratch start"
Function Name: test
Runtime: python 3.10
Note: you can use any language like Node.js, Ruby and Python.
- Now, go to the "Advanced Settings" and enable "Enable function URL".
This will provide you with a public IP address to access your application that is written in Lambda function, if you do not enable it you can't access your application from the outside world.
- Auth type: NONE
This parameter will ask you about who wants to access it anyone or an IAM user.
- Keep the rest of the things as default and click on "Create function".
So, here we have created our test function and inside our function, we have two options:
Add trigger (Lambda functions are event-triggering functions and hence they are triggered by a specific function it can be Cloudwatch or some API-driven event)
Add destination
Now, if you scroll down you will see the code source inside that we have our hello world lambda function
import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Here lambda_handler is the first function called by this serverless architecture and its similar to the main function of Java or C++.
We can also upload the file from our local or S3 storage.
Now, if you go to the configurations section you will see "Environment Variables" which you can create and use inside your script.
Now, if you copy your Function URL and paste it on your browser you will see that your application is running using that URL.
That's a wrap.......................