Description
aws_iam_role.assume_role_policy defines who can assume the role. Adding "AWS": "*" alongside the EC2 service principal extends trust to AWS callers beyond EC2.
Potential impact
An unintended caller that satisfies the permissions and trust conditions can use the role's permissions.
Remediation
Keep only the service principals the role needs. For cross-account use, name the approved accounts or roles and apply the required conditions. Test that the service works and unapproved requests are denied.
Examples
Set var.name_tag_prefix and configure an instance profile for this EC2 role.
Before
resource "aws_iam_role" "example" {
name = "${var.name_tag_prefix}-openshift-instance-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com",
"AWS": "*"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
Service and AWS name separate principals. The EC2 entry does not narrow the wildcard.
After
resource "aws_iam_role" "example" {
name = "${var.name_tag_prefix}-openshift-instance-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
This statement trusts only ec2.amazonaws.com. Limit the role's workload permissions and who can attach it to an instance.