Deploying APIs
- Possible options:
- Servers:
- Linux/Windows machine & deploy fastapi
- Containers:
- Build a docker image and deploy the application
- Cloud:
- PaaS Services
- Serverless: (Pay only for what you have executed)
- AWS:
- Lambda
- Azure:
- Functions
- GCP
- Functions
- AWS:
- Servers:
Serverless
- The approach to code the serverless is very much vendor specific
Problem
- Lets build a books api to create retrieve update delete books using Serverless
- Exercise: We have relational databases like mysql what’s need for nosql databases
AWS Lambda
- pricing
AWS Lambda Pricing Explained
AWS Lambda pricing is based on two main factors: the number of requests and the compute time (measured in GB-seconds) your functions consume. There is also a free tier, which allows for significant usage at no cost.
Key Pricing Components:
- Requests: You are charged based on the number of times your Lambda function is invoked.
- Compute Time: Charges are calculated based on the duration your code runs, combined with the amount of memory allocated, measured in GB-seconds.
- Ephemeral Storage: By default, Lambda provides 512 MB of ephemeral storage per function at no additional cost. Additional storage incurs a small charge.
- Processor Architecture: Pricing can vary slightly if you use x86 or ARM (Graviton2) architectures, with ARM generally being less expensive for compute time[3].
Free Tier
- 1 million free requests per month
- 400,000 GB-seconds of compute time per month
- 512 MB of ephemeral storage per function at no extra cost
This free tier is available to both new and existing AWS customers every month[1][2][3][4][5].
On-Demand Pricing (After Free Tier)
- Requests: $0.20 per 1 million requests
- Compute Time: $0.0000166667 per GB-second (for x86, US East region; ARM is slightly cheaper)
- Ephemeral Storage: $0.0000000309 per GB-second for storage above the free 512 MB[1][5]
- Tiered Compute Pricing: For very high usage, the price per GB-second decreases as you use more (e.g., after 6 billion GB-seconds per month)[3][4].
AWS Lambda Pricing Summary Table
| Component | Free Tier (per month) | On-Demand Price (after free tier) |
|---|---|---|
| Requests | 1 million requests | $0.20 per 1 million requests |
| Compute Time | 400,000 GB-seconds | $0.0000166667 per GB-second |
| Ephemeral Storage | 512 MB per function | $0.0000000309 per GB-second (above 512 MB) |
Notes:
– Compute time is calculated as: (function duration in seconds) × (memory allocated in GB).
– Prices may vary slightly by AWS region and processor architecture.
– Additional features like provisioned concurrency and data transfer may incur extra charges[1][5].
This pricing model allows you to pay only for what you use, and the free tier is generous enough for low-traffic or development workloads[2][3][4][5].
Citations:
[1] https://aws.amazon.com/lambda/pricing/
[2] https://cloudvisor.co/blog/is-aws-lambda-free-understanding-pricing/
[3] https://cloudchipr.com/blog/aws-lambda-pricing
[4] https://modal.com/blog/aws-lambda-price-article
[5] https://www.cloudzero.com/blog/lambda-pricing/
[6] https://aws.amazon.com/blogs/compute/aws-lambda-introduces-tiered-pricing-for-amazon-cloudwatch-logs-and-additional-logging-destinations/
[7] https://aws.amazon.com/free/
[8] https://aws.amazon.com/blogs/compute/aws-lambda-standardizes-billing-for-init-phase/
[9] https://www.reddit.com/r/aws/comments/1bpt3jw/aws_lambda_free_tier/
[10] https://aws.amazon.com/pm/lambda/
Lambda function
- Basic structure
import json
def lambda_handler(event, context):
print(event)
print(context)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
- event is the json input which we pass to lambda function which is of python type dict
- context is of the type awslambdaric.lambda_context.LambdaContext
- To interact with dynamo db Refer Here for boto3 sdk
- Watch the classroom video to create a Post/create books api
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('books')
try:
book_id = event.get('book_id')
print(f"Creating book with id {book_id}")
table.put_item(
Item=event
)
return {
'statusCode': 201,
'body': json.dumps(f"Book with id {book_id} created successfully")
}
except Exception as e:
print(f"Error creating book: {e}")
return {
'statusCode': 500,
'body': json.dumps({ 'error': str(e) })
}
- Exercise: Write Get books functionality
Azure Functions
@app.route(route="books_add")
def books_add(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
request_body = req.get_json()
container.create_item(request_body)
return func.HttpResponse(
json.dumps({"message": "Everything's fine"}),
status_code=200
)
except Exception as e:
return func.HttpResponse(
json.dumps({"message": "Somethings's wrong"}),
status_code=500
)
