これは何?
re:Invent2018でプロビジョニングが不要なDynamoDBのリクエストごとの支払いが可能になりました。
これに対応したCloudFormationのTemplateのサンプルです。
テンプレート
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
SampleTableBillingMode:
Type: String
Description: DynameDB Billing mode 'PROVISIONED' or 'PAY_PER_REQUEST'
AllowedPattern: "^(PAY_PER_REQUEST|PROVISIONED)$"
Default: PROVISIONED
SampleTableReadCU:
Type: String
Default: "1"
AllowedPattern: "^[0-9]+$"
Description: Ignore if BillingMode set 'PAY_PER_REQUEST'
SampleTableWriteCU:
Type: String
Default: "1"
AllowedPattern: "^[0-9]+$"
Description: Ignore if BillingMode set 'PAY_PER_REQUEST'
Conditions:
# SampleTableの請求モードがProvisionedCapacityかどうかのCondition
IsSampleTableProvisionedBilling: !Equals [!Ref SampleTableBillingMode, "PROVISIONED"]
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "SampleTable Configuration"
Parameters:
- SampleTableBillingMode
- SampleTableReadCU
- SampleTableWriteCU
ParameterLabels:
SampleTableBillingMode:
default: "BillingMode"
SampleTableReadCU:
default: "Read Capacity Units"
SampleTableWriteCU:
default: "Write Capacity Units"
Resources:
SampleTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "Key"
AttributeType: "S"
KeySchema:
- AttributeName: "Key"
KeyType: "HASH"
BillingMode: !Ref SampleTableBillingMode
# 請求モードがPROVISIONEDではない場合は、このプロパティは設定しない(NoValue)
ProvisionedThroughput:
Fn::If:
- IsSampleTableProvisionedBilling
- ReadCapacityUnits: !Ref SampleTableReadCU
WriteCapacityUnits: !Ref SampleTableWriteCU
- !Ref "AWS::NoValue"
何をしている?
AWS::DynamoDB::Table
に新しいプロパティBillingMode
が追加されています(2018-12-13現在、日本語のCloudFormationドキュメントには追加されていません)。
このプロパティはPAY_PER_REQUEST|PROVISIONED
のいずれかの値をとりますが、
PAY_PER_REQUEST
の場合はProvisionedThroughput
が設定されているとエラーになります。
そのため、Conditions
セクションで判定し、その値を使っています。
ProvisionedThroughput:
Fn::If:
- IsSampleTableProvisionedBilling
- ReadCapacityUnits: !Ref SampleTableReadCU
WriteCapacityUnits: !Ref SampleTableWriteCU
- !Ref "AWS::NoValue"
!Ref "AWS::NoValue"
により、ProvisionedThroughput
はIsSampleTableProvisionedBilling
が満たされていない場合は、そのプロパティ自身が設定されません。
Metadata
セクションは、Management Console上でStack Parametersを見やすく表示するために使っているだけなので、無くても構いません。
このような感じに表示されます。
これを使ってStackを作成すると、どちらの請求モードのテーブルでも作ることができます。また、請求モードをStack Parameterから変更することも可能ですが、テーブルの利用状況によっては、制限に引っかかる可能性があるので注意してください。