AWS Builder Center
Automating S3 Bucket Security Audits with AWS Lambda and Python (Boto3)

Automating S3 Bucket Security Audits with AWS Lambda and Python (Boto3)

Learn how to build a lightweight, serverless auditor that scans your S3 buckets for public access and missing encryption, then reports the findings automatically — a practical first project for student builders exploring cloud security.

Overview & Problem Statement

One of the most common — and most costly — cloud security mistakes is an S3 bucket left publicly accessible or unencrypted by accident. Manually checking bucket settings across an account doesn't scale, and it's easy to miss a misconfigured bucket buried in a long list.
This project solves that with a small, serverless auditor: an AWS Lambda function that scans your S3 buckets on a schedule, flags any that are publicly accessible or missing default encryption, and logs the results — no servers to manage, and it runs entirely within the AWS Free Tier for a student account.

Architecture & Workflow

The setup uses three services working together:
  • Amazon EventBridge triggers the Lambda function on a schedule (e.g., once a day).
  • AWS Lambda runs a Python script using Boto3 to list all S3 buckets, check each one's public access block settings and encryption configuration, and compile a findings report.
  • AWS IAM provides the Lambda function a tightly scoped role so it can only read bucket configurations — not modify or delete anything.Step-by-Step Implementation Guide

1. Create a least-privilege IAM role for the Lambda function

The function only needs read access to bucket policies and configuration — never write or delete permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketPolicyStatus",
"s3:GetBucketPublicAccessBlock",
"s3:GetEncryptionConfiguration",
"s3:ListAllMyBuckets"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}

2. Write the Lambda function (Python 3.12 + Boto3)

import boto3

s3 = boto3.client("s3")

def lambda_handler(event, context):
findings = []
buckets = s3.list_buckets()["Buckets"]

for bucket in buckets:
name = bucket["Name"]

# Check public access block settings
try:
pab = s3.get_public_access_block(Bucket=name)
block_config = pab["PublicAccessBlockConfiguration"]
is_public = not all(block_config.values())
except s3.exceptions.ClientError:
is_public = True # No block configuration = potentially public

# Check encryption
try:
s3.get_bucket_encryption(Bucket=name)
encrypted = True
except s3.exceptions.ClientError:
encrypted = False

if is_public or not encrypted:
findings.append({
"bucket": name,
"publicly_accessible": is_public,
"encrypted": encrypted
})

print(f"Audit complete. {len(findings)} bucket(s) flagged: {findings}")
return findings

3. Schedule the audit with EventBridge (AWS CLI)

aws events put-rule \
--name "daily-s3-security-audit" \
--schedule-expression "rate(1 day)"

aws lambda add-permission \
--function-name s3-security-audit \
--statement-id EventBridgeInvoke \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:REGION:ACCOUNT_ID:rule/daily-s3-security-audit

aws events put-targets \
--rule daily-s3-security-audit \
--targets "Id"="1","Arn"="arn:aws:lambda:REGION:ACCOUNT_ID:function:s3-security-audit"

Cost & Best Practices / Security Considerations

  • This project stays comfortably within the AWS Free Tier — Lambda's free tier includes 1M requests/month, and a daily invocation is negligible.
  • The IAM policy above follows least privilege: it can only read bucket settings, never modify them, minimizing risk if credentials were ever compromised.
  • For a next step, findings could be routed to Amazon SNS for email/SMS alerts instead of just CloudWatch Logs.
  • Always test IAM policies in a sandbox/student account before applying them broadly.

Summary & Next Steps

This is a small project, but it demonstrates a core pattern in cloud security: automated, scheduled auditing instead of manual checks. From here, natural extensions include auditing IAM users for MFA status, or checking EC2 security groups for open ports.
I'd love to hear how other student builders are approaching automated security checks in their own AWS learning journey — what would you add to this?


Note: This project is written for educational and learning purposes as part of my AWS cloud and cybersecurity learning journey. It is not intended as a production-ready security tool.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article