【Python】CDKでオンデマンドなDynamoDBのテーブルを作る
もくじ
赤ちゃんとともに暮らし初めてから、初の投稿です 👍
以前作成した記事の記事をベースに cdk で作った DynamoDB のテーブルをオンデマンド設定にしよう。という記事です。
何がしたいの?
cdk で DynamoDB の Table の料金設定をオンデマンドに設定する
前提
cdk のシリーズの続きです 👍
では 作っていきましょう 👻
と言ってもオンデマンドに設定するには、テーブルの支払いモードを従量課金に設定するだけです 😊
具体的なコードはこんな感じです
from aws_cdk import core
from aws_cdk.aws_dynamodb import Attribute, AttributeType, BillingMode, Table
class DynamoTableStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# DynamoDB
table = Table(
self,
"DynamoTableTable",
partition_key=Attribute(name="pk", type=AttributeType.NUMBER),
sort_key=Attribute(name="sk", type=AttributeType.STRING),
billing_mode=BillingMode.PAY_PER_REQUEST, # オンデマンドに設定
removal_policy=core.RemovalPolicy.DESTROY,
)
簡単ですね 😅
デプロイ 🚀
$ cdk deploy dynamo-table-stack
動作確認
まず、デプロイした dynamo の名前を取得します。
$ aws dynamodb list-tables | grep dynamo-table-stack
今回はこんな名前でした 👍
dynamo-table-stack-DynamoTableTable529948A5-HIKJJVBF9FUR
ブログの可読性向上のために変数に入れときます。
$ TABLE_NAME=dynamo-table-stack-DynamoTableTable529948A5-HIKJJVBF9FUR
テーブルの詳細情報を取得します。
$ aws dynamodb describe-table --table-name $TABLE_NAME
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "pk",
"AttributeType": "N"
},
{
"AttributeName": "sk",
"AttributeType": "S"
}
],
"TableName": "dynamo-table-stack-DynamoTableTable529948A5-HIKJJVBF9FUR",
"KeySchema": [
{
"AttributeName": "pk",
"KeyType": "HASH"
},
{
"AttributeName": "sk",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": xxxxxxxxxx.xxx,
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:xxxxxxxxxxxxxx:xxxxxxxxxxxx:table/dynamo-table-stack-DynamoTableTable529948A5-HIKJJVBF9FUR",
"TableId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"BillingModeSummary": {
"BillingMode": "PAY_PER_REQUEST",
"LastUpdateToPayPerRequestDateTime": xxxxxxxxxx.xxx
}
}
}
はい、"BillingMode": "PAY_PER_REQUEST"
になっていることが確認できました 👍
Dynamon のコンソールからも確認できます(↓)
デストロイ 🎉
$ cdk destroy dynamo-table-stack