Boto3でDynamoDBを操作してみました

概要

Python SDKのboto3を使って、DynamoDBの操作をしてみました。

テーブルの作成

# -*- coding: utf-8 -*-

import boto3
from boto3.session import Session

dynamodb = boto3.resource('dynamodb')

def create_table():
  table = dynamodb.create_table(
    TableName = 'customer',
    KeySchema =[
      {
        'AttributeName' : 'id',
        'KeyType' : 'HASH'
      },
      {
        'AttributeName' : 'name',
        'KeyType' : 'RANGE'

      }
    ],
    AttributeDefinitions = [
      {
        'AttributeName' : 'id',
        'AttributeType' : 'S'
      },
      {
        'AttributeName' : 'name',
        'AttributeType' : 'S'
      }
    ],

    ProvisionedThroughput = {
      'ReadCapacityUnits' : 1,
      'WriteCapacityUnits' : 1
    }
  )

  table.meta.client.get_waiter('table_exists').wait(TableName = 'customer')
  print (table.item_count)


create_table()

https://github.com/handa3/study/blob/master/aws/dynamodb/create_table.py

テーブルの削除

# -*- coding: utf-8 -*-

from __future__ import print_function # Python 2/3 compatibility
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('customer')

table.delete()

https://github.com/handa3/study/blob/master/aws/dynamodb/delete_table.py

さいごに

Sdkを使いこなせば出来ることがどんどん広がって楽しいです!
Pythonも好きなのでどんどん自動化出来たらいいなと思ってます。

ソースコードは本人の許可を得て、掲載しています。

コメントを残す

メールアドレスが公開されることはありません。

Time limit is exhausted. Please reload CAPTCHA.