Securing Lambda Permissions and Secrets
This lesson explains the two policies that govern a function's security, and how secrets reach it without being stored in code or plaintext configuration.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish the execution role from the resource-based policy.
- Describe how configuration and secrets reach a function securely.
- Diagnose access errors from either policy.
Execution role and resource-based policy
Two policies govern a Lambda function's security, one for each direction. The execution role is an IAM role that Lambda assumes when it runs the function, and its permissions decide what the function's code can do, such as reading from DynamoDB and writing logs. If the function receives AccessDenied when calling a service, the execution role is missing a permission.
The resource-based policy is attached to the function and decides who may invoke it, such as S3 for a particular bucket, an SNS topic, API Gateway or another account. If a trigger is configured but the function never runs, this policy usually lacks a statement for it. The following table summarises the difference.
Event source mappings for SQS, Kinesis and DynamoDB Streams read the source with the execution role, so those read permissions belong in the role.
Execution role or resource policy?
Configuration and secrets
Lambda functions receive configuration through environment variables, which are encrypted at rest with AWS KMS. Secrets, such as database passwords and API keys, should not be stored in environment variables, where anyone who can view the function's configuration can read them. They belong in AWS Secrets Manager or Parameter Store, read at run time and cached, for example with the AWS Parameters and Secrets Lambda Extension.
For the code itself, Lambda code signing ensures that only code signed by trusted publishers can be deployed to a function, which protects against tampered deployment packages.
Two access errors on the same day
Lambda access errors arise from either the resource-based policy or the execution role. The following table identifies the cause and remedy for two such errors.
| Error | Cause | Fix |
|---|---|---|
| S3 uploads do not trigger the function | S3 is not allowed to invoke it | Add a resource-based policy statement for s3.amazonaws.com scoped to the bucket |
| The function cannot read a secret | The execution role lacks secretsmanager:GetSecretValue | Add the permission for that secret's ARN |
Who may invoke the function is decided by its resource-based policy; what the function may do is decided by its execution role. The secret is then cached by the Parameters and Secrets extension instead of being fetched on every invocation.
Where should a function keep a database password?
Show answer
In Secrets Manager, or a Parameter Store SecureString, read at runtime and cached, for example with the Parameters and Secrets Lambda Extension, rather than in a plain environment variable.
Exam Tips
Remember for the exam
- AccessDenied when the function calls another service: fix the execution role.
- A trigger that cannot invoke the function: fix the resource-based policy.
- Keep database passwords in Secrets Manager rather than in environment variables.
- To ensure only approved code runs in Lambda, use code signing.