Automating Code Reviews and Security Scans with Agent Hooks
This lesson explains how Kiro agent hooks run checks automatically while code is written, and how automated reviews and security scans combine deterministic tools with AI review.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the triggers and actions of agent hooks.
- Identify the review points an automated review of AWS code should cover.
- Match each kind of security scan to where it runs in the development process.
Agent hooks
An agent hook is an automation that runs when something happens in the workspace. Hooks are JSON files stored in .kiro/hooks/, so they are versioned with the project and shared by the team; global hooks in ~/.kiro/hooks/ apply to every workspace. Each hook has a trigger and an action, as the following table describes.
| Trigger | Fires |
|---|---|
PostFileSave | After a file that matches a pattern is saved |
PreToolUse | Before the agent runs a tool, so the hook can check or block it |
PostToolUse | After a tool has run, for example after the agent writes a file |
Stop | When the agent finishes its turn |
A command action runs a shell command in the project root, such as a linter, a test runner or a scanner, and receives the session context as JSON on standard input. An agent action injects a prompt into the conversation, such as "review the file you just changed against the security rules in steering and fix any issue". Command actions are deterministic and cheap; agent actions can judge things a tool cannot check, such as whether error handling matches the team's standard.
Choose the hook trigger
Automated code reviews
An automated review applies the same checklist to every change, so reviewers can focus on design. The checklist belongs in a steering file, and hooks or a pipeline step apply it. Typical review points for AWS code include the following.
- SDK clients and connections are created outside the Lambda handler and reused.
- Errors from AWS calls are handled, retries are left to the SDK, and 4xx errors are not retried.
- IAM policies use specific actions and resources rather than wildcards.
- No credentials, keys or account-specific values appear in code; configuration comes from the environment, Parameter Store or Secrets Manager.
- Input from users, events and models is validated before it is used.
In the IDE, a Stop hook can ask the agent to review its own changes against the checklist before it hands control back. In a pull request, the Kiro CLI in headless mode runs the same review without a person present, as described in the lesson on AI tools in CI/CD pipelines.
Security scanning
Security scanning looks for known classes of problems before code reaches production. Combining deterministic scanners with an AI review gives better coverage than either alone, because scanners are precise about known patterns and the agent can reason about the context. The following table lists the main kinds of scan and where each runs.
| What is scanned | Example checks | Where it runs |
|---|---|---|
| Secrets | Access keys, passwords and tokens in code or configuration | A hook before commit; the pipeline |
| Dependencies | Libraries with known vulnerabilities | A hook on dependency changes; CodeBuild |
| Code | Injection, unsafe deserialisation, missing input validation | An agent review hook; headless Kiro in the pipeline |
| Infrastructure as code | Unencrypted resources, public access, wildcard permissions | cfn-lint and policy checks in the pipeline |
| Container images | Vulnerable operating system and language packages | Amazon ECR image scanning on push |
When a scan finds a problem, the agent can explain it and propose a fix, for example replacing string-built SQL with a parameterised query or moving a key into Secrets Manager. The developer reviews the fix like any other change.
Review on every save
A team wants every Python change checked for style, test failures and hard-coded secrets before it is committed.
The developer adds three hooks to .kiro/hooks/: a PostFileSave command hook for \.py$ files that runs the linter and the unit tests for the changed module; a PreToolUse hook that runs a secrets scanner before the agent may run git commit and blocks the commit if anything is found; and a Stop agent hook that asks the agent to review its changes against .kiro/steering/security.md.
The next time the agent writes an IAM policy with "Action": "s3:*", the review hook flags it and the agent narrows it to the two actions the function uses. Because the hooks live in the repository, every developer on the team gets the same checks.
Which hook trigger can stop the agent from committing code that contains a secret?
Show answer
PreToolUse, because it runs before the agent executes a tool such as a git command, so a scanner in the hook can block the commit. A PostToolUse hook would run only after the commit.
Exam Tips
Remember for the exam
- Run checks automatically when a file is saved: a PostFileSave agent hook.
- Block a risky agent action, such as a commit with secrets: a PreToolUse hook.
- Hooks and steering files in the repository give the whole team the same automated review.
- Combine deterministic scanners with an AI review; the developer still approves every fix.