# S3 bucket policy: restrict access to a VPC endpoint

IAM policy examples · Updated 17 November 2026

**Short answer:** Add a Deny for s3:* on the bucket and its objects when aws:SourceVpce is not your endpoint ID, and exclude an admin role with aws:PrincipalArn so the bucket can still be managed. Requests from the internet, the console and other VPCs are then refused even if IAM allows them.

## The policy

```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyUnlessFromVpce",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::my-bucket",
        "arn:aws:s3:::my-bucket/*"
      ],
      "Condition": {
        "StringNotEquals": {
          "aws:SourceVpce": "vpce-0123456789abcdef0"
        },
        "ArnNotLike": {
          "aws:PrincipalArn": "arn:aws:iam::123456789012:role/BucketAdmin"
        }
      }
    }
  ]
}
```

**Warning:** without the `ArnNotLike` exception, this policy also blocks you from editing or deleting the bucket policy from the console. Recovering then needs the account root user, which can always delete a bucket policy.

## How it works

- **`aws:SourceVpce`** is the ID of the VPC endpoint the request came through. It exists for both gateway and interface endpoints for S3. For requests that don't use an endpoint, the key is absent, and `StringNotEquals` on a missing key evaluates to true, so they are denied.
- **Two conditions in one block are ANDed.** The deny applies only when the request is not from the endpoint *and* the caller is not `BucketAdmin`.
- Because this is a **Deny**, it doesn't grant anything. Callers inside the VPC still need an IAM allow such as [read-only bucket access](https://cloudconsole.dev/iam/s3-read-only-one-bucket).

## The other half: an endpoint policy

The bucket policy controls who reaches the bucket. The endpoint policy controls where traffic through the endpoint may go, which stops a compromised instance copying data to an attacker's bucket. A gateway endpoint policy limited to your bucket:

```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "OnlyMyBucket",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::my-bucket",
        "arn:aws:s3:::my-bucket/*"
      ]
    }
  ]
}
```

```
aws ec2 modify-vpc-endpoint --vpc-endpoint-id vpce-0123456789abcdef0 \
  --policy-document file://endpoint-policy.json
```

Be careful: instances that pull packages or container layers from S3-backed repositories (Amazon Linux repos, ECR layers) also go through the gateway endpoint. Add those buckets or they will fail to update.

## Variations

- **Any endpoint in a VPC**: use `aws:SourceVpc` with the VPC ID instead of one endpoint ID.
- **Allow AWS services to write** (for example log delivery): add `"Bool": {"aws:PrincipalIsAWSService": "false"}` to the deny's conditions so service principals are not caught.
- **Organisation-wide**: a resource control policy can apply the same perimeter to many buckets.

## Find your endpoint ID

```
aws ec2 describe-vpc-endpoints \
  --filters Name=service-name,Values=com.amazonaws.eu-west-2.s3 \
  --query 'VpcEndpoints[].[VpcEndpointId,VpcId,VpcEndpointType]' --output table
```

## Common mistakes

- **Using `aws:SourceIp` with private addresses.** Requests through a VPC endpoint carry no public source IP; use `aws:SourceVpce`, `aws:SourceVpc` or `aws:VpcSourceIp`.
- **Testing from the console.** The console runs in your browser, not in the VPC, so it is denied unless you are the excepted role. That is the policy working.
- **Endpoint in the wrong region.** Gateway endpoints only reach buckets in the same region; cross-region traffic goes over the internet path and is denied.

## Questions

### How do I restrict an S3 bucket to a VPC?

Deny s3:* in the bucket policy when aws:SourceVpce (one endpoint) or aws:SourceVpc (any endpoint in that VPC) does not match, keeping an exception for an admin role.

### I locked myself out of my bucket with a VPC endpoint policy. How do I fix it?

Sign in as the account root user and delete the bucket policy, then re-add it with an aws:PrincipalArn exception for an admin role.

## When this happens away from your desk

A data-perimeter policy blocks the console by design, so checking a file from your phone will be denied too. That is worth knowing before you are paged about a missing object.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [Controlling access from VPC endpoints with bucket policies](https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies-vpc-endpoint.html)
- [AWS global condition context keys](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)

## Related

- [S3 bucket policy vs IAM policy](https://cloudconsole.dev/iam/s3-bucket-policy-vs-iam-policy)
- [IAM policy: deny requests from outside approved IP addresses](https://cloudconsole.dev/iam/restrict-by-source-ip)
- [S3 bucket policy: deny requests that are not over HTTPS](https://cloudconsole.dev/iam/s3-deny-http-requests)
- [aws s3api put-bucket-policy](https://cloudconsole.dev/cli/s3api-put-bucket-policy)
- [S3 AccessDenied: Access Denied when calling an S3 operation](https://cloudconsole.dev/errors/s3-access-denied)
- [ABAC: tag-based access control policy example](https://cloudconsole.dev/iam/abac-tags)

---

Source: https://cloudconsole.dev/iam/s3-restrict-to-vpc-endpoint

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
