Skip to content
This repository has been archived by the owner on Jul 26, 2020. It is now read-only.

Latest commit

 

History

History
96 lines (76 loc) · 3.24 KB

boto3.org

File metadata and controls

96 lines (76 loc) · 3.24 KB

boto3

Table of Contents

Concepts

FeatureDescription
Resourcesa high level, object oriented interface
Collectionsa tool to iterate and manipulate groups of resources
Clientslow level service connections
Paginatorsautomatic paging of responses
Waitersa way to block until a certain state has been reached

Boto 3 is built atop of a library called Botocore, which is shared by the AWS CLI.

It is recommended to create a resource instance for each thread in a multithreaded application rather than sharing a single instance among the threads

Client vs Resource

import boto3
# Create a low-level client with the service name
sqs = boto3.client('sqs')# Create the resource

# Access client through resource
sqs_resource = boto3.resource('sqs')
sqs = sqs_resource.meta.client

Paginators

import boto3

client = boto3.client('s3', region_name='us-west-2')
paginator = client.get_paginator('list_objects')
operation_parameters = {'Bucket': 'my-bucket',
                        'Prefix': 'foo/baz'}
page_iterator = paginator.paginate(**operation_parameters)
for page in page_iterator:
    print(page['Contents'])

ec2

# EC2 find instances
ec2 = boto3.resource('ec2')
base = ec2.instances.filter(InstanceIds=['id1', 'id2', 'id3'])

filters = [{
    'name': 'tenancy',
    'value': 'dedicated'
}]
filtered1 = base.filter(Filters=filters)

# Note, this does NOT modify the filters in ``filtered1``!
filters.append({'name': 'instance-type', 'value': 't1.micro'})
filtered2 = base.filter(Filters=filters)

print('All instances:')
for instance in base:
    print(instance.id)

print('Dedicated instances:')
for instance in filtered1:
    print(instance.id)

print('Dedicated micro instances:')
for instance in filtered2:
    print(instance.id)
import boto3

ec2 = boto3.resource('ec2')
instance = ec2.Instance('id')

print instance.private_ip_address  # For more attributes, SEE: EC2.Instance

References