AWS CLI DynamoDB Query コマンド

AWS CLIを使った DynamoDB Queryコマンドを使った テーブルデータ取得方法です。

テーブル名 = table1として Partition Key = ID ソートキーなしの場合

aws dynamodb query \
  --table-name table1 \
  --key-condition-expression "ID = :U" \
  --expression-attribute-values  '{":U":{"S":"検索したい文字"}}' \
  --profile あれば --region ap-northeast-1 > out.json

–profileや–region指定なければ入れなくてOKです。例では最後にout.jsonファイルに出力されます。ソートキーが無い場合Queryはあまり意味が無いかもです

上記 table1テーブルにPartition KeyとSort Keyがある場合(Sort Key = SORT1)

aws dynamodb query \
  --table-name table1 \
  --key-condition-expression "ID = :U and begins_with(SORT1, :S)" \
  --expression-attribute-values  '{":U":{"S":"検索したい文字"}, ":S":{"S":"検索したいソート名"}}' \
  --profile あれば --region ap-northeast-1 > out.json

–key-condition-expressionにbegins_with()が入っていますが この場合前方一致の文字列検索が可能です。

API Gateway マッピングテンプレートでリモートIPなど取得する

API Gateway マッピングテンプレートでリモートIPなど取得

AWS API GatewayからDynamoDBテーブルに直接データを書き込む場合、

API Gatewayマッピングテンプレートを使います。その場合、リモートIPやUserAgentを取得するには$contextを利用し

テンプレートを下記のように記述します

{
  "TableName": "テーブル名",
  "Item": { 
    "sourceIp": {"S": "$context.identity.sourceIp" },
    "requestTime": {"S": "$context.requestTime"},
    "userAgent": {"S": "$context.identity.userAgent"},
    "hoge": {
      "S": "$input.params('hoge')"
    }
  }
}

https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED