Neil Kuan
May 17, 2020

AWS S3 Private endpoint

Posted on May 17, 2020  •  2 minutes  • 237 words

原文:https://aws.amazon.com/tw/premiumsupport/knowledge-center/s3-private-connection-no-authentication/

S3 policy 產生網站:https://awspolicygen.s3.amazonaws.com/policygen.html

需要大量的 object 頻繁的上傳到 s3 ,又不想走外部網路時做的方式

VPC 對S3 的endpoint 傳輸吧!!!

Need S3 , EC2 , VPC , endpoint , route table.

  1. Open Amazon VPC console .

  2. 使用預設的VPC 即可,region 由你決定,但是VPC region 要和 S3 region 相同。

    這邊先幫我記下 VPC 的 ID 等等較好操作。

  3. Create Endpoint. → Virtual Private Cloud > Endpoints > cretae endpoint

  4. 點選項目 AWS Service → Service Name 搜尋 S3 → VPC 選取剛剛記下的 VPC ID.

    由於我選擇 region 為 Tokyo 所以 ,service name 為 com.amazonaws.ap-northeast-1.s3

  5. 設定 route tables,選取VPC 內要與S3 做private endpoint 的 Route Tables

  6. 設定 Policy, 為endpoint 設上 IAM Policy → 並打上 Tag. → 按下 Create endpoint

確認以關聯 endpoint 與 Route Table ID。 (並將Endpoint ID 記下來)

查看 已關聯 route table → route . 看到 vpce-xxxxxxx destination 是指定的 region .

設定 s3 bucket policy

  1. Open Amazon S3 console .
  2. Create S3 (s3 名稱需符合 dns 解析,以及選取與剛剛建立 VPC endpoint 相同 Region)→ Create

點選 剛剛建立好了 s3 → Permissions → Bucket Policy (設定Bucket Policy)

官方提供建立 policy web site. https://awspolicygen.s3.amazonaws.com/policygen.html

#將 BUCKETARN    arn:aws:s3:::$BUCKETARN/*
#   VPCE   aws:sourceVpce": "$VPCE
# 換成你的
{
    "Version": "2012-10-17",
    "Id": "Policy1415115909152",
    "Statement": [
        {
            "Sid": "Access-to-specific-VPCE-only",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::$BUCKETARN/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:sourceVpce": "$VPCE"
                }
            }
        }
    ]
}

save 存檔

開啟一台 ec2 在 剛剛的 vpc 內進行測試。

#可以使用 awscli 進行測試。
aws s3 cp $object s3://$your-bucket-name/

aws s3 cp  s3://$your-bucket-name/$object $objectname

下圖為,為尚未設定endpoint 與 s3 bucket policy 上傳object 結果。

下圖為,為設定endpoint 與 s3 bucket policy 上傳object 結果。

也可去 s3 console 查看。

2020年5月17日 Neil Kuan

Follow me

Here's where I hang out in social media