Lưu ý khi configure AWS S3 bucket policy

Bài toán:

Khi đi ra bên ngoài khỏi EC2 của private subnet thông qua NATGateway, thì IP nguồn kết nối sẽ là EIP đã được attach vào NAT Gateway.
Để các request từ EC2 instance bên trong private subnet được attach với NAT Gateway mới có thể thực hiện hành động GetObject bằng bucket policy như sau:

{

    "Version": "2012-10-17",

    "Statement": [

        {

            "Sid": "PublicReadGetObject",

            "Effect": "Allow",

            "Principal": "*",

            "Action": "s3:GetObject",

            "Resource": "arn:aws:s3:::<bucket-name>/*",

            "Condition": {

                "IpAddress": {

                    "aws:SourceIp": [

                    "xxx.xxx.xx.x/32"

                    ]

                }

            }

        }

    ]

}

với aws:SourceIp là giá trị EIP của NAT gateway.

Đúng hay sai?

Trả lời:

  1. “Khi đi ra bên ngoài khỏi EC2 của private subnet thông qua NATGateway, thì IP nguồn kết nối sẽ là EIP đã được attach vào NAT Gateway. ” => điều này đúng!

  2. “Bằng bucket policy như sau”:

  • Đúng nếu như bạn không đồng thời sử dụng S3 endpoint (gateway type) cho VPC chứa instance bạn thực hiện câu lệnh curl <s3-website-enpoint>

  • Sai nếu như bạn có đồng thời sử dụng S3 endpoint (gateway type) vì nếu sử dụng S3 endpoint (gateway type) thì AWS sẽ ưu tiên routing đến S3 endpoint thay vì NAT gateway. Chi tiết tham khảo thêm trong tài liệu [1] => Trong TH này bạn sẽ phải configure policy theo điều kiện aws: sourceVpce để restrict theo S3 endpoint thay vì NAT gateway EIP.

Tài liệu tham khảo:

[1][Gateway endpoints | Routing]

https://docs.aws.amazon.com/vpc/latest/privatelink/gateway-endpoints.html#gateway-endpoint-routing

f there is a route that sends all internet traffic (0.0.0.0/0) to an internet gateway, the endpoint route takes precedence for traffic destined for the service (Amazon S3 or DynamoDB) in the current Region. Traffic destined for a different AWS service uses the internet gateway.

[2] [Gateway endpoints for Amazon S3 | Control access using bucket policies]

https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-s3.html#bucket-policies-s3

You can create a bucket policy that restricts access to a specific endpoint by using the aws:sourceVpce condition key.