AWS CDK で DynamoDB をデプロイする方法

はじめに

AWS CDK で DynamoDB をデプロイする方法を記載する。

事前準備

AWS CDK のインストールはこちらを参照する。

コード

  • index.ts
#!/usr/bin/env node
import cdk = require('@aws-cdk/cdk');
import dynamodb = require('@aws-cdk/aws-dynamodb');

export class CdkStackIam extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    
    new dynamodb.CfnTable(this, 'TableMultiIndex', {
      attributeDefinitions: [
        {
          attributeName: "col1",
          attributeType: "S"
        },{
          attributeName: "col2",
          attributeType: "S"
        },{
          attributeName: "col3",
          attributeType: "N"
        },{
          attributeName: "col4",
          attributeType: "S"
        }
      ],
      keySchema: [
        {
          attributeName: "col1",
          keyType: "HASH"
        },{
          attributeName: "col2",
          keyType: "RANGE"
        }
      ],
      globalSecondaryIndexes: [
        {
          indexName: "gindex1",
          keySchema: [
              {
                attributeName: "col3",
                keyType: "HASH"
              },{
                attributeName: "col4",
                keyType: "RANGE"
              }
          ],
          projection: {
            nonKeyAttributes: [
                "col1",
                "col2"
            ],
            projectionType: "INCLUDE"
          },
          provisionedThroughput: {
            readCapacityUnits: 1,
            writeCapacityUnits: 1
          }
        }
      ],
      provisionedThroughput: {
        readCapacityUnits: 1,
        writeCapacityUnits: 1
      },
      tableName: "table1"
    });
  }
}

const app = new cdk.App();
new CdkStackIam(app, 'prd-dynamodb');
app.run();

Stackを実行する

  • ビルドする。
$ npm run build
> cdk@0.31.0 build /home/ubuntu/environment/devops/cdk/dynamodb
> tsc
  • CloudFormation テンプレートを確認する。
$ cdk synth prd-dynamodb
Resources:
  TableMultiIndex:
    Type: AWS::DynamoDB::Table
    Properties:
      KeySchema:
        - AttributeName: col1
          KeyType: HASH
        - AttributeName: col2
          KeyType: RANGE
      AttributeDefinitions:
        - AttributeName: col1
          AttributeType: S
        - AttributeName: col2
          AttributeType: S
        - AttributeName: col3
          AttributeType: "N"
        - AttributeName: col4
          AttributeType: S
      GlobalSecondaryIndexes:
        - IndexName: gindex1
          KeySchema:
            - AttributeName: col3
              KeyType: HASH
            - AttributeName: col4
              KeyType: RANGE
          Projection:
            NonKeyAttributes:
              - col1
              - col2
            ProjectionType: INCLUDE
          ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      TableName: table1
    Metadata:
      aws:cdk:path: prd-dynamodb/TableMultiIndex
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: aws-cdk=0.31.0,@aws-cdk/aws-applicationautoscaling=0.31.0,@aws-cdk/aws-autoscaling-common=0.31.0,@aws-cdk/aws-cloudwatch=0.31.0,@aws-cdk/aws-dynamodb=0.31.0,@aws-cdk/aws-iam=0.31.0,@aws-cdk/cdk=0.31.0,@aws-cdk/cx-api=0.31.0,@aws-cdk/region-info=0.31.0,jsii-runtime=node.js/v8.16.0
  • CloudFormation をデプロイする。
$ cdk deploy prd-dynamodb -f
prd-dynamodb: deploying...
prd-dynamodb: creating CloudFormation changeset...
 0/3 | 08:29:22 | CREATE_IN_PROGRESS   | AWS::DynamoDB::Table | TableMultiIndex 
 0/3 | 08:29:23 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata   | CDKMetadata 
 0/3 | 08:29:23 | CREATE_IN_PROGRESS   | AWS::DynamoDB::Table | TableMultiIndex Resource creation Initiated
 0/3 | 08:29:26 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata   | CDKMetadata Resource creation Initiated
 1/3 | 08:29:26 | CREATE_COMPLETE      | AWS::CDK::Metadata   | CDKMetadata 
 2/3 | 08:29:54 | CREATE_COMPLETE      | AWS::DynamoDB::Table | TableMultiIndex 
 3/3 | 08:29:56 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | prd-dynamodb 

 ✅  prd-dynamodb

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:522299100431:stack/prd-dynamodb/0111f4f0-7dfe-11e9-9c3c-0637bd8af9a0

  • 差分が無い事を確認する。
$ cdk deploy prd-dynamodb -f
prd-dynamodb: deploying...
prd-dynamodb: creating CloudFormation changeset...

 ✅  prd-dynamodb (no changes)

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:522299100431:stack/prd-dynamodb/0111f4f0-7dfe-11e9-9c3c-0637bd8af9a0

まとめ

AWS CDK を使用して DynamoDB のテーブルを作成することができた。
非常に短いステップ数で記述することができる。

その他

今回使用したソースコードを下記のリポジトリに格納した。
実行する方法を記載する。

$ git clone -b blog-linux https://github.com/curry9999/devops.git
$ cd devops/
$ ./_deploy_dynamodb.sh