4.3.3 Determine Minimum Memory and Compute

Determine Minimum Memory and Compute Power

Lambda Memory & CPU

  • Memory: 128MB → 10,240MB
  • CPU scales linearly: 1,769MB = 1 vCPU
  • Tăng memory có thể giảm duration → giảm cost

Optimization Strategy

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

Code Optimization

# 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']})

Connection Reuse

from botocore.config import Config
config = Config(tcp_keepalive=True, max_pool_connections=25)
client = boto3.client('dynamodb', config=config)

/tmp Storage

  • 512MB → 10,240MB
  • Persists in warm container
  • Cache files between invocations

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.