Skip to content

Lesson 5 of 9 · 24m

Cloud SDKs: boto3 and API Reality

How cloud APIs really behave: pagination, retries, throttling, eventual consistency, credentials, and defensive resource handling with boto3 as the example.

Cloud consoles train people to think infrastructure is a list of objects. Cloud APIs teach the truth: infrastructure is a distributed system with partial views, rate limits, eventual consistency, and errors that mean "try again, but slower." boto3 exposes that reality whether you acknowledge it or not.

Clients map to the API

import boto3

s3 = boto3.client('s3', region_name='us-east-1')
response = s3.list_objects_v2(Bucket='sudosec-backups', Prefix='db/')

boto3 also has higher-level resource objects. They are convenient but hide pagination, response metadata, waiter control, and precise error behavior. For automation, low-level clients are usually the honest abstraction.

Pagination is mandatory

List APIs return pages. Reading only the first response quietly drops resources:

paginator = s3.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket='sudosec-backups', Prefix='db/'):
    for obj in page.get('Contents', []):
        print(obj['Key'], obj['Size'])

A page can omit Contents entirely when empty; use .get().

Retries are not a correctness proof

boto3 retries some failures automatically. That makes read-only operations convenient, but mutations are different: the original request may have succeeded before the network failed. Safe repetition requires idempotency tokens, state checks, or reconciliation.

from botocore.config import Config

client = boto3.client(
    'ec2',
    config=Config(retries={'max_attempts': 8, 'mode': 'adaptive'}, connect_timeout=5, read_timeout=20),
)

A backup job that waits forever is not reliable; it is invisible until the disk fills. Put timeout and retry budgets in the script.

Errors and identity are operational data

Catch ClientError and inspect structured fields, not human strings:

except ClientError as exc:
    if exc.response['Error']['Code'] == '404':
        missing = True
    else:
        raise

Know which identity the ambient credential chain selected. Log sts.get_caller_identity()['Arn'] at startup and use least-privilege roles. When something goes wrong, "which identity did this run as?" is one of the first questions.

Field notes

  • Cloud APIs are eventually consistent; creation followed immediately by read can fail.
  • Permission failures can masquerade as absence depending on the service.
  • Explicitly set regions; inference differs across shells, containers, CI, and managed runtimes.

Was this lesson helpful?

Sign in to rate