Skip to content
IAM Policy Generator

IAM policy example: let EC2 assume a role, with a least privilege rewrite

Lets the EC2 service assume a role so an instance profile can hand temporary credentials to an instance. The original also trusts every principal in account 111122223333, so any user or role there can assume the role. The rewrite keeps only the service principal.

Open the workbench ยท All examples

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Ec2AndAccount",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com",
        "AWS": "arn:aws:iam::111122223333:root"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

What it allows

  • Allows the EC2 service to assume the role.
  • Allows every principal in account 111122223333 whose own policy permits sts:AssumeRole to assume the role.

What it allows

  1. Allow Service ec2.amazonaws.com, AWS arn:aws:iam::111122223333:root to sts:AssumeRole.

    Action or patternActionsAccess levels
    sts:AssumeRole1Write 1

Checks

  • High Trusts arn:aws:iam::111122223333:root with no condition (for example sts:ExternalId for a third party, or aws:PrincipalOrgID): any principal the trusted account allows can assume this role. AWS documentation for this check
  • Low Trusts ec2.amazonaws.com with no aws:SourceArn or aws:SourceAccount condition, which AWS recommends against the confused deputy problem. AWS documentation for this check

The account root principal makes the role assumable by any identity in the account that has sts:AssumeRole.

Least privilege rewrite

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Ec2Only",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

The rewrite removes the account principal. Only the EC2 service can assume the role, and a person who needs it gets a separate role.

What it allows

  1. Allow Service ec2.amazonaws.com to sts:AssumeRole.

    Action or patternActionsAccess levels
    sts:AssumeRole1Write 1

Checks

  • Low Trusts ec2.amazonaws.com with no aws:SourceArn or aws:SourceAccount condition, which AWS recommends against the confused deputy problem. AWS documentation for this check

Sources

  1. IAM User Guide: create a role for an AWS service, read 16 Nov 2026
  2. IAM User Guide: create a role to delegate permissions to an IAM user, read 16 Nov 2026
  3. AWS Service Authorization Reference, programmatic access (fetched 16 Nov 2026), read 16 Nov 2026

Last reviewed 16 Nov 2026.