Lambda security is different from EC2 security in ways that make some traditional security concerns irrelevant and introduce new ones. You don't patch Lambda execution environments — AWS manages the underlying OS and runtime. You do control what permissions your functions have, what they can access, what data they handle, and how the function code itself is secured. Getting these elements right is the Lambda security challenge.
The main threat vectors for Lambda functions are: overly permissive execution roles that allow a compromised function to cause significant damage, exposed environment variables containing secrets, injection attacks through event data, and supply chain risks from dependencies with vulnerabilities.
Execution Role Least Privilege
Every Lambda function runs with an IAM execution role. The execution role grants the function permissions to call AWS services. The most common Lambda security mistake is using overly broad execution roles — attaching AmazonDynamoDBFullAccess when the function only reads from one table, or AdministratorAccess because it's easier than figuring out the minimum permissions needed.
Define execution roles at the function level, not shared across functions. A function that reads from DynamoDB should have a role that allows dynamodb:GetItem and dynamodb:Query on the specific table ARN, not DynamoDB access to all resources. This precision limits the blast radius of a compromised function.
IAM Access Analyzer generates least-privilege policies from CloudTrail activity. Deploy your function, run representative test traffic, then use Access Analyzer to generate a policy based on what the function actually called. This gives you a minimum policy derived from real behavior rather than trying to enumerate all required permissions manually. Review and adjust the generated policy before using it in production.
Environment Variable Security
Lambda environment variables are a common place to store configuration including sensitive values like database passwords, API keys, and connection strings. By default, Lambda encrypts environment variables at rest using AWS-managed KMS keys. For additional security, configure customer-managed KMS keys (CMK) for environment variable encryption — this allows you to audit key usage through CloudTrail and provides more control over key lifecycle.
For secrets that change frequently or need tight access control, use AWS Secrets Manager or SSM Parameter Store (with SecureString parameters) rather than environment variables. Retrieve secrets at function initialization time and cache them for the function lifetime. Secrets Manager handles rotation automatically; Parameter Store provides simpler access patterns for non-rotating values. Using Secrets Manager eliminates the risk of secrets visible in Lambda console screenshots, deployment logs, or environment variable exports.
The Lambda execution environment can be inspected by anyone with lambda:GetFunctionConfiguration permission. In many organizations this is broadly granted. If your function's environment variables contain sensitive values and you've chosen environment variables over Secrets Manager, restrict who can list and view function configurations.
VPC Configuration for Outbound Control
Lambda functions outside a VPC make direct network calls to AWS services through service endpoints and to the internet through the general AWS network. Lambda functions inside a VPC use VPC networking — outbound calls to the internet require a NAT gateway, and AWS service calls can use VPC endpoints.
For functions that should only communicate with specific internal resources (a database, an internal API), deploying in a VPC with appropriate security groups limits what the function can reach. A compromised function in a VPC with no internet access has significantly limited exfiltration options compared to one with unrestricted outbound. The tradeoff is cold start latency (VPC functions take longer to initialize) and the cost and complexity of the NAT gateway.
For functions that make many external calls and where internet access is legitimate, the VPC's value is in the security group rules and monitoring rather than network isolation. VPC Flow Logs on function network interfaces capture all outbound connections for forensic analysis.
Runtime Security and Dependency Management
Lambda's OS and language runtime are maintained by AWS. Your responsibility is the function code and its dependencies. Dependency vulnerabilities are a significant Lambda security risk — npm packages with vulnerabilities, pip packages with known CVEs, and container image layers with security patches are all surfaces you manage.
Enable Amazon Inspector for Lambda to scan function code and Lambda layers for known vulnerabilities in third-party dependencies. Inspector integrates with the Lambda console and generates findings that map to specific vulnerable packages with remediation recommendations. Enable it in all regions where you run Lambda functions.
Build dependency scanning into your CI/CD pipeline. Tools like Snyk, OWASP Dependency Check, and GitHub Dependabot scan for vulnerable packages at build time. Blocking deployments with critical vulnerabilities forces remediation before code reaches production.
Event Data Validation
Lambda functions receive event data from various triggers — API Gateway request bodies, SQS message contents, S3 event metadata, DynamoDB stream records. This data comes from external sources (API callers, upstream services) and must be treated as untrusted input. Function code that uses event data without validation is vulnerable to injection attacks — SQL injection if the data is used in a database query, command injection if it's passed to a subprocess, path traversal if it's used in file operations.
Validate all event data at the function entry point before using it in any downstream operation. Define a schema for expected event structure and reject events that don't match. For API Gateway triggers, API Gateway request validation can enforce input schema at the API layer before the event reaches Lambda, providing defense in depth.
Related Reading
- Lambda IAM permissions — detailed least-privilege implementation for Lambda roles
- Lambda concurrency limits — operational security through concurrency controls
- Serverless monitoring — observability for Lambda security events
- IAM security best practices — execution role design principles
FAQ
Does Lambda support security groups?
Lambda functions deployed in a VPC have ENIs (elastic network interfaces) with security groups, just like EC2 instances. Lambda functions not deployed in a VPC don't use security groups — they use service-level access controls. For Lambda functions in a VPC, security groups control what the function can reach (outbound) and what can reach the function (inbound, though Lambda functions are typically invoked through service triggers rather than directly accepting inbound connections).
How do I rotate a Lambda function's secrets without downtime?
If you're using Secrets Manager, it handles rotation while the function continues running. The Secrets Manager rotation Lambda updates the secret to the new value; your function's next call to GetSecretValue returns the new value. The function transparently picks up the rotation. For environment variables, you'd need to update the variable and redeploy the function — which is why environment variables are not the right mechanism for rotating secrets. Use Secrets Manager for anything that needs rotation.
Can I audit what my Lambda functions are doing?
CloudTrail logs all AWS API calls made by Lambda functions, including the function ARN and execution role ARN as the principal. You can see exactly which AWS services each function called, what resources it accessed, and from which region. For application-level audit (what your function code did with data), add structured logging to your function code and ship logs to CloudWatch Logs for analysis.
Protect your AWS accounts before it's too late
Vigilare monitors your AWS accounts for suspension risks — billing anomalies, IAM issues, GuardDuty findings, and more — and alerts you before AWS takes action.
Written by Vigilare Engineering
Platform Team