-
Notifications
You must be signed in to change notification settings - Fork 7
/
cloudwatch_dashboard_mock.py
64 lines (60 loc) · 1.92 KB
/
cloudwatch_dashboard_mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import boto3
import random
import time
import decimal
from botocore.exceptions import ClientError
dynamodb = boto3.resource('dynamodb', region_name='eu-central-1')
table_name = 'ShoppingData'
# Create DynamoDB table if it does not exist
def create_table():
try:
table = dynamodb.create_table(
TableName=table_name,
KeySchema=[
{
'AttributeName': 'order_id',
'KeyType': 'HASH'
},
],
AttributeDefinitions=[
{
'AttributeName': 'order_id',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
print(f"Table {table_name} created successfully.")
table.wait_until_exists()
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceInUseException':
print(f"Table {table_name} already exists.")
else:
print("Unexpected error: %s" % e)
def put_random_shopping_data():
table = dynamodb.Table(table_name)
counter = 0
while True:
order_id = f"order_{random.randint(1, 10000)}"
item_name = f"item_{random.randint(1, 100)}"
quantity = random.randint(1, 20)
price = decimal.Decimal(str(round(random.uniform(1, 1000), 2)))
response = table.put_item(
Item={
'order_id': order_id,
'item_name': item_name,
'quantity': quantity,
'price': price
}
)
print(f"PutItem succeeded: {order_id}, {item_name}, {quantity}, {price}")
counter += 1
if counter % 100 == 0:
print("Sleeping for 1 minute...")
time.sleep(60)
if __name__ == '__main__':
create_table()
put_random_shopping_data()