Files
awslings/exercises/04_lambda/lambda_2_init_reuse.py
T
nessarandClaude Opus 5.5 c571c3a5de Type the boto3 clients and DynamoDB tables in exercises and solutions
Annotate client and resource parameters with boto3-stubs types (S3Client,
SQSClient, Table, ...) so VS Code autocompletes methods and arguments,
as in iam_3. Finished exercises (intro_1-2, iam_1-3) are left as the
learner wrote them. Tests sections are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 20:57:24 +01:00

75 lines
2.4 KiB
Python

"""
Exercise lambda_2 — Reuse the execution environment
===================================================
Book: chapter 5, "Execution environment lifecycle" (book/src/ch05-lambda.md)
Exam: Domain 1 · Task 1.2 / Domain 4 · Task 4.3 (tuning Lambda functions for optimal performance)
Lambda runs your code in an execution environment. On a cold start it
loads your module (the INIT phase), then calls the handler. Following
invocations routed to the same warm environment only call the handler.
So:
* create SDK clients, database connections and caches OUTSIDE the
handler (module scope / lazily cached), so warm invocations reuse them,
* never keep request-specific data in globals (another request will see it),
* `/tmp` (512 MB to 10 GB) also survives between warm invocations.
The current code creates a new DynamoDB Table object on every call.
Your task
---------
Implement `get_table()` so that the table object is created at most once
per execution environment, and use it in the handler.
"""
# I AM NOT DONE
import os
from mypy_boto3_dynamodb.service_resource import Table
import boto3
TABLE_NAME = os.environ.get("TABLE_NAME", "visits")
TABLE_FACTORY = boto3.resource("dynamodb", region_name="eu-west-3").Table
_table = None
def get_table() -> Table:
return TABLE_FACTORY(TABLE_NAME) # TODO: create once, then reuse
def handler(event, context):
table = get_table()
table.put_item(Item={"pk": event["visitor"]})
return {"ok": True}
# =============================================================================
# Tests — do not edit below this line. Run them with: awslings run lambda_2
# =============================================================================
import sys # noqa: E402
class FakeTable:
def __init__(self, name):
self.name, self.items = name, []
def put_item(self, Item):
self.items.append(Item)
def test_table_created_once(monkeypatch):
module = sys.modules[__name__]
created = []
def factory(name):
created.append(name)
return FakeTable(name)
monkeypatch.setattr(module, "TABLE_FACTORY", factory)
monkeypatch.setattr(module, "_table", None)
for visitor in ("ada", "grace", "linus"):
assert handler({"visitor": visitor}, None) == {"ok": True}
assert created == ["visits"], "the table object must be created only once (cold start), then reused"
assert [item["pk"] for item in get_table().items] == ["ada", "grace", "linus"]