LoginSignup
3
2

More than 5 years have passed since last update.

CloudFormationでDynamoDBのBillingModeを切り替えて作成する

Last updated at Posted at 2018-12-12

これは何?

re:Invent2018でプロビジョニングが不要なDynamoDBのリクエストごとの支払いが可能になりました。

AWS Blog(日本語)

これに対応した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"により、ProvisionedThroughputIsSampleTableProvisionedBillingが満たされていない場合は、そのプロパティ自身が設定されません。

Metadataセクションは、Management Console上でStack Parametersを見やすく表示するために使っているだけなので、無くても構いません。

image.png

このような感じに表示されます。

これを使ってStackを作成すると、どちらの請求モードのテーブルでも作ることができます。また、請求モードをStack Parameterから変更することも可能ですが、テーブルの利用状況によっては、制限に引っかかる可能性があるので注意してください。

プロビジョニングされるスループットを減らす

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2