DynamoDB Boto3 Example

I spent most of yesterday banging my head against the wall trying to figure out DynamoDB receiving a lot of ParameterErrors and ValidationErrors on my values when Putting new items into a DynamoDB table. Mainly because I failed to notice some key lines of code in the documentation for Boto3. Below I’m going to show the very basics of using Boto3 to put a new item into a table to hopefully help someone somewhere quick start using DynamoDB.

Initialize a DynamoDB Object

def init_dynamo(region='us-east-1'):
    Setup dynamo for writing
    perf data
    user_id = current_user.id
    amazon_creds = AmazonAccount.query.filter_by(account_id=user_id).first()
    if amazon_creds:
        creds = amazon_creds.get_creds()
        if not creds['region']:
            creds['region'] = region
        session = Session(
        if session:
            dynamo = session.resource('dynamodb')
            return dynamo

The above code is used to get the credentials from a database and use those to establish a session that then is used to initialize a dynamo object via boto3.

Receiving JSON and Writing it to DynamoDB

class ProcessPerf(Resource):
    Process data from nagios

    def post(self): # This is how FlaskRestful Handles HTTP Methods
        send data to dynamodb
        data = request.get_json(force=True) # Receive JSON
        dynamo = init_dynamo() # Initialize Session From Above Snippet
        table = dynamo.Table('my_perf_data') # Specify which DynamoDB table we are working with (in this case my_perf_data)
        response = table.put_item( # Call the put_item method on the table object
            Item={ # Specify this is an item
                'hash_key': data.get('hostname'), # This is required and used for querying
                'timestamp': data.get('timestamp'), # This is also required (in my case b/c this is my sort key)
                'service': data.get('disp_servicedesc'), # Other data
                'value': data.get('value'), # Other data
        return True

I commented each step above to ensure the most readability of what is going on. I hope this saves someone a little time when first diving into DynamoDB with boto3.

For more information please see the official documentation at:

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.