From Specifying Conditions in a Policy - Amazon Simple Storage Service:
The new condition keys aws:sourceVpce
and aws:sourceVpc
are used in bucket policies for VPC endpoints.
Therefore, you need to be accessing the S3 bucket via a VPC Endpoint to be able to restrict access to a VPC. This is because, without the VPC Endpoint, the request being received by Amazon S3 simply appears to be coming "from the Internet", so it is not able to identify the source VPC. In difference, a request coming via a VPC Endpoint includes an identifier of the source VPC.
Making it work
Assumption: You already have an IAM Policy on your user(s) that allow access to the bucket. You are wanting to know how to further restrict the bucket so that it is only accessible from a specific VPC. If this is not the case, then you should be using an Allow
policy to grant access to the bucket, since access is denied by default.
To reproduce your situation, I did the following:
- Created a new VPC with a public subnet
- Added a VPC Endpoint to the VPC
- Launched an Amazon EC2 instance in the public subnet, assigning an IAM Role that already has permission to access all of my Amazon S3 buckets
- Created an Amazon S3 bucket (
my-vpc-only-bucket
)
- Added a Bucket Policy to the bucket (from Example Bucket Policies for VPC Endpoints for Amazon S3):
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Access-to-specific-VPC-only",
"Principal": "*",
"Action": "s3:*",
"Effect": "Deny",
"Resource": ["arn:aws:s3:::my-vpc-only-bucket",
"arn:aws:s3:::my-vpc-only-bucket/*"],
"Condition": {
"StringNotEquals": {
"aws:sourceVpc": "vpc-111bbb22"
}
}
}
]
}
Please note that this policy assumes that the user(s) already have access to the bucket via an IAM Policy that grants Allow
access. This policy is adding a Deny
that will override the access they already have to the bucket.
- Logged in to the Amazon EC2 instance in the new VPC and then:
- Run
aws s3 ls s3://my-vpc-only-bucket
- It worked!
- From my own computer on the Internet:
- Run
aws s3 ls s3://my-vpc-only-bucket
- Received a
AccessDenied
error (which is what we want!)
By the way, the Deny policy will also prohibit your use of the Amazon S3 management console to manage the bucket because requests are not coming from the VPC. This is a side-effect of using Deny
and s3:*
on the bucket. You can always remove the bucket policy by using your root credentials (login via email address), then go to the Bucket Policy in the S3 console and click Delete. (You'll see some errors on the screen getting to the Bucket Policy, but it will work.)
Alternate method via Allow
If, on the other hand, the user(s) do not already have access to all Amazon S3 buckets, then by default they will not have access to the new bucket. Thus, you will need to grant Allow
access to the bucket, but only from the VPC via the VPC Endpoint.
Setup is the same as above, but the Bucket Policy would be:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Access-to-specific-VPC-only",
"Principal": "*",
"Action": "s3:*",
"Effect": "Allow", <-- This changed
"Resource": ["arn:aws:s3:::my-vpc-only-bucket",
"arn:aws:s3:::my-vpc-only-bucket/*"],
"Condition": {
"StringEquals": { <--- This changed
"aws:sourceVpc": "vpc-111bbb22"
}
}
}
]
}
- I then tested it with an IAM Role assigned to the EC2 instance that does not have any permissions to access Amazon S3
- Ran
aws s3 ls s3://my-vpc-only-bucket
- Ran from my own computer, using an IAM User that does not have any permissions to access Amazon S3
- Received a
AccessDenied
error (which is what we want!)
Bottom line: You need to add a VPC Endpoint to the VPC.