With serverless you no longer manage servers, and the infrastructure surface shrinks. Application security, however, is still yours. In Lambda functions, the risks usually show up as an execution role with too many permissions, a secret embedded in the code or in a variable, vulnerable dependencies, event data used without validation and uncontrolled exposure through API Gateway or a Function URL. Because functions are small and numerous, it is easy to keep piling up permissions and secrets without anyone reviewing them.
From a defensive point of view, Lambda security comes down to two questions that every function needs to have answered. The first is whether it has exactly the access it needs and not a bit more, which involves least privilege in the role, secrets management and dependencies. The second is whether only callers with a reason to trigger it can do so, and that is where the invocation policy, event validation, logs and the function's exposure come in.
Least-privilege execution role
Each function should have its own role, with the minimum permissions for what it does. The common mistake is reusing one broad role across many functions. In that case, compromising the most insignificant function in the group is enough to gain access to sensitive resources. Model permissions per function and restrict actions and resources to concrete names, without broad wildcards in Action or Resource, as in the snippet below.
{ "Effect": "Allow", "Action": ["sqs:ReceiveMessage", "sqs:DeleteMessage"], "Resource": "arn:aws:sqs:REGION:ACCOUNT:orders-queue"}The function runs with the role's permissions. If a flaw in the code allows unintended execution, whoever exploits it inherits that access. This is where least privilege limits the damage: if the role only reads one specific queue and writes to one table, that is all the attacker reaches. In the deployment role, pay special attention to iam:PassRole. It allows attaching more powerful roles to new functions, which is a classic privilege escalation path.
Who can invoke the function
The execution role says what the function can do. The resource-based policy answers a different question that matters just as much: who can invoke the function. Each trigger (API Gateway, S3, EventBridge) gets an invoke permission in that policy. If the grant is too broad (allowing invocation from any source in an account, for example), the function is open to unwanted invocations. Restrict the principal and the source to the legitimate trigger.
Function URLs call for care. When the auth type is open (NONE), the Function URL exposes the function directly to the internet with no authentication at all, and that is almost never what you want. Prefer IAM authorization, or put the function behind API Gateway or CloudFront, where you have an authorizer, WAF and throttling. A Function URL left open and forgotten gives direct access to the function's logic to anyone who discovers the address.
Secrets, dependencies and variables
Secrets do not belong in the code or in plaintext environment variables. Fetch them from Secrets Manager or Parameter Store at runtime, with the role allowed to read only the secrets the function needs. The problem with Lambda environment variables is that anyone with access to the function's configuration can see them, even with encryption at rest. In other words, they are not a place to keep credentials. For non-sensitive configuration they work fine, and you can encrypt them with your own KMS key instead of the default key.
- One role per function, with minimum permissions and named resources
- Who can invoke the function defined in the resource-based policy
- Function URL with IAM authorization, or the function behind API Gateway or CloudFront
- Secrets read from Secrets Manager or Parameter Store at runtime
- Sensitive credentials kept out of environment variables
- Dependencies and layers kept up to date and scanned for vulnerabilities
- Timeout, memory and reserved concurrency sized for the function
Third-party dependencies and layers bring vulnerabilities too. Scan and update them regularly, because a vulnerable library inside an exposed function is a concrete attack path. Also consider code signing: with it, only signed artifacts can be deployed, which makes it harder to inject malicious code through the deployment pipeline.
Event validation and attack surface
Everything that reaches the function through the event is untrusted input, even when it comes from an AWS service. Think about the common cases. An API Gateway event carries a body, headers and parameters that the client controls. A queue message may have been put there by a compromised producer. An S3 event carries object names that a third party may have influenced. Arriving through the platform does not make the data trustworthy, and all of it needs validation.
The classic flaws still apply in serverless. There is injection when event data goes into a query or a command without sanitization, insecure deserialization of payloads and path traversal when file names are built. Validate the event schema and handle errors explicitly. Be careful with temporary storage too: it can persist between warm invocations of the same function, so do not leave sensitive data there from one execution to the next.
Exposure, logs and network
If the function is exposed through API Gateway, all the authentication and authorization controls from the API Gateway guide apply. Enable CloudWatch logs for each function and monitor errors and anomalous invocations, such as unusual spikes, which can indicate abuse. And abuse costs money: a public trigger with no limit can be invoked en masse and run up an unexpected bill (what is called denial of wallet). Reserved concurrency and throttling at the edge, therefore, also protect the budget.
When the function needs to reach a private resource, such as a database, place it in the right VPC with restricted security groups instead of exposing the resource. Inside the VPC, control outbound internet access with the usual care, because a compromised function with unrestricted egress can exfiltrate data. GuardDuty has a protection feature specific to Lambda that helps detect suspicious network communication coming from functions.
Practical checklist
- Give each function its own execution role, with least privilege and named resources
- Get rid of broad roles shared by many functions
- Restrict who can invoke the function in the resource-based policy
- Replace open Function URLs with IAM authorization, API Gateway or CloudFront
- Read secrets from Secrets Manager or Parameter Store at runtime
- Move credentials out of environment variables
- Validate the schema and content of every event, treated as untrusted input
- Scan and update dependencies and layers, and consider code signing
- Tune timeout, memory and reserved concurrency
- Require authentication and authorization when the function is exposed through API Gateway
- Turn on CloudWatch logs and monitor anomalies and invocation cost
- Place functions that access private resources in the VPC, with restricted security groups
- Review function permissions and triggers periodically
Best practices
- Model permissions function by function instead of using one role for all of them
- Control what the function does and also who can invoke it
- Keep secrets out of the code and out of environment variables
- Always validate event data, because it is untrusted input
- Count dependencies as part of the attack surface
- Apply the API Gateway controls when the function is exposed through it
- Track each function's logs and metrics, and watch what an abuse would cost
- Reach private resources through the VPC, with restricted security groups
Common mistakes
- Broad role reused across many functions
- Compromising one simple function is enough to reach sensitive resources.
- Function URL left open and forgotten
- Anyone on the internet can trigger the function's logic, with no authentication at all.
- Secrets in plaintext environment variables
- Anyone who can see the function's configuration also sees the credentials.
- Trusting event data without validating it
- The function is open to injection, insecure deserialization and path traversal, like any application.
- Outdated dependencies and layers
- A vulnerable library in an exposed function becomes an attack path.
- Public trigger with no concurrency limit
- Mass invocations overload the dependencies and show up on the bill as an unexpected cost.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.