Sub topic 13
Automating response with Lambda and Security Hub automation
This chapter assumes you have an EventBridge rule that matches high-severity GuardDuty findings, as built in the previous chapter. It also assumes you can read basic Python and IAM policies. Here you connect that rule to a Lambda function and decide which actions to automate.
The working model
EventBridge delivers a matching finding to a target. The target is a Lambda function that reads the event, decides what to do, and acts on the affected resource.
Security Hub automation works at a different point in the pipeline. Automation rules change findings after Security Hub ingests them. For example, a rule can raise severity, set the workflow status to SUPPRESSED, or add a note. They do not run your code. To act on resources, use an EventBridge rule or a Security Hub custom action, which sends selected findings to EventBridge when an analyst chooses it.
A response function
The handler from the plan extracts the finding type, severity, and instance ID. The version below adds two safeguards. It tags the instance first, and it isolates the instance only when you set a quarantine security group.
import os
import boto3
ec2 = boto3.client("ec2")
QUARANTINE_SG = os.environ.get("QUARANTINE_SG_ID")
def handler(event, context):
detail = event["detail"]
finding_type = detail["type"]
severity = detail["severity"]
instance_id = (
detail.get("resource", {})
.get("instanceDetails", {})
.get("instanceId")
)
print(f"{finding_type} severity={severity} instance={instance_id}")
if instance_id:
ec2.create_tags(
Resources=[instance_id],
Tags=[{"Key": "SecurityReview", "Value": finding_type[:200]}],
)
if QUARANTINE_SG:
ec2.modify_instance_attribute(
InstanceId=instance_id, Groups=[QUARANTINE_SG]
)
return {"type": finding_type, "severity": severity, "instanceId": instance_id}The execution role needs ec2:CreateTags and, for isolation, ec2:ModifyInstanceAttribute. Scope both to the resources you intend to touch. EventBridge also needs permission to call the function, which you add with aws lambda add-permission using the principal events.amazonaws.com and the rule's ARN as the source.
Test before trusting
Run create-sample-findings as shown earlier in the tutorial and check the function's CloudWatch logs. Sample findings use placeholder instance IDs, so the EC2 calls will fail with an InvalidInstanceID error. This is expected. Test the isolation path against a real instance that you own and can afford to disconnect.
Common errors
- The rule matches but nothing runs. The Lambda resource policy is usually missing. Check the rule's target and the function's permissions.
- Duplicate invocations. GuardDuty republishes updated findings at your publishing frequency, and EventBridge delivers at least once. Make the handler idempotent. Tagging is safe to repeat. Sending an email each time is not.
- A
KeyErroroninstanceDetails. Not every finding concerns an EC2 instance. S3, IAM, and EKS findings carry different resource fields. Use.get()as above and branch ondetail["resource"]["resourceType"]. - Isolating production by mistake. Isolating an instance is disruptive. Start with tagging and notification. Add isolation only for finding types you have reviewed, and keep the quarantine group's rules documented.
- Rule too broad. A severity filter alone can trigger actions on findings you have not reviewed. Add a
typeprefix filter when an action fits only certain threats.
Exercise
Your Security Hub automation rule sets high-severity GuardDuty findings to a CRITICAL label, but your Lambda function never runs. Why, and what should you add?
Show answer
Automation rules only update findings inside Security Hub. They do not invoke code. Add an EventBridge rule (or a Security Hub custom action) that matches the findings and targets the Lambda function, and grant EventBridge permission to invoke it.