前回の Amazon Inspector の検出結果を Athena で確認 の続きです。 AWS の設定方法をまとめます。
Amazon Inspector の検出結果を Lambda で S3 に保管する参考記事は色々とあったのですが、コードを書かなくてもいい方法ということで下記のデータフローにしています。
Inspector --> Security Hub --> EventBridge --> Kinesis(Lambda) --> S3 --> Athena
Lambda を作成します。
Node.js 14.x で index.js ファイルに下記のコードを入力します。
※ClassMethod さんのこちらの記事を参考にさせてもらいました!! dev.classmethod.jp
exports.handler = (event, context, callback) => { const output = event.records.map((record) => { let entry = (new Buffer(record.data, 'base64')).toString('utf8'); let result = entry + '\n' const payload = (new Buffer(result, 'utf8')).toString('base64'); return { recordId: record.recordId, result: 'Ok', data: payload, }; }); console.log(`Processing completed. Successful records ${output.length}.`); callback(null, { records: output }); };
Kinesis Data Streams を作成します。
データストリーム名 を入力して作成します。
Kinesis Delivery streams を作成します。
- Source:Kinesis Data Streams を選択
- Destination:Amazon S3 を選択
- Source settings:作成した Kinesis Data Streams を選択
- Delivery stream name:Delivery stream name を入力
作成した Lambda を指定します。
下記を選択して、Kinesis Delivery streams を作成します。
- S3 bucket:S3 バケットを選択
- S3 bucket prefix - optional:inspector/
- S3 bucket error output prefix - optional:inspector-error/
Amazon EventBridge を作成します。
Amazon Inspector のイベントを検出するよう下記パターンを入力します。
{ "source": [ "aws.securityhub" ], "detail-type": [ "Security Hub Findings - Imported" ], "detail": { "findings": { "ProductFields": { "aws/securityhub/ProductName": [ "Inspector" ] } } } }
下記を選択して、Amazon EventBridge を作成します。
- ターゲット:Kinesis ストリームを選択
- ストリーム:作成したストリームを選択
- この特定のリソースに対して新しいロールを作成する:選択
後は 前回の記事 の通り、Amazon Inspector の検出結果が自動的に S3 へ保管されるので、Athena クエリで出力します。