Skip to content

Instantly share code, notes, and snippets.

@dmukhg
Created May 10, 2022 14:21
Show Gist options
  • Save dmukhg/ecbae774b296b24afbc63e934d554d6e to your computer and use it in GitHub Desktop.
Save dmukhg/ecbae774b296b24afbc63e934d554d6e to your computer and use it in GitHub Desktop.
import boto3
from moto import mock_dynamodb2
# Requires having created a table called "test-abuse" with only
# a partition key called 'Key'
item = {
"Key": "some-irrelevant_key",
"SetTypeAttribute": {"SS": set([])},
}
def write_item(table):
table.put_item(Item=item)
def write_via_boto():
session = boto3.Session()
dynamodb = session.resource("dynamodb", region_name="us-east-1")
table = dynamodb.Table("test-abuse")
write_item(table=table)
@mock_dynamodb2
def write_via_moto():
schema = {
"TableName": "test-abuse",
"KeySchema": [{"AttributeName": "Key", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "Key", "AttributeType": "S"}],
"ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
}
conn = boto3.client("dynamodb", region_name="us-west-2")
conn.create_table(**schema)
session = boto3.Session()
dynamodb = session.resource("dynamodb", region_name="us-west-2")
table = dynamodb.Table("test-abuse")
write_item(table=table)
if __name__ == "__main__":
print("Writing to real dynamodb")
try:
write_via_boto()
print("Succeeded writing to real dynamodb")
except:
print("Failed writing to real dynamodb")
import traceback
traceback.print_exc()
print("Writing to mock dynamodb")
try:
write_via_moto()
print("Succeeded writing to mock dynamodb")
except:
print("Failed writing to mock dynamodb")
import traceback
traceback.print_exc()
write_via_moto()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment