---
# System prepended metadata

title: Query 267

---

Calculating the cost of an AWS Lambda function based on the number of requests and execution duration can be a bit complex, as AWS Lambda pricing involves a combination of factors such as requests, execution duration, and memory configuration. It's also important to note that AWS Lambda has a free tier that includes a certain number of free requests and execution duration per month.

Here's a Python script to calculate the cost of an AWS Lambda function based on the number of requests, execution duration, and memory configuration. The script takes into account the AWS Lambda pricing model and the free tier options for requests and execution duration:
```
def calculate_lambda_cost(requests, execution_duration, memory_size_mb):
    # AWS Lambda pricing parameters
    free_requests_per_month = 400000
    free_execution_duration_per_month = 400000
    execution_cost_per_gb_sec = 0.00001667  # $0.00001667 per GB-second
    request_cost_per_million = 0.20  # $0.20 per 1 million requests

    # Calculate free tier usage
    free_requests_used = min(requests, free_requests_per_month)
    free_execution_duration_used = min(execution_duration, free_execution_duration_per_month)

    # Calculate the cost of additional requests and execution duration
    additional_requests = requests - free_requests_used
    additional_execution_duration = execution_duration - free_execution_duration_used

    # Calculate the cost
    request_cost = (additional_requests / 1000000) * request_cost_per_million
    execution_cost = (additional_execution_duration * (memory_size_mb / 1024)) * execution_cost_per_gb_sec

    total_cost = request_cost + execution_cost
    return total_cost

# Example usage:
requests = 500000
execution_duration = 5000000  # in milliseconds
memory_size_mb = 256  # in MB

cost = calculate_lambda_cost(requests, execution_duration, memory_size_mb)
print(f"The estimated cost for the Lambda function is ${cost:.2f}")
```
This script calculates the cost by considering the free tier options and pricing details for requests and execution duration. It assumes a specific execution cost per GB-second and request cost per million requests based on AWS Lambda's pricing model. You can adjust the values as AWS Lambda pricing changes over time.
Please note that this script provides an estimation and may not cover all nuances in AWS Lambda pricing, especially for complex workloads with varying execution durations and resource configurations. To get the most accurate cost estimate, consider using AWS's Cost Explorer or AWS Cost and Usage Reports, which provide detailed billing information.