LoginSignup
1
0

More than 5 years have passed since last update.

【AWS勉強メモ】Serverless Frameworkで暗号化をサポートしたDynamoDBテーブルを作成

Posted at

2018年2月8日よりDynamoDBは暗号化をサポートしています. 機密度の高いデータを取り扱うアプリケーションをDynamoDBで構築することが可能となっています.本記事では,Serverless Frameworkを利用して,暗号化されたDynamoDBをお手軽に作成する方法を記述します.

目次

  • 1. Serverless Frameworkで環境構築
  • 2. 注意事項
  • 3. 補足

1. Serverless Frameworkで環境構築

実行環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.4
BuildVersion:   17E202

$ sls -v
1.27.1

環境構築

DynamoDBを作成するだけのプロジェクトを作成

$ sls create --template aws-nodejs --path createDynamoDB
$ cd createDynamoDB/
$ rm handler.js
$ tree
.
└── serverless.yml

0 directories, 1 files
serverless.yml
service: createDynamoDB

provider:
  name: aws
  profile: serverless-admin
  region: ap-northeast-1 # 東京リージョン

  iamRoleStatements:
    - Effect: "Allow"
      Action:
       - dynamodb:GetItem
       - dynamodb:UpdateItem
      Resource: "arn:aws:dynamodb:ap-northeast-1:xxxxxxxxxxx:table/*"

resources:
  Resources:
    DynamoDbHeight:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: height
        AttributeDefinitions:
          - AttributeName: user_id
            AttributeType: S
        KeySchema:
          - AttributeName: user_id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        SSESpecification:
          SSEEnabled: true # 暗号化を有効化
        Tags:
          - Key: name
            Value: Dev # 開発用をわかるようにタグを付ける
    DynamoDbWeight:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: weight
        AttributeDefinitions:
          - AttributeName: user_id
            AttributeType: S
        KeySchema:
          - AttributeName: user_id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        SSESpecification:
          SSEEnabled: false # 暗号化しない
        Tags:
          - Key: name
            Value: Dev # 開発用をわかるようにタグを付ける

環境をデプロイ

$ sls deploy -v

Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - createDynamoDB-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - createDynamoDB-dev
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - createDynamoDB-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - DynamoDbWeight
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - DynamoDbHeight
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - DynamoDbWeight
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - DynamoDbHeight
CloudFormation - CREATE_COMPLETE - AWS::DynamoDB::Table - DynamoDbWeight
CloudFormation - CREATE_COMPLETE - AWS::DynamoDB::Table - DynamoDbHeight
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - createDynamoDB-dev
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - createDynamoDB-dev
Serverless: Stack update finished...
Service Information
service: createDynamoDB
stage: dev
region: ap-northeast-1
stack: createDynamoDB-dev
api keys:
  None
endpoints:
  None
functions:
  None

Stack Outputs
ServerlessDeploymentBucketName: createdynamodb-dev-serverlessdeploymentbucket-mjtk4r2bqc01

暗号化されたheightテーブルと,暗号化していないweightテーブルが作成されたかをAWS Management Consoleにて確認

image00.png

環境を削除

$ sls remove

Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
.....
Serverless: Stack removal finished...

2. 注意事項

保管時の Amazon DynamoDB 暗号化に記載がある通り,新規にDynamoDBテーブルを作成するときのみデータの暗号化が可能です.暗号化されていない既存のテーブルで,データを暗号化することは2018年7月9日現在対応していません.

保管時の暗号化は 新規の DynamoDB テーブルを作成するときのみ有効化できます。現在のところ、既存のテーブルで 保管時の暗号化 を有効にすることはできません。保管時の暗号化を有効にすると、無効化することはできません。機密データを含むすべてのテーブルで暗号化を有効化することが推奨されます。

3. 補足

AWS Management Consoleにて暗号化したテーブルを作成する場合は,テーブル作成時に, 「デフォルト設定の使用」のチェックを外し,「保管時の暗号化」にチェックを入れる.

image01.png

image02.png

1
0
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
1
0