Skip to content

AWS Lambda security: permissions, secrets, logs and safe execution

Serverless does not take security off your plate. See how to restrict permissions, secrets and exposure in Lambda functions.

Reading time
9 min
Published

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.

Execution policy snippet restricted to a specific queue
{  "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.

Frequently asked questions

Is serverless more secure by default?
Serverless shrinks the infrastructure surface, since you do not manage servers, but application security is still yours. Role permissions, secrets, event validation, dependencies and exposure remain under your control, and that is exactly where the risks concentrate. Serverless changes what you manage. The need for least privilege and good practices stays the same.
Can I use environment variables for secrets in Lambda?
Not for sensitive credentials. Anyone with access to the function's configuration can see its environment variables, so for whoever administers the account they are practically plaintext. The right approach is to fetch the secret from Secrets Manager or Parameter Store at runtime, with the role allowed to read only what it needs. That way the value is never stored in the function's configuration.
Why one role per function instead of a shared role?
Because with a broad shared role, compromising any function, even the most trivial one, grants access to everything the role allows. When each function has a role with minimal, specific permissions, the blast radius shrinks: a problem in one function only reaches the few resources it actually uses.
Do I need to validate the data that arrives in the event?
Yes. Event data is untrusted input even when it arrives through an AWS service, because it often originated in a client, in a queue producer or in an object that a third party influenced. Injection, insecure deserialization and path traversal still happen in serverless. Validate the schema, sanitize the content and handle errors explicitly.
Do I need to put my Lambda function in a VPC?
Only when it needs to access private resources, such as a database in a private subnet. In that case, put the function in the VPC with restricted security groups instead of exposing the resource. If the function does not access anything private, keeping it out of the VPC makes operations simpler. In other words, what the function needs to reach is what decides.
Can a public function generate unexpected costs?
Yes. A public trigger with no limit, such as an open Function URL or an endpoint without throttling, accepts mass invocations. They overload the dependencies and run up costs, a risk sometimes called denial of wallet. Set reserved concurrency on the function and throttling at the edge, and track invocation volume so you notice anomalous spikes early.

When to bring in specialist help

Reviewing the permissions, secrets and exposure of serverless functions is part of GUARDIASEC's Application Security and AWS and Cloud Security services. The assessment looks at the serverless environment from the angle of least privilege and the real risk of each function.