tezu memo blog

日々行った作業をメモしていきます

SES メールを連続送信後にAthenaで結果を確認すると1件しか表示されない Kinesis 動的パーティショニングキーで解決

最近、SESと戯れています

事象

SESを使用してメールを連続送信後にAthenaでメール送信の結果を確認すると1件しか表示されない

CREATE
EXTERNAL TABLE SES_RESULTS (
  notificationType string,
  mail struct <messageId:string, commonHeaders:struct < `from`:array<string>,`to`:array<string>, subject:string >>,
  delivery struct < timestamp:string >
 )
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ('ignore.malformed.json' = 'true')
LOCATION 's3://bucket_name/[folder]/'

メールボックスシミュレーターを使用しています docs.aws.amazon.com

WITH dataset AS (
SELECT
    notificationType,
    mail.commonHeaders.to as to_addresses,
    mail.commonHeaders.subject as subject,
    mail.messageId AS messageId
FROM SES_RESULTS )
SELECT 
  notificationType,
  TIMESTAMP,
  to_addresses[1] as to_address,
  subject,
  messageId
FROM dataset
WHERE to_addresses[1] = 'success@simulator.amazonses.com'
ORDER BY TIMESTAMP DESC
LIMIT 10

原因

aws.typepad.com

Q8. Amazon Kinesis Firehose を利用してCloudWatch LogsをS3に転送してそれをAthena で分析したいのですが、Kinesis Firehoseを通すと{json}{json}のように1行に複数のJSONオブジェクトが保存されるようです。このデータを効率的にAthenaで分析するにはどういった方法がありますか?? A8. Amazon Kinesis FirehoseにはData TransformationをAWS Lambdaで行う機能がございますので,こちらを使って所望の形式に変換すると良いです. http://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html

確かに1行に複数のJSONオブジェクトが保存されている。送信したメールの件数も20件で一致している

% cat ses-kinesis-8-2022-10-07-00-19-59-f5cec068-edf9-414b-a03d-xxxxxxx | jq | grep notificationType | head -n 5
  "notificationType": "Delivery",
  "notificationType": "Delivery",
  "notificationType": "Delivery",
  "notificationType": "Delivery",
  "notificationType": "Delivery",

% cat ses-kinesis-8-2022-10-07-00-19-59-f5cec068-edf9-414b-a03d-xxxxxxx | jq | grep notificationType | wc -l
     20

対応

動的パーティショニングを使用する aws.amazon.com

SES ログ

こんな感じのフォーマットなので

{
  "notificationType": "Delivery",
  "mail": {
    "timestamp": "2022-10-07T00:19:58.503Z",
    "messageId": "01010183afd029a7-914ed9cd-641a-4eaf-87de-xxxxxxx-xxxxxxx",
  }
}
{
  "notificationType": "Delivery",
  "mail": {
    "timestamp": "2022-10-07T00:19:58.498Z",
    "messageId": "01010183afd029a2-a2d69f7f-4b3f-4a07-9935-xxxxxxx-xxxxxxx",
  }
}

Kinesis

ses-log-partitioning-!{partitionKeyFromQuery:year}/!{partitionKeyFromQuery:month}/!{partitionKeyFromQuery:day}/!{partitionKeyFromQuery:messageId}/

S3

  • 年月日 + MessageIdのフォルダに分かれている
  • 送信したメール数とオブジェクト数が一致している

Athena

送信したメールとレコード数が一致する

おまけ

制限とクォータ

aws.amazon.com

Kinesis Data Firehose 動的パーティショニング では、データをアクティブにバッファリングしている間、配信ストリームあたり 500 のアクティブパーティションの制限があります。これは、設定されたバッファリングヒント中に配信ストリームに存在するアクティブなパーティションの数です。この制限は調整可能で、引き上げたい場合は、制限引き上げのためのサポートチケットを送信する必要があります。

一度に500件以上のメールを送信すると発生しました。なかなか面倒ですね

{
   "attemptsMade":1,
   "arrivalTimestamp":1665109794722,
   "errorCode":"DynamicPartitioning.ActivePartitionsLimitExceeded",
   "errorMessage":"active-partition-count-exceeded",
   "attemptEndingTimestamp":1665109885901,
   "rawData":"="
}