背景
WAFコンソール上にWafのログが3時間しか表示されなくて、CLIでログをlocalにダウンロードしたいと思います。
※AWS WAFのログを直接CloudWatch LogsおよびS3に出力可能ですが、ここでは、ほかのサービスを使わない場合、CLIだけで取得する方法を紹介します。
—–AWS WAF———
Sampled requests
Samples of requests from the past 3 hours.
—–AWS WAF———
WAFのログを取得するpowershell
$global:LASTEXITCODE = $null
$SYS_DATE_CURRENT=(Get-Date).ToString("yyyy-MM-ddTHH")
$SYS_DATE_TWO_HOURS_AGO=((Get-Date).Addhours(-2)).ToString("yyyy-MM-ddTHH")
$date_from=$SYS_DATE_TWO_HOURS_AGO + ":01"
$date_to=$SYS_DATE_CURRENT + ":00"
$date_from_utc9=$date_from + "+0900"
$date_to_utc9=$date_to + "+0900"
$out_file_sort="D:\WAF\" + $SYS_DATE_CURRENT + "waf_test.log"
aws wafv2 get-sampled-requests `
--web-acl-arn arn:aws:wafv2:ap-northeast-1:717076937412:regional/webacl/KbyDev3-Tyo-WebACL-BlockUrl/ebe0d79c-3d57-4d9b-a0d1-f16d911bbb71 `
--rule-metric-name WhitelistWAFv2WebACLRulePathPattern `
--scope=REGIONAL `
--time-window StartTime=$date_from_utc9,EndTime=$date_to_utc9 `
--max-items 100 | D:\jq\jq.exe -r ".SampleRequests[] | [.Timestamp,.RuleNameWithinRuleGroup,.Action,.Request.ClientIp,.Request.Country,.Request.URI,.Request.Method,.Request.Headers[].Name,.Request.Headers[].Value]|@csv" `
| Sort-Object | Set-Content -Encoding UTF8 $out_file_sort
$PS_EXITCODE=$LASTEXITCODE
$endstr="PS_EXITCODE" + $PS_EXITCODE
Write-Output $endstr
exit $PS_EXITCODE
参考
https://docs.aws.amazon.com/cli/latest/reference/wafv2/get-sampled-requests.html