AWS Lambda is a serverless computing service that allows you to execute code without the need to provision or manage servers. With Lambda functions, you can run code for various types of applications or backend services. You only pay for the computing time consumed, with no charges when the function is not running.
1. Main Features of AWS Lambda
a) Serverless Code Execution
AWS Lambda eliminates the need to manage server infrastructure. You simply upload your code and the Lambda function handles everything else, including scalability, availability, and maintenance.
b) Automatic Scaling
Lambda automatically scales your application by executing code in response to each triggered event, from a few requests per day to thousands per second, according to demand. See article: [docs.aws.amazon.com/lambda/latest/dg/config..
c) Multiple Language Support
Supports various programming languages, including Node.js, Python, Java, C#, Go, and Ruby. It also allows you to bring your own runtime using Lambda Layers functionality.
d) Native AWS Service Integration
Lambda integrates easily with other AWS services, such as Amazon S3, DynamoDB, Kinesis, SNS, SQS, API Gateway, and many others, allowing you to build complex applications simply.
e) Usage-Based Cost Model
You only pay for the computing time consumed while your code is running, with billing in millisecond increments, making it an economical option for many use cases. See source at [aws.amazon.com/lambda/pricing/]
2. Fundamental Concepts
a) Lambda Functions
A Lambda Function is a deployable code unit that AWS Lambda executes in response to events. Each function has associated code and configurations, such as runtime environment, allocated memory, and environment variables. It's important to note that we don't directly manage the dedicated CPU for that function.
b) Events
Events are triggers that start the execution of a Lambda function. They can be generated by other AWS services, custom applications, or called directly via API or SDK. In English, triggers, are the starting point for executing our code via lambda function.
c) Layers (Layers)
Layers allow managing shared code and data between multiple Lambda functions. This facilitates reusing libraries, dependencies, and common configurations. This is a very important feature that can optimize function performance. Read here an article about.
d) Version Sets and Aliases
Each Lambda function can have multiple Versions, allowing for different deployments of the code. Aliases are pointers to specific versions, facilitating management of environments like development, testing, and production.
e) Runtime Environment
The Runtime Environment specifies the programming language and version that the Lambda uses to execute its code, such as Python 3.8, Node.js 14.x, Java 11, etc.
3. First Steps with AWS Lambda
a) Prerequisites
Access to the AWS Management Console.
Basic knowledge of one of the supported languages.
b) Step 1: Create a Lambda Function
i. Using the AWS Console
Log in to the AWS Management Console.
Search for Lambda in the search bar and select the service.
Click on Create function (Create function).
ii. Choose Creation Method
- Select Author from scratch (Create from zero).
iii. Configure Function Details
Function name (Function name): Enter a name, for example, myTestFunction.
Runtime: Choose the programming language, such as Python 3.8.
- Permissions (Permissions): The Lambda will create a new execution function with basic permissions by default. You can modify the permissions as needed.
iv. Create the Function
- Click on Create function (Create function).
c) Step 2: Write the Function Code
On the function editing page, you will see an integrated code editor.
Enter the desired code. Let's go for our Hello World in Python:
def lambda_handler(event, context):
message = 'Hello World!'
print(message)
return {
'statusCode': 200,
'body': message
}
- Click on Deploy (Deploy) to save the changes.
d) Step 3: Test the Function
Click on Test (Test).
Configure a test event:
Event name (Event name):
teste-evento
.Leave the default JSON event or customize as needed.
Click on Create (Create).
Click again on Test to execute the function.
- Check the results in the Execution result (Execution result) and logs in the Log output (Log output).
e) Step 4: Configure a Trigger (Trigger)
On the Configuration (Configuration) tab, select Triggers.
Click on Add trigger (Add trigger).
Choose a service to trigger the function, such as API Gateway, S3 or EventBridge.
Configure the trigger as needed.
Click on Add (Add).
Here we added a rule in EventBridge to trigger this lambda every 10 minutes.
We get this result:
Analyzing the logs in CloudWatch, we can see the execution every 10 minutes, exactly as we configured:
Each log stream containing the following flow:
4. Final Considerations
AWS Lambda revolutionized the way applications are built and executed in the cloud, eliminating the need to manage servers and allowing full focus on code and business logic. With its ability to integrate with various AWS services and support multiple use cases, Lambda is an essential tool for modern developers.