Course contentsShow
← Security
Lesson 1950 of 3,05239. Cloud Security - Compute and ServerlessFree lesson

Least Privilege for Serverless Functions

Apply principle of least privilege to function execution roles, granting only necessary permissions for specific operations.

Least Privilege for Serverless Functions

What You'll Learn

You'll learn how to restrict serverless function permissions to only what's absolutely necessary, minimizing blast radius when things go wrong.

The Core Problem

Each serverless function executes under an IAM role (or equivalent execution identity). If you grant that role overly broad permissions—say, S3:* on all buckets—a compromised function could read, modify, or delete data far beyond its intended scope. Since serverless functions often process untrusted input and run third-party dependencies, least privilege is your safety net.

Why It Matters

Think of a function execution role like a security badge at a facility. You wouldn't give a cafeteria worker access to the server room, executive offices, and vault—just the kitchen and storage. Similarly, a function that only needs to read from one S3 bucket shouldn't have write permissions, access to other buckets, or the ability to terminate EC2 instances.

Applying Least Privilege

  1. Grant per-function roles: Don't reuse one "super role" across all functions. Each function gets its own role.
  2. Scope by resource: Use ARNs to limit permissions to specific buckets, tables, or queues—not wildcards.
  3. Restrict actions: If the function only reads, grant s3:GetObject, not s3:*.
  4. Condition keys: Add conditions like IP ranges, time windows, or MFA requirements where applicable.

Example Scenario

A function processes uploaded images from my-uploads bucket and writes thumbnails to my-thumbnails. Grant:

  • s3:GetObject on arn:aws:s3:::my-uploads/*
  • s3:PutObject on arn:aws:s3:::my-thumbnails/*

Not s3:* on *. If an attacker injects malicious code via a dependency, they can't exfiltrate data from unrelated buckets or delete production files.

Monitoring & Iteration

Use CloudTrail and function logging to audit which permissions are actually used. Remove unused permissions over time.

Key Takeaway:
Assign each serverless function the minimum IAM permissions required for its specific task—nothing more—to contain the damage from injection attacks, dependency vulnerabilities, or misconfigurations.