Welcome to Chronicles, let's learn more about AWS Lambda a serverless service.
Description:
AWS Lambda is a serverless computing service offered by Amazon Web Services (AWS) that allows developers to run code in response to events without the need to provision or manage servers. It is a key component of the serverless architecture, which enables developers to focus on writing code and building applications without worrying about the underlying infrastructure.
Here are some key points about AWS Lambda:
-
Event-Driven Execution: AWS Lambda is designed to execute code in response to various events, such as HTTP requests, changes in data stored in AWS services like Amazon S3, database updates, and more. It can be triggered by a wide range of AWS services and custom events.
-
Supported Languages: Lambda supports multiple programming languages, including Node.js, Python, Java, Ruby, C#, Go, and custom runtimes. This flexibility allows developers to choose the language that best fits their needs.
-
Scalability: Lambda automatically scales the execution environment based on the incoming workload. It can handle a single request or scale to thousands of requests per second, making it suitable for both small-scale and large-scale applications.
-
Pay-as-You-Go Pricing: AWS Lambda follows a pay-as-you-go pricing model, where you only pay for the compute time consumed during the execution of your code. There are no upfront fees or charges for idle time.
-
Statelessness: Lambda functions are stateless, meaning they do not retain information between executions. Any state information must be stored externally, such as in a database or a file storage service.
-
Custom Execution Roles: You can define execution roles for Lambda functions, granting them specific permissions to interact with other AWS services securely.
-
Monitoring and Logging: AWS provides tools like Amazon CloudWatch for monitoring and logging Lambda functions. You can set up alarms, track execution metrics, and troubleshoot any issues.
Architecture:
The instruction set architecture of a Lambda function determines the type of computer processor that Lambda uses to run the function. Lambda provides 2 types of instruction set architectures:
- Arm64: 64-bit ARM architecture, for the AWS Graviton2 processor.
- x86_64: 64-bit x86 architecture, for x86-based processors. X86_64 is the default architecture.
Functions that use arm64 architecture offer lower cost per Gb/s compared with the equivalent function running on an x86-based CPU.
Runtime:
A runtime is a version of a programming language or framework that you can use to write Lambda functions. Lambda supports runtime versions for Node.js, Python, Ruby, Go, Java, C# (.NET Core), and PowerShell (.NET Core).
To use other languages in Lambda, you can create your own runtime.
Advanced Options:
-
Code Signing: Use code signing configurations to enable code signing for a function. With code signing, you can ensure that the code has been signed by an approved source and has not been altered since signing, and that the code signature has not expired or been revoked.
-
Enable function URL: A function URL is a dedicated HTTP(S) endpoint for your function. When your function URL is configured, you can use it to invoke your function through a browser, curl, Postman, or any HTTP client.
-
Enable VPC: All Lambda functions run securely inside a default system-managed virtual private cloud (VPC). However, you can also configure your Lambda function to access resources in a custom VPC.
-
Enable Tags: Tags are optional key-value pairs that you can attach to your Lambda function. They can be used for various purposes such as function organization, cost allocation, access control.
Working:
Refer to the following steps to create a Lambda function.
-
Log into your AWS account via AWS Management Console (opens in a new tab).
-
In the AWS Management Console, search for "Lambda" in the services search bar or navigate to "Compute" and then select "Lambda."
-
Click on the “Create function” button.
-
There are two options, either a user can choose to create a function from scratch or use a blueprint/template. Select “Author from Scratch”.
-
Give the lambda function a name, there are a few options to choose the runtime environment. Select Python 3.11.
-
There are two different types of architecture set that can be used. Select “x86_64”.
-
Select the default execution role.
-
Select “Create function”.
In the code editor, use the provided code for the lambda function.
import json
def lambda_handler(event, context):
numbers = event['numbers']
a = numbers['number_1']
b = numbers['number_2']
# Add two numbers
c = a + b
# Print the result
print(c)
return {
'statusCode': 200,
'body': json.dumps(c)
}
Configure Test Event:
- Select the drop-down menu from Test field.
- Select Configure Test Event.
- Give the event a name and use the following code to configure a test event.
Test Event:
{
"numbers": {
"number_1": 10,
"number_2": 20
}
}
4. Select Save.
Executing Lambda Function:
- Select the test button.
- In the response section and function logs the sum of two numbers configured in the test event is visible.
Congratulations!! You have successfully explored the features of AWS Lambda and created a lambda function to add 2 numbers.