How to create a Lambda function in AWS using Python?
The growing Amazon Web Services (AWS) ecosystem presents itself as a powerful solution for developers looking to streamline processes and automate tasks. An essential component of this ecosystem is AWS Lambda, which allows you to run code without the need to manage servers, thanks to the serverless approach. In this tutorial, we will explore how to create a Lambda function using Python, from the AWS console.
What is AWS Lambda?
AWS Lambda is a compute service that allows you to run code without the need to provision or manage servers. It works through the on-demand execution model, allowing developers to upload and run code only when needed, making Lambda a very efficient and cost-effective option for a variety of applications.
How to get started with AWS Lambda?
-
Access the AWS console: Go to the AWS console and make sure you are in the correct section:
- Go to "All Services".
- Find and select "Lambda" under the "Compute" category.
-
Create a new Lambda function:
- Select "Create a function" and choose "From scratch".
- Assign a name to your function, for example,
latsifond
.
- Choose Python 3.6 as the execution language, although AWS Lambda supports several languages such as C, Java, and Go.
-
Configure the execution role: Define a role that allows your Lambda function to execute safely. This configuration is crucial to ensure that your function has the necessary permissions to interact with other AWS services.
How to write and deploy the code of a Lambda function in AWS?
Developing a function in AWS Lambda involves defining a controller function, known as lambda_handler
, which takes two arguments: event
and context
.
def lambda_handler(event, context): what_to_print = event['what_to_print'] how_many_times = event['how_many_times']
if how_many_times > 0: for _ in range(how_many_times): print(what_to_print)
what_to_print
and how_many_times
: These are environment variables that determine what is printed and how many times.
- The code checks a simple condition and executes actions according to the parameters received.
How to configure environment variables and additional parameters?
-
Environment variables: In AWS Lambda, you can set environment variables that will be accessible to your function without coding them directly. Example:
what_to_print
= "Hello from Platzi".
how_many_times
= 6
-
Configure memory and concurrency:
- Memory: you can set the RAM memory dedicated to your function, which defaults to 128 MB and can be expanded up to 3 GB. AWS Lambda automatically adjusts memory based on historical usage.
- Concurrency: AWS Lambda allows up to 1000 concurrent executions by default. If you need more, you can contact AWS support to discuss your needs.
How to test and validate Lambda functions?
Once your function is configured, it is vital to make sure it works correctly by creating test events and executing the function. After execution, AWS Lambda provides detailed performance information, including execution time and memory used.
-
Create a test event:
- Create a new test event, name it appropriately, such as
hello
.
- Execute the event to verify the output and performance of your function.
-
Evaluate the results: Be sure to check the top area of the console for execution results, which indicate duration, memory usage, and other vital metrics.
Additional tips and troubleshooting.
- Attention to detail: Run results are often displayed at the top in the console, which can be confusing at first. Scroll up to confirm that the results are displayed correctly.
- Optimization: Observe resource usage and adjust the configuration according to your actual needs.
- Feedback and support: If you have questions, contact AWS Support for guidance and adjust settings based on feedback received.
AWS Lambda and Python provide a powerful combination to automate processes and build efficient applications in the cloud. This tutorial is just a start; experiment and adapt these tools to meet your development goals.
Want to see more contributions, questions and answers from the community?