Securing Serverless Functions and Their Invocation
This lesson explains where the serverless attack surface moves: function permissions, who can invoke functions, the events they receive, the secrets they use and the code they run.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish a function's execution role from its resource-based policy.
- Choose the API Gateway authorisation method that matches the caller.
- Describe secure practices for secrets, code and input handling.
Two policies per function
A Lambda function has two separate policies, controlling two different directions. The execution role controls what the function can do in AWS, such as reading a table or writing to a bucket. Least privilege means one role per function, scoped to the exact actions and resources it needs, so a compromised function cannot reach anything else. The resource-based policy controls who can invoke the function. Least privilege means allowing only the specific service, identified by aws:SourceArn and aws:SourceAccount, or specific accounts. The following table summarises the two policies.
| Policy | Controls | Least privilege means |
|---|---|---|
| Execution role | What the function can do in AWS | One role per function, scoped to exact resources and actions |
| Resource-based policy | Who can invoke the function | Allow only the specific service with aws:SourceArn and aws:SourceAccount, or specific accounts |
A resource policy that allows s3.amazonaws.com to invoke a function without a source ARN lets any bucket in any account trigger it. Function URLs with AuthType NONE are public endpoints; use AWS_IAM unless the function performs its own authentication.
The following diagram compares an upload-processing Lambda function's policies before and after a security review.

Execution role or resource-based policy?
Authorizing API calls
The way API Gateway authorises a call should match the caller. IAM authorisation (SigV4) suits service-to-service and internal callers that already have AWS credentials. A Cognito user pool authorizer or JWT authorizer suits end users signed in through Cognito or another OIDC provider. A Lambda authorizer handles everything else: custom tokens, keys issued by an external system, or fine-grained decisions, for example by calling Verified Permissions.
A resource policy restricts REST APIs by source VPC endpoint, IP range or account, and private APIs require one. Mutual TLS on a custom domain suits clients that authenticate with certificates; the default endpoint must be disabled, or callers could bypass the check by using it. The following table summarises the options.
| Option | Use |
|---|---|
| IAM authorisation (SigV4) | Service-to-service and internal callers with AWS credentials |
| Cognito user pool authorizer / JWT authorizer | End users signed in with Cognito or another OIDC provider |
| Lambda authorizer | Custom tokens, API keys from an external system, or fine-grained decisions (for example with Verified Permissions) |
| Resource policy | Restrict REST APIs by source VPC endpoint, IP range or account; required for private APIs |
| Mutual TLS on a custom domain | Clients that authenticate with certificates; disable the default endpoint so it cannot be bypassed |
API keys and usage plans are for metering and throttling, not authentication.
Choosing an authoriser
Secrets, code and runtime
Serverless functions must handle secrets, code and input securely. The following list describes the recommended practices.
- Read secrets at runtime from Secrets Manager or Parameter Store (the Parameters and Secrets Lambda extension caches them) rather than placing them in environment variables. If environment variables hold sensitive values, encrypt them with a customer managed KMS key.
- Code signing: a code signing configuration with AWS Signer profiles makes Lambda reject (Enforce) or warn about (Warn) packages not signed by a trusted publisher.
- Validate event input: data from S3 object keys, queue messages and API requests can carry injection payloads.
- Reserved concurrency limits the blast radius of a function being abused for cost or denial of wallet.
- GuardDuty Lambda Protection monitors function network activity for connections to known malicious hosts; Inspector scans dependencies and code.
Serverless protections
A security review finds a Lambda function that processes uploads. Its resource policy allows the principal s3.amazonaws.com with no conditions, and its execution role has AmazonS3FullAccess. Which changes reduce risk the most?
Add aws:SourceArn (the upload bucket) and aws:SourceAccount conditions to the resource policy, so only that bucket can trigger the function. Replace the managed policy with one allowing s3:GetObject on the upload bucket and s3:PutObject on the output prefix only.
Why is a function URL with AuthType NONE a risk?
Show answer
It is public: anyone with the URL can invoke the function. AWS_IAM requires signed requests.
Exam Tips
Remember for the exam
- Lambda security has two sides: the execution role and the resource-based invoke policy.
- Restrict service invocations with aws:SourceArn and aws:SourceAccount.
- Function URLs with AuthType NONE are public; prefer AWS_IAM.
- API keys are not authentication; use IAM, Cognito/JWT or Lambda authorizers.
- Code signing configurations with AWS Signer block unsigned deployment packages.
- Fetch secrets at runtime; encrypt sensitive environment variables with a customer managed key.