Should trust policies for AWS Lambda be narrowly scoped, and how can this be implemented (e.g. using Terraform)?
Using Terraform I ran into a cyclic dependency problem as follows: an AWS Lambda function resource must specify an IAM role. An IAM role must specify an assume role (trust) policy. I wanted to put a condition in the trust policy that would restrict use of that role to this specific Lambda, like:
data "aws_iam_policy_document" "trust_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "lambda:FunctionArn"
values = [aws_lambda_function.myfunction.arn]
} } }
This creates a cyclic reference, and hence a Terraform error. (Frankly, it seems like this error shouldn't be a problem, as one can predict the ARNs for both the Lambda function and the IAM role before they have been created, and even if that weren't true, it ought also be possible to initially create the resources without the cyclic reference and then update the configuration to achieve the desired state.)
I presume it isn't a common practice to include this sort of condition. Is there some reason why one wouldn't want to narrowly scope the trust policy (and prevent another lambda, say one that has been hijacked via some vulnerability, from assuming the role intended for this lambda and thereby escalating privileges)?