Welcome to Day 22 of the 30 Days AWS Security Challenge, and the start of week four, focused on application and workload security. We've spent three weeks on identity, data, networking, and monitoring. Now we apply those ideas specifically to the workloads running on top, starting with Lambda.
Why Serverless Doesn't Mean Security Free
It's tempting to think that because Lambda removes the need to manage servers, it also removes most security concerns. That's only partly true. AWS still handles the underlying infrastructure, following the Shared Responsibility Model from Day 1, but everything about how your function is configured, what it can access, and what code it runs is still entirely your responsibility.
Give Every Function Its Own Role
Every Lambda function runs with an IAM role attached, called an execution role, which determines what AWS resources that function can access. A common mistake is reusing one broad role across many functions because it's convenient, or attaching an overly permissive managed policy to save time during setup.
Instead, give each function its own role, scoped to exactly what that specific function needs, following the least privilege principle we've talked about since Day 2. A function that only reads from one DynamoDB table should have a role that allows exactly that, and nothing more. If that function is ever compromised through a vulnerability in its code or a dependency, a tightly scoped role limits how much damage it can actually do.
Manage Environment Variables Carefully
Lambda lets you pass configuration to your function through environment variables, which is convenient, but it's easy to misuse this for secrets. Storing a database password directly as a plain environment variable means it's visible to anyone with permission to view the function's configuration in the console.
Lambda does support encrypting environment variables using KMS, which helps, but a stronger pattern is retrieving actual secrets from Secrets Manager or Parameter Store, which we covered on Day 11, at runtime instead of storing them as static configuration on the function itself. This also makes rotation possible without needing to update and redeploy the function.
Keep Dependencies Updated
Lambda functions often rely on external libraries and packages, just like any other application. A vulnerability in one of those dependencies becomes a vulnerability in your function. This is exactly what Inspector, which we covered on Day 19, scans for automatically when applied to Lambda, flagging vulnerable dependencies without requiring a manual audit.
Beyond automated scanning, it's worth periodically reviewing and updating your function's dependencies deliberately, rather than only reacting when a scanner flags something.
Validate All Input
Lambda functions are frequently triggered by external input, an API Gateway request, an S3 event, a message from SQS. Never assume that input is well formed or safe just because it came through an AWS service. Validate and sanitize anything a function receives before acting on it, the same way you would for a traditional web application, since a Lambda function processing malicious input is just as exploitable as a server doing the same thing.
Control Network Access Where It Matters
By default, a Lambda function has no access to resources inside your VPC. If a function needs to reach a private RDS database, you configure it to run inside your VPC, which brings Security Groups (from Day 12) into play the same way they apply to EC2. Scope that Security Group narrowly, allowing only the specific access the function actually needs, rather than attaching an overly broad one out of convenience.
Set Sensible Limits
Lambda lets you configure a timeout and memory allocation for each function. Setting these too generously isn't just a cost issue, it can also increase the potential impact of certain abuse patterns, like a function accidentally or maliciously triggered in a tight loop. Set timeouts and memory to match what the function genuinely needs, rather than defaulting to the maximum out of habit.
A Practical Example
Imagine a Lambda function that processes uploaded images from an S3 bucket. A secure setup gives that function a role allowing only s3:GetObject on the specific source bucket and s3:PutObject on the specific destination bucket, retrieves any needed API keys from Secrets Manager rather than as a plain environment variable, validates that uploaded files are actually images before processing them, and has dependencies kept current through regular Inspector scans. Compare that to a function with a broad s3:* role, a hardcoded API key, and no input validation, and the difference in blast radius if something goes wrong is enormous.
Key Takeaways
- Give every Lambda function its own narrowly scoped execution role, don't reuse one broad role across functions
- Retrieve secrets from Secrets Manager or Parameter Store at runtime instead of storing them as plain environment variables
- Keep dependencies updated and let Inspector flag known vulnerabilities automatically
- Validate all input, regardless of which AWS service triggered the function
- Scope VPC access and Security Groups narrowly when a function needs to reach private resources
Tomorrow we widen the lens from individual functions to containers, covering the basics of keeping ECR, ECS, and EKS secure.
Loading article