Skip to content

Serverless FrameworkでDynamoDBのテーブルをstageで分ける

Posted on:December 9, 2017 at 04:29 PM

今回は、Serverless FrameworkでDynamoDBをstageによって分ける書き方のメモです。

nabeen/linebot-kotlin

参考サイト

こちらが参考になりました。

書き方

参考サイトではstageに応じたプレフィックスをつけることで対応していますが、僕は設定ファイルから読み出す方法で書きました。

詳細はGithubを見ていただくとして、ここでは関係している部分のみ抜粋します。

まずはserverless.ymlResourceの部分、

provider:
  name: aws
  runtime: java8
  #  profile: serverless

  # you can overwrite defaults here
  stage: ${opt:stage, self:custom.defaultStage}
  region: ap-northeast-1
  # you can add statements to the Lambda function's IAM Role here
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:DescribeStream
        - dynamodb:ListStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
      Resource:
        - arn:aws:dynamodb:*:*:table/${self:custom.otherfile.environment.${self:provider.stage}.TABLE_NAME}
        - Fn::GetAtt: [LineBotDynamodb, StreamArn]

そして同じくserverless.ymlTableNameの部分です。

resources:
  Resources:
    LineBotDynamodb:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.otherfile.environment.${self:provider.stage}.TABLE_NAME}
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        StreamSpecification:
          StreamViewType: NEW_IMAGE

これでOK。

TABLE_NAMEはプログラム側でも使う数値なので、環境変数に設定するのもお忘れなく。