1. Start with default (128MB or 256MB)
2. Run Lambda Power Tuning
3. Check REPORT line: Max Memory Used
4. Set memory = Max Memory Used + 20% buffer
5. Verify duration acceptable
# GOOD: Init outside handler
import boto3
table = boto3.resource('dynamodb').Table('Orders')
def handler(event, context):
return table.get_item(Key={'id': event['id']})
# BAD: Init inside handler
def handler(event, context):
table = boto3.resource('dynamodb').Table('Orders') # Every time!
return table.get_item(Key={'id': event['id']})
from botocore.config import Config
config = Config(tcp_keepalive=True, max_pool_connections=25)
client = boto3.client('dynamodb', config=config)
Exam Tip: Tăng memory = tăng CPU = có thể giảm duration = có thể giảm cost. Init code ngoài handler. Lambda Power Tuning cho optimal setting.