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

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

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.

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

- Allows 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/*.

Checks:
- No flags.

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

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

- Allows 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.

Checks:
- No flags.

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

## Related pages

- [IAM policy example: snapshot tagged EBS volumes](/examples/ec2-create-snapshots-tagged-volumes/)
- [IAM policy example: invoke one Lambda function](/examples/lambda-invoke-one-function/)
- [IAM policy example: deploy code to one Lambda function](/examples/lambda-deploy-one-function/)
- [All of them](/examples/)

## Sources

- IAM User Guide: EC2 instances in a specific subnet: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_ec2_instances-subnet.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/ec2-run-instances-in-one-subnet/

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