Lesson 6 of 6 Compute Security: EC2, Containers and Lambda

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.

PolicyControlsLeast privilege means
Execution roleWhat the function can do in AWSOne role per function, scoped to exact resources and actions
Resource-based policyWho can invoke the functionAllow 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.

Reducing the risk of an upload-processing function. The resource policy allowed s3.amazonaws.com with no conditions, so any bucket could invoke it; adding aws:SourceArn and aws:SourceAccount limits it to the upload bucket. The execution role's AmazonS3FullAccess becomes GetObject on the upload bucket and PutObject on the output prefix.
The scenario is illustrative.
Sort into groups

Execution role or resource-based policy?

Exam skills 3.2.2

Decide which Lambda policy controls each permission.

Log in to try this activity Interactive activities are free with an account.

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.

OptionUse
IAM authorisation (SigV4)Service-to-service and internal callers with AWS credentials
Cognito user pool authorizer / JWT authorizerEnd users signed in with Cognito or another OIDC provider
Lambda authorizerCustom tokens, API keys from an external system, or fine-grained decisions (for example with Verified Permissions)
Resource policyRestrict REST APIs by source VPC endpoint, IP range or account; required for private APIs
Mutual TLS on a custom domainClients 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.

Scenario questions

Choosing an authoriser

Exam skills 4.2.1 · 3.1.2

Choose the API Gateway authorisation option for each caller.

Log in to try this activity Interactive activities are free with an account.

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.
Match the pairs

Serverless protections

Exam skills 3.2.6 · 3.2.3

Match each serverless risk with the control that addresses it.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.