Interview Prep/Python for DevOps/boto3 & AWS automation

How do you type-check boto3 code?

AdvancedConceptboto3 & AWS automation
Answer

boto3 builds clients dynamically, so out of the box everything is Any. Install boto3-stubs[ec2,s3,...] (mypy-boto3), and annotate:

from mypy_boto3_ec2 import EC2Client
from mypy_boto3_ec2.type_defs import InstanceTypeDef

def running(ec2: EC2Client) -> list[InstanceTypeDef]: ...

Now mypy and the IDE know every method, parameter and response key, catching typos like ec2.describe_instance() or inst["InstanceID"] before runtime. Keep the stubs in dev dependencies only.

All Python for DevOps questions Roadmap: Python for automation

More on boto3 & AWS automation