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

Facts read 16 Nov 2026. Page reviewed 16 Nov 2026.

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.

```json
{
  "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.

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

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. (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_common-scenarios_third-party.html)
- low: Trusts ec2.amazonaws.com with no aws:SourceArn or aws:SourceAccount condition, which AWS recommends against the confused deputy problem. (https://docs.aws.amazon.com/IAM/latest/UserGuide/confused-deputy.html)

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

## Least privilege rewrite

```json
{
  "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.

- Allows Service ec2.amazonaws.com to sts:AssumeRole.

Checks:
- low: Trusts ec2.amazonaws.com with no aws:SourceArn or aws:SourceAccount condition, which AWS recommends against the confused deputy problem. (https://docs.aws.amazon.com/IAM/latest/UserGuide/confused-deputy.html)

- [Paste it into the workbench](/)
- [Every STS action and its access level](/actions/sts/)

## Related pages

- [IAM policy example: let Lambda assume an execution role](/examples/trust-policy-lambda-execution-role/)
- [IAM policy example: cross-account role with external ID](/examples/trust-policy-cross-account-external-id/)
- [IAM policy example: trust GitHub Actions through OIDC](/examples/trust-policy-github-actions-oidc/)
- [All of them](/examples/)

## Sources

- IAM User Guide: create a role for an AWS service: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html (read 16 Nov 2026)
- IAM User Guide: create a role to delegate permissions to an IAM user: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html (read 16 Nov 2026)
- AWS Service Authorization Reference, programmatic access (fetched 16 Nov 2026): https://docs.aws.amazon.com/service-authorization/latest/reference/service-reference.html (read 16 Nov 2026)

Last reviewed 16 Nov 2026. Canonical: https://iampolicygenerator.com/examples/trust-policy-ec2-instance-role/

This site is not affiliated with Amazon Web Services. AWS service and product names are used only to describe AWS services.
