AWS Lambda - Practicing
Let's create our own function in AWS Lambda.
Create Folder
Question 1
Create .env
Question 2
Create function
Question 3
Create ZIP
Question 4
Show functions
Let's use this code to list the functions available in our account:
import boto3
import os
from dotenv import load_dotenv
load_dotenv()
# Create a Boto3 client for AWS Lambda
lambda_client = boto3.client(
"lambda",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
region_name=os.getenv("AWS_REGION"),
)
# Call the list_functions API to retrieve all Lambda functions
response = lambda_client.list_functions(MaxItems=1000)
# Extract the list of Lambda functions from the response
functions = response["Functions"]
print(f"You have {len(functions)} Lambda functions")
# Print the name of each Lambda function
if len(functions) > 0:
print("Here are their names:")
for function in functions:
function_name = function["FunctionName"]
print(function_name)
Question 5
Create Lambda Function
Let's create a lambda function with:
Atention!
Change the function_name
variable.
Provide a name in the pattern sayHello_<YOUR_INSPER_USERNAME>
import boto3
import os
from dotenv import load_dotenv
load_dotenv()
# Provide function name: sayHello_<YOUR_INSPER_USERNAME>
function_name = ""
# Create a Boto3 client for AWS Lambda
lambda_client = boto3.client(
"lambda",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
region_name=os.getenv("AWS_REGION"),
)
# Lambda basic execution role
lambda_role_arn = os.getenv("AWS_LAMBDA_ROLE_ARN")
# Read the contents of the zip file that you want to deploy
# Inside the "my_lambda.zip" there is a "my_lambda.py" file with the
# "say_hello" function code
with open("my_lambda.zip", "rb") as f:
zip_to_deploy = f.read()
lambda_response = lambda_client.create_function(
FunctionName=function_name,
Runtime="python3.10",
Role=lambda_role_arn,
Handler="my_lambda.say_hello", # Python file DOT handler function
Code={"ZipFile": zip_to_deploy},
)
print("Function ARN:", lambda_response["FunctionArn"])
Question 6
Question 7
Check if worked
Let's check if the function worked:
Atention!
Change the function_name
variable.
import boto3
import os
import io
from dotenv import load_dotenv
load_dotenv()
# Create a Boto3 client for AWS Lambda
lambda_client = boto3.client(
"lambda",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
region_name=os.getenv("AWS_REGION"),
)
# Lambda function name
# Provide function name: sayHello_<YOUR_INSPER_USERNAME>
function_name = ""
try:
# Invoke the function
response = lambda_client.invoke(
FunctionName=function_name,
InvocationType="RequestResponse",
)
payload = response["Payload"]
txt = io.BytesIO(payload.read()).read().decode("utf-8")
print(f"Response:\n{txt}")
except Exception as e:
print(e)
Question 8
This code provides a way to check if the lambda function works. However, when integrating it with other applications, its execution would probably depend on an event such as the creation of a file on S3 or an API call.
Let's check out how to use API Gateway to create an API for our Lambda function.