When using DynamoDB when creating python numbering process If you want to use actual DynamoDB during development ... I think that you will develop using DynamoDB Local, so I will summarize the method. Continuing from Last time, we will actually create a python program.
sequence_key
is fixed, but when actually using it, it is better to receive it as an argument.import os
import boto3
endpoint_url = os.getenv('DYNAMODB_ENDPOINT', 'http://192.168.99.100:8000/')
aws_access_key_id = os.getenv('DYNAMODB_ACCESS_KEY_ID', 'DummyID')
aws_secret_access_key_id = os.getenv('DYNAMODB_SECRET_ACCESS_KEY_ID', 'DummyKey')
aws_region_name = os.getenv('DYNAMODB_REGION', 'ap-northeast-1')
dynamodb = boto3.resource(
'dynamodb',
region_name=aws_region_name,
endpoint_url=endpoint_url,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key_id
)
sequences = dynamodb.Table('sequences')
res = sequences.update_item(
Key= {
'sequence_key': 'TEST_SEQ'
},
UpdateExpression="ADD #name :increment",
ExpressionAttributeNames={
'#name':'val'
},
ExpressionAttributeValues={
":increment": int(1)
},
ReturnValues="UPDATED_NEW"
)
count=res['Attributes']['val']
print(count)
Recommended Posts