When you create an interface or gateway endpoint, you can attach an endpoint policy to it that controls access to the service to which you are connecting. A VPC endpoint policy is an IAM resource policy that you attach to an endpoint. If you do not attach a policy when you create an endpoint, AWS attaches a default policy for you that allows full access to the service through the endpoint.
You can create a policy that restricts access to specific S3 buckets only. This is useful if you only want certain S3 Buckets to be accessible through the endpoint.
In this section you will create a VPC endpoint policy that restricts access to the S3 bucket specified in the VPC endpoint policy.
aws s3 ls s3://\<your-bucket-name\>
The bucket contents include the two 1 GB files uploaded in earlier.
Successfully create bucket
The default policy allows access to all S3 Buckets through the VPC endpoint.
{
"Id": "Policy1631305502445",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1631305501021",
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::yourbucketname-2",
"arn:aws:s3:::yourbucketname-2/*"
],
"Principal": "*"
}
]
}
Successfully customize policy
aws s3 ls s3://<yourbucketname>
This command will return an error because access to this bucket is not permitted by your new VPC endpoint policy:
cd~
fallocate -l 1G test-bucket2.xyz
aws s3 cp test-bucket2.xyz s3://<your-2nd-bucket-name>
This operation succeeds because it is permitted by the VPC endpoint policy.
aws s3 cp test-bucket2.xyz s3://<your-1st-bucket-name>
This command will return an error because access to this bucket is not permitted by your new VPC endpoint policy.
In this section, you created a VPC endpoint policy for Amazon S3, and used the AWS CLI to test the policy. AWS CLI actions targeted to your original S3 bucket failed because you applied a policy that only allowed access to the second bucket you created. AWS CLI actions targeted for your second bucket succeeded because the policy allowed them. These policies can be useful in situations where you need to control access to resources through VPC endpoints.