Type Annotations for boto3 Resource Explorer 2

1.42.30 · active · verified Sat Apr 11

mypy-boto3-resource-explorer-2 provides type annotations for the `boto3` AWS SDK's Resource Explorer 2 service. It enhances development experience by enabling static type checking with tools like MyPy, catching potential errors at development time. The library is part of the `mypy-boto3` project, which generates stubs for all `boto3` services, released frequently in sync with `boto3` and `botocore` updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to obtain a type-hinted `ResourceExplorer2Client` using `boto3` and perform a search operation. It highlights the benefits of `mypy-boto3` by showing how type annotations guide parameter input and response parsing, improving code readability and maintainability.

import boto3
from mypy_boto3_resource_explorer_2.client import ResourceExplorer2Client
from mypy_boto3_resource_explorer_2.type_defs import SearchInputRequestTypeDef
import os

# Ensure boto3 is configured, e.g., via environment variables or ~/.aws/credentials
# For testing without actual credentials, you might mock boto3 or use a localstack setup.

def get_explorer_client() -> ResourceExplorer2Client:
    """Gets a type-hinted AWS Resource Explorer 2 client."""
    # boto3.client expects a string for the service name
    client: ResourceExplorer2Client = boto3.client("resource-explorer-2")
    return client


def search_resources(client: ResourceExplorer2Client, query: str):
    """Performs a search using the Resource Explorer 2 client."""
    print(f"Searching for resources with query: '{query}'")
    search_params: SearchInputRequestTypeDef = {
        "QueryString": query,
        "MaxResults": 5
    }
    response = client.search(**search_params)
    
    print("Found Resources:")
    for resource in response.get("Resources", []):
        print(f"  - Name: {resource.get('ResourceArn')}, Type: {resource.get('ResourceType')}")
    
    return response

if __name__ == "__main__":
    # Example usage
    explorer_client = get_explorer_client()
    
    # An example query (replace with a real query for your AWS account)
    # For a real run, ensure you have resources indexed by Resource Explorer 2.
    test_query = "region:us-east-1 and resource.type:ec2:instance"
    
    # This part will fail if AWS credentials are not configured or if Resource Explorer 2
    # is not set up in your AWS account/region.
    # For local testing, consider using localstack with boto3's endpoint_url.
    try:
        search_result = search_resources(explorer_client, test_query)
        print(f"Search result has a NextToken: {search_result.get('NextToken') is not None}")
    except Exception as e:
        print(f"Error during search (this might be expected if AWS config/resources are not set up): {e}")

view raw JSON →