LoginSignup
0
0

More than 1 year has passed since last update.

AWS API Gateway で直接 DynamoDB に読み取り・書き込み・削除を行う

Posted at

初めに

この記事では、以下のようなプライマリキーをもつテーブルを想定し、API Gateway の AWS 統合タイプを使用して DynamoDB の操作について書きます。

パーティションキー ソートキー
year title

読み取り

アクション:Scan

マッピングテンプレート例

{
    "TableName": "Movies"
}

書き込み

アクション:PutItem

マッピングテンプレート例

#set($inputRoot = $input.path('$'))
{
  "TableName": "Movies",
  "Item": {
    "title": {
      "S": "$inputRoot.title"
    },
    "year": {
      "N": "$inputRoot.year"
    },
    "genre": {
      "S": "$inputRoot.genre"
    },
    "director": {
      "S": "$inputRoot.director"
    },
    "star": {
      "S": "$inputRoot.star"
    },
    "place": {
      "S": "$inputRoot.place"
    }
  }
}

実行例

リクエスト body

{
    "year": "999",
    "title": "myMovie",
    "genre": "myGenre",
    "director": "myDirector",
    "star": "myStar",
    "place": "myPlace"
}

変換後

{
  "TableName": "Movies",
  "Item": {
    "title": {
      "S": "myMovie"
    },
    "year": {
      "N": "999"
    },
    "genre": {
      "S": "myGenre"
    },
    "director": {
      "S": "myDirector"
    },
    "star": {
      "S": "myStar"
    },
    "place": {
      "S": "myPlace"
    }
  }
}

削除

アクション:DeleteItem

マッピングテンプレート例

#set($inputRoot = $input.path('$'))
{
  "TableName": "Movies",
  "Key": {
    "title": {
      "S": "$inputRoot.title"
    },
    "year": {
      "N": "$inputRoot.year"
    }
  }
}

実行例

リクエスト body

{
    "year": "999",
    "title": "myMovie"
}

変換後

{
  "TableName": "Movies",
  "Key": {
    "title": {
      "S": "myMovie"
    },
    "year": {
      "N": "999"
    }
  }
}

参考記事

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