📝
Security Hub のカスタムアクションを AWS CLI で作成してみた
以下の構成を AWS CLI で作成してみました。
前提
- AWS CLI の実行環境: CloudShell
- Lambda の実行ロールは作成済み: AdministratorAccess を付与
- Security Hub は有効化済み
$ aws securityhub get-enabled-standards
{
"StandardsSubscriptions": [
{
"StandardsSubscriptionArn": "arn:aws:securityhub:ap-northeast-1:012345678901:subscription/aws-foundational-security-best-practices/v/1.0.0",
"StandardsArn": "arn:aws:securityhub:ap-northeast-1::standards/aws-foundational-security-best-practices/v/1.0.0",
"StandardsInput": {},
"StandardsStatus": "READY",
"StandardsControlsUpdatable": "READY_FOR_UPDATES"
}
]
}
01. Lambda 関数の作成
$ cat > lambda_function.py << 'EOF'
import json
import logging
# ログレベルを設定
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""
Security HubのカスタムアクションからのEventBridgeイベントを処理する関数
"""
logger.info("=== Security Hub Custom Action Event Received ===")
# イベント全体をログ出力
logger.info(f"Full Event: {json.dumps(event, indent=2, default=str)}")
# EventBridgeからの詳細情報を抽出
if 'detail' in event:
detail = event['detail']
logger.info(f"Event Detail: {json.dumps(detail, indent=2, default=str)}")
# カスタムアクション情報があれば表示
if 'actionName' in detail:
logger.info(f"Custom Action Name: {detail['actionName']}")
# 対象のfinding情報があれば表示
if 'findings' in detail:
logger.info(f"Number of findings: {len(detail['findings'])}")
return {
'statusCode': 200,
'body': json.dumps('Security Hub event processed successfully')
}
EOF
$ zip lambda_function.zip lambda_function.py
$ aws lambda create-function \
--function-name SecurityHubCustomActionHandler \
--runtime python3.13 \
--role your-role-arn \
--handler lambda_function.lambda_handler \
--zip-file fileb://lambda_function.zip \
--description "Security Hub Custom Action Event Handler"
02. EventBridge ルールの作成
$ aws events put-rule \
--name SecurityHubCustomActionRule \
--event-pattern '{
"source": ["aws.securityhub"],
"detail-type": ["Security Hub Findings - Custom Action"]
}' \
--description "Rule to capture Security Hub custom action events"
$ cat > eventbridge-trust-policy.json << 'EOF'
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
$ aws iam create-role \
--role-name EventBridgeExecutionRole \
--assume-role-policy-document file://eventbridge-trust-policy.json \
--description "Role for EventBridge to invoke Lambda functions"
$ cat > lambda-invoke-policy.json << 'EOF'
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "*"
}
]
}
EOF
$ aws iam put-role-policy \
--role-name EventBridgeExecutionRole \
--policy-name LambdaInvokePolicy \
--policy-document file://lambda-invoke-policy.json
$ aws events put-targets \
--rule SecurityHubCustomActionRule \
--targets "Id"="1","Arn"="arn:aws:lambda:ap-northeast-1:012345678901:function:SecurityHubCustomActionHandler","RoleArn"="arn:aws:iam::012345678901:role/EventBridgeExecutionRole"
03. Security Hub のカスタムアクションの作成
$ aws securityhub create-action-target \
--name "test" \
--description "test" \
--id "test"
04. 動作確認
Security Hub コンソール > 検出結果で任意の結果をチェックし、アクションからカスタムアクションを選択します。
CloudWatch Logs のロググループに Lambda の実行ログが出力されていることを確認します。
以下のようなログが出力されていれば OK です。
=== Security Hub Custom Action Event Received ===
Full Event: <Security Hub の検出結果の JSON>
Event Detail: <Security Hub の検出結果の JSON>
Custom Action Name: test
Number of findings: 1
まとめ
今回は Security Hub のカスタムアクションを AWS CLI で作成してみました。
どなたかの参考になれば幸いです。
Discussion