Skip to content
IAM Policy Generator

IAM policy example: launch EC2 instances in one subnet, with a least privilege rewrite

Allows RunInstances only into one subnet, with the supporting resources the call also checks: the image, the instance, the network interface, the volume, the key pair and the security group. The AWS documentation example shows this subnet pattern. The original leaves the image, key pair and security group open.

Open the workbench ยท All examples

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "RunInSubnet",
      "Effect": "Allow",
      "Action": "ec2:RunInstances",
      "Resource": [
        "arn:aws:ec2:us-east-1:111122223333:subnet/subnet-1a2b3c4d",
        "arn:aws:ec2:us-east-1:111122223333:network-interface/*",
        "arn:aws:ec2:us-east-1:111122223333:instance/*",
        "arn:aws:ec2:us-east-1:111122223333:volume/*",
        "arn:aws:ec2:us-east-1::image/ami-*",
        "arn:aws:ec2:us-east-1:111122223333:key-pair/*",
        "arn:aws:ec2:us-east-1:111122223333:security-group/*"
      ]
    }
  ]
}

What it allows

  • Allows launching instances into subnet-1a2b3c4d, using any image, key pair and security group.

What it allows

  1. Allow 1 action (ec2:RunInstances) on arn:aws:ec2:us-east-1:111122223333:subnet/subnet-1a2b3c4d, arn:aws:ec2:us-east-1:111122223333:network-interface/*, arn:aws:ec2:us-east-1:111122223333:instance/*, arn:aws:ec2:us-east-1:111122223333:volume/*, arn:aws:ec2:us-east-1::image/ami-*, arn:aws:ec2:us-east-1:111122223333:key-pair/*, arn:aws:ec2:us-east-1:111122223333:security-group/*.

    Action or patternActionsAccess levels
    ec2:RunInstances1Write 1

Checks

No flags from the six checks.

The security group resource is a wildcard, so a launch can attach any group, including one that opens ports to the internet.

Least privilege rewrite

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "RunInSubnet",
      "Effect": "Allow",
      "Action": "ec2:RunInstances",
      "Resource": [
        "arn:aws:ec2:us-east-1:111122223333:subnet/subnet-1a2b3c4d",
        "arn:aws:ec2:us-east-1:111122223333:network-interface/*",
        "arn:aws:ec2:us-east-1:111122223333:instance/*",
        "arn:aws:ec2:us-east-1:111122223333:volume/*",
        "arn:aws:ec2:us-east-1::image/ami-0123456789abcdef0",
        "arn:aws:ec2:us-east-1:111122223333:key-pair/build-key",
        "arn:aws:ec2:us-east-1:111122223333:security-group/sg-0123456789abcdef0"
      ]
    }
  ]
}

The rewrite names one image, one key pair and one security group instead of wildcards. A launch can no longer use another image or group.

What it allows

  1. Allow 1 action (ec2:RunInstances) on arn:aws:ec2:us-east-1:111122223333:subnet/subnet-1a2b3c4d, arn:aws:ec2:us-east-1:111122223333:network-interface/*, arn:aws:ec2:us-east-1:111122223333:instance/*, arn:aws:ec2:us-east-1:111122223333:volume/*, arn:aws:ec2:us-east-1::image/ami-0123456789abcdef0, arn:aws:ec2:us-east-1:111122223333:key-pair/build-key, arn:aws:ec2:us-east-1:111122223333:security-group/sg-0123456789abcdef0.

    Action or patternActionsAccess levels
    ec2:RunInstances1Write 1

Checks

No flags from the six checks.

Sources

  1. IAM User Guide: EC2 instances in a specific subnet, read 16 Nov 2026
  2. AWS Service Authorization Reference, programmatic access (fetched 16 Nov 2026), read 16 Nov 2026

Last reviewed 16 Nov 2026.