スキップしてコンテンツを表示

AWS CLIで範囲指定が効かない

0

以下のコードで1日間のログをAWSCLIにて取得しようとしたのですが、30行しか出力できません。どうしてでしょうか。

現在時刻と24時間前のUNIXタイムスタンプ(ミリ秒)を取得

$now = Get-Date $epoch = Get-Date "1970-01-01T00:00:00Z"

現在のUTC時間を取得

$currentUTC = [DateTime]::UtcNow $currentUTC2 = [DateTime]::UtcNow.AddHours(-24)

UTC時間をミリ秒に換算

$endTime = [System.Math]::Round(($currentUTC - $epoch).TotalMilliseconds) $startTime = [System.Math]::Round(($currentUTC2 - $epoch).TotalMilliseconds)

#Write-Host "開始時間:" + $currentUTC #Write-Host "終了時間:" + $currentUTC2 #Write-Host "開始時間ms:" + $startTime #Write-Host "終了時間ms:" + $endTime

#すでに当日のログがあれば削除

if (Test-Path $cwLogFile) { Remove-Item -Path $cwLogFile -Force } if (Test-Path $outputCsv1) { Remove-Item -Path $outputCsv1 -Force } if (Test-Path $outputCsv2) { Remove-Item -Path $outputCsv2 -Force } if (Test-Path $outputCsv3) { Remove-Item -Path $outputCsv3 -Force }

CloudWatch Logs からログを取得

$PSDefaultParameterValues['*:Encoding'] = 'UTF8'

$nextToken = $null

do { if ($nextToken) { $response = aws logs get-log-events --log-group-name $logGroupName --log-stream-name $logStreamName --start-time $startTime --end-time $endTime --region $region --limit 10000 --next-token $nextToken --output json | ConvertFrom-Json } else { $response = aws logs get-log-events --log-group-name $logGroupName --log-stream-name $logStreamName --start-time $startTime --end-time $endTime --region $region --limit 10000 ` --output json | ConvertFrom-Json }

--end-time $endTime `

$response.events | ForEach-Object {
	"$($_.message)"| Out-File -FilePath $cwLogFile -Append
}

# 次のトークンを取得
$newToken = $response.nextForwardToken

# トークンが更新されない場合はループを終了
if ($nextToken -eq $newToken) {
	break
}

$nextToken = $newToken

} while ($nextToken -ne $null)

質問済み 9ヶ月前163ビュー
1回答
0

ご認識されております通り、get-log-events コマンド [1] は 1 回の実行で全てのログイベントを取得できるとは限らず、next-token パラメータを指定して API を繰り返し実行いただく必要がございます。 ご共有いただきましたスクリプトに関して以下の観点でご確認いただけますでしょうか。

  1. スクリプト実行時に --next-token に正しい値が設定されているか お客様にてスクリプトのデバッグをご実施いただき、繰り返し実行される get-log-events コマンドの next-token パラメータに前回返答されたトークンの値が反映されているかご確認ください

  2. --start-from-head を有効化または nextForwardToken ではなく nextBackwardToken を指定する ご共有いただきましたスクリプトからは --start-from-head [1] を有効にされていないように見受けられたため、こちらを有効化いただくか next-token に nextBackwardToken を指定することで事象が改善されるかご確認ください

また代替案にはなりますが、filter-log-events [2] ではトークンを用いて対象のログイベントが全量取得できるまで繰り返し API を呼び出す仕組みがコマンド自体に実装されておりますため、こちらをご利用いただくこともご検討ください。

[1] get-log-events — AWS CLI 1.40.27 Command Reference https://docs.aws.amazon.com/cli/latest/reference/logs/get-log-events.html

[2] filter-log-events — AWS CLI 1.40.27 Command Reference https://docs.aws.amazon.com/cli/latest/reference/logs/filter-log-events.html

回答済み 9ヶ月前
AWS
サポートエンジニア
修正済み 9ヶ月前
AWS
サポートエンジニア
修正済み 9ヶ月前
AWS
サポートエンジニア
修正済み 9ヶ月前
AWS
サポートエンジニア
修正済み 9ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

関連するコンテンツ