Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting S3 bucket access to a VPC

I am trying to apply the following policy in order to restrict my_bucket's access to a particular VPC.

When I try to apply this as a bucket policy, I get an Policy has an invalid condition key - ec2:Vpc.

How do I correct this?

{    "Version": "2012-10-17",    "Statement": [       {          "Effect": "Deny",          "Principal": {             "AWS": "*"          },          "Action": "*",          "Resource": "arn:aws:s3:::my_bucket/*",          "Condition":{             "StringNotEquals": {                "ec2:Vpc": "arn:aws:ec2:region:account:vpc/vpc-ccccccc"             }          }       }    ] } 
like image 448
Lelouch Lamperouge Avatar asked Aug 28 '14 00:08

Lelouch Lamperouge


People also ask

How do I deny access to an S3 bucket?

You can use the NotPrincipal element of an IAM or S3 bucket policy to limit resource access to a specific set of users. This element allows you to block all users who are not defined in its value array, even if they have an Allow in their own IAM user policies.

How do I restrict access to an S3 bucket by IP address?

To allow users to perform S3 actions on the bucket from the VPC endpoints or IP addresses, you must explicitly allow the user-level permissions. You can explicitly allow user-level permissions on either an AWS Identity and Access Management (IAM) policy or another statement in the bucket policy.

Do S3 buckets go in a VPC?

Many customers own multiple Amazon S3 buckets, some of which are accessed by applications running in VPCs. Amazon Virtual Private Cloud (Amazon VPC) enables you to launch AWS resources into a virtual network that you define.

Which of the below allows you to restrict access to an entire S3 bucket?

Amazon S3 is the only object storage service that allows you to block public access to all of your objects at the bucket or the account level, now and in the future by using S3 Block Public Access. To ensure that public access to all your S3 buckets and objects is blocked, turn on block all public access.


1 Answers

I just got this to work. I had to do two things. 1) Create the bucket policy on the S3 bucket, 2) create a "VPC Endpoint"

My S3 bucket policy looks like this (of course put in your bucket name and VPC identifier):

{     "Version": "2012-10-17",     "Id": "Policy1234567890123",     "Statement": [         {             "Sid": "Stmt1234567890123",             "Effect": "Allow",             "Principal": "*",             "Action": "s3:*",             "Resource": "arn:aws:s3:::my_bucket/*",             "Condition": {                 "StringEquals": {                     "aws:sourceVpc": "vpc-12345678"                 }             }         }     ] } 

The S3 bucket also has some permissions outside the bucket policy to allow access from the AWS Console. Doing the above did not give access. To get access, I also had to go to AWS Console -> VPC -> Endpoints, and then create an endpoint. I attached the newly created endpoint to the only routing policy the account has at the moment (that has all subnets attached to it) and I used the default policy of

{     "Statement": [         {             "Action": "*",             "Effect": "Allow",             "Resource": "*",             "Principal": "*"         }     ] } 

Once I created the endpoint, I was able to read from the S3 bucket from any EC2 instance in my VPC simply using wget with the right URL. I am still able to access the bucket from the AWS Console. But if I try to access the URL from outside the VPC, I get 403 forbidden. Thus, access to the S3 bucket is restricted to a single VPC, just like what you are looking for.

This is apparently a new feature. See this AWS blog entry for more information.

like image 156
Eddie Avatar answered Sep 28 '22 08:09

Eddie