# AWS DynamoDB

DynamoDB tables, items and queries through both the resource API and the low-level client.

Zato lets you work with Amazon DynamoDB directly from your Python services. You create an [AWS connection](https://zato.io/docs/dev/examples/cloud/aws/index.html) in the Dashboard, and DynamoDB is available both through the low-level client under `conn.dynamodb` and through the higher-level resource API under `conn.resource('dynamodb')`.

## The resource API {#the-resource-api}

For most day-to-day work, the resource API is the more convenient of the two - it accepts and returns plain Python values instead of DynamoDB's typed attribute format.

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class SaveCustomer(Service):

    input = 'customer_id', 'name'

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.aws['My AWS']

        # Get a handle to the table ..
        dynamodb = conn.resource('dynamodb')
        table = dynamodb.Table('customers')

        # .. store the item ..
        table.put_item(Item={
            'customer_id': self.request.input.customer_id,
            'name': self.request.input.name,
        })

        # .. and read it back.
        response = table.get_item(Key={'customer_id': self.request.input.customer_id})
        self.response.payload = response['Item']
```

## The low-level client {#the-low-level-client}

The low-level client under `conn.dynamodb` maps one-to-one to the DynamoDB wire API, with explicit attribute types - `S` for strings, `N` for numbers, and so on.

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class GetCustomer(Service):

    input = 'customer_id'

    def handle(self):

        conn = self.aws['My AWS']

        response = conn.dynamodb.get_item(
            TableName='customers',
            Key={'customer_id': {'S': self.request.input.customer_id}},
        )
        self.response.payload = response['Item']
```

## Querying {#querying}

```python
# -*- coding: utf-8 -*-

# boto3
from boto3.dynamodb.conditions import Key

# Zato
from zato.server.service import Service

class GetCustomerOrders(Service):

    input = 'customer_id'

    def handle(self):

        conn = self.aws['My AWS']

        dynamodb = conn.resource('dynamodb')
        table = dynamodb.Table('orders')

        # All the orders of one customer, using the table's partition key
        response = table.query(
            KeyConditionExpression=Key('customer_id').eq(self.request.input.customer_id),
        )

        self.response.payload = {'orders': response['Items']}
```

## Creating tables {#creating-tables}

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class CreateCustomersTable(Service):

    def handle(self):

        conn = self.aws['My AWS']

        conn.dynamodb.create_table(
            TableName='customers',
            KeySchema=[{'AttributeName': 'customer_id', 'KeyType': 'HASH'}],
            AttributeDefinitions=[{'AttributeName': 'customer_id', 'AttributeType': 'S'}],
            BillingMode='PAY_PER_REQUEST',
        )
```

## More resources {#more-resources}

- [AWS integrations overview](https://zato.io/docs/dev/examples/cloud/aws/index.html)
- [AWS tutorial](https://zato.io/docs/dev/examples/cloud/aws/tutorial.html)
- [boto3 DynamoDB reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html)

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
