A gateway VPC endpoint lets instances in a VPC reach S3 without going through the Internet Gateway. Combined with the aws:sourceVpce condition key in a bucket policy, you can make sure objects are only accessible from your VPC.
S3-Endpoint and keep AWS services selected.
s3 and select com.amazonaws.
SID-vpc. For now, do not select any route table – you will add it later to see the difference. Keep Full access for the endpoint policy and choose Create endpoint.
vpce-...).
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyOutsideVpce",
"Action": "s3:*",
"Effect": "Deny",
"Principal": "*",
"Resource": "arn:aws:s3:::<your-bucket-name>/*",
"Condition": {
"StringNotEquals": {
"aws:sourceVpce": "<your-vpc-endpoint-id>"
}
}
}
]
}

aws s3api head-object --key app1/file1 --bucket $bucket
The request fails with 403 Forbidden: the subnet route table has no route to the endpoint, so the traffic still leaves through the Internet Gateway and does not carry the endpoint ID.

S3-Endpoint → Actions → Manage route tables.
SID-routes and choose Modify route tables.

This policy also denies object access from the S3 console and from anywhere outside the VPC – including you. Delete it (or the whole stack) when you are done with the lab.