| 1 |
***************************** |
| 2 |
Getting Started With botocore |
| 3 |
***************************** |
| 4 |
|
| 5 |
|
| 6 |
The ``botocore`` package provides a low-level interface to Amazon |
| 7 |
services. It is responsible for: |
| 8 |
|
| 9 |
* Providing access to all available services |
| 10 |
* Providing access to all operations within a service |
| 11 |
* Marshaling all parameters for a particular operation in the correct format |
| 12 |
* Signing the request with the correct authentication signature |
| 13 |
* Receiving the response and returning the data in native Python data structures |
| 14 |
|
| 15 |
``botocore`` does not provide higher-level abstractions on top of these |
| 16 |
services, operations and responses. That is left to the application |
| 17 |
layer. The goal of ``botocore`` is to handle all of the low-level details |
| 18 |
of making requests and getting results from a service. |
| 19 |
|
| 20 |
The ``botocore`` package is mainly data-driven. Each service has a JSON |
| 21 |
description which specifies all of the operations the service supports, |
| 22 |
all of the parameters the operation accepts, all of the documentation |
| 23 |
related to the service, information about supported regions and endpoints, etc. |
| 24 |
Because this data can be updated quickly based on the canonical description |
| 25 |
of these services, it's much easier to keep ``botocore`` current. |
| 26 |
|
| 27 |
Using Botocore |
| 28 |
============== |
| 29 |
|
| 30 |
The first step in using botocore is to create a ``Session`` object. |
| 31 |
``Session`` objects then allow you to create individual clients:: |
| 32 |
|
| 33 |
import botocore.session |
| 34 |
session = botocore.session.get_session() |
| 35 |
client = session.create_client('ec2', region_name='us-west-2') |
| 36 |
|
| 37 |
Once you have that client created, each operation provided by the service is |
| 38 |
mapped to a method. Each method takes ``**kwargs`` that maps to the parameter |
| 39 |
names exposed by the service. For example, using the ``client`` object created |
| 40 |
above:: |
| 41 |
|
| 42 |
for reservation in client.describe_instances()['Reservations']: |
| 43 |
for instance in reservation['Instances']: |
| 44 |
print(instance['InstanceId']) |
| 45 |
|
| 46 |
# All instances that are in a state of pending. |
| 47 |
reservations = client.describe_instances( |
| 48 |
Filters=[{"Name": "instance-state-name", "Values": ["pending"]}]) |