How to Secure AI Agents on AWS
SafeClaw by Authensor provides deny-by-default action gating for AI agents running on any AWS compute service. Whether your agent runs on EC2, Lambda, ECS/Fargate, or App Runner, SafeClaw intercepts every action the agent attempts and checks it against your YAML policy before execution. Install it in seconds with npx @authensor/safeclaw and enforce least-privilege controls across your entire AWS deployment.
Prerequisites
- Node.js 18+ on your build machine or container image
- An AWS account with IAM permissions to deploy to your target compute service
- Your agent code using Claude, OpenAI, or any supported LLM provider
Step 1 — Install SafeClaw in Your Project
npx @authensor/safeclaw
This scaffolds a safeclaw.config.yaml policy file and wires the gating middleware into your agent runtime. No external dependencies are pulled — SafeClaw is zero-dependency by design.
Step 2 — Define an AWS-Specific Policy
Create or edit safeclaw.config.yaml to restrict actions relevant to your AWS environment:
version: 1
defaultAction: deny
rules:
- action: "file:read"
path: "/app/**"
effect: allow
- action: "file:write"
path: "/tmp/**"
effect: allow
- action: "network:request"
host: "s3.amazonaws.com"
effect: allow
- action: "network:request"
host: "*.execute-api.amazonaws.com"
effect: allow
- action: "shell:execute"
command: "aws s3 cp *"
effect: deny
reason: "Direct S3 CLI access blocked — use the application SDK"
- action: "env:read"
key: "AWS_SECRET_ACCESS_KEY"
effect: deny
reason: "Agent must not read raw credentials"
This policy allows the agent to read application files and write to /tmp, permits network calls to S3 and API Gateway, but blocks direct CLI access to S3 and prevents credential exfiltration from environment variables.
Step 3 — Deploy on EC2
For EC2 instances running a long-lived agent process, bake SafeClaw into your AMI or user data script:
#!/bin/bash
cd /app
npm ci --production
npx @authensor/safeclaw
node agent.js
Combine SafeClaw's application-level gating with an IAM instance profile that grants only the specific S3 buckets and DynamoDB tables your agent needs. SafeClaw handles action-level decisions; IAM handles service-level authorization.
Step 4 — Deploy on AWS Lambda
For serverless agents triggered by API Gateway, SQS, or EventBridge:
import { createSafeClawGate } from "@authensor/safeclaw";
const gate = createSafeClawGate({ policy: "./safeclaw.config.yaml" });
export const handler = async (event) => {
const agent = buildAgent({ gate });
return agent.run(event);
};
Lambda's execution environment is ephemeral, so SafeClaw's hash-chained audit log should be flushed to S3 or CloudWatch at the end of each invocation. Set audit.sink: "stdout" and let CloudWatch Logs capture the trail.
Step 5 — Deploy on ECS / Fargate
In your Dockerfile:
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY . .
RUN npx @authensor/safeclaw
CMD ["node", "agent.js"]
In your ECS task definition, assign an IAM task role scoped to only the resources the agent needs. SafeClaw runs inside the container and gates every action the LLM attempts before it reaches the AWS SDK.
Step 6 — Gate S3 Bucket Access
If your agent needs to read from S3 but should never write:
rules:
- action: "network:request"
method: "GET"
host: "*.s3.amazonaws.com"
effect: allow
- action: "network:request"
method: "PUT"
host: "*.s3.amazonaws.com"
effect: deny
reason: "Agent is read-only for S3"
This provides defense-in-depth on top of IAM policies. Even if the IAM role accidentally grants s3:PutObject, SafeClaw blocks the write at the application layer.
Step 7 — Verify with the Audit Trail
SafeClaw's 446-test suite includes audit integrity checks. After deployment, verify the hash-chained audit log is intact:
npx @authensor/safeclaw audit verify --last 100
Every denied and allowed action is logged with a tamper-proof hash chain, giving you a compliance-grade record of everything your AI agent attempted on AWS.
Why This Matters
IAM policies control which AWS services your compute role can access, but they cannot see inside your application. An AI agent with s3:* permissions and no application-level gating can read every object in every bucket the role allows. SafeClaw closes this gap by gating the agent's intent before it reaches the AWS SDK.
Related Pages
- Deny-by-Default Explained
- Hash-Chained Audit Logs Deep Dive
- SafeClaw vs. Cloud IAM
- Zero-Trust AI Agent Architecture
- CI/CD Pipeline Agent Safety
Try SafeClaw
Action-level gating for AI agents. Set it up in your browser in 60 seconds.
$ npx @authensor/safeclaw