0
0

VPCエンドポイント経由でS3にアクセスしてみる

Posted at

今回やりたいこと

- VPCエンドポイント経由でS3にアクセス
- VPCエンドポイントにポリシーを設定
- S3にバケットポリシーを設定
- セッションマネージャーを使用してS3のオブジェクトをダウンロード

image.png

S3用のエンドポイントの作成

image.png

ポリシーも作成

json
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Principal": "*",
			"Action": "s3:GetObject",
			"Resource": "arn:aws:s3:::vpc-endpoint-test-bucket2/*",
			"Condition": {
				"IpAddress": {
					"aws:SourceIp": "10.0.0.13"
				}
			}
		}
	]
}

Resourceで指定したバケットに対してGetObjectを許可しています。
Conditionでこのエンドポイント経由するEC2のプライベートIPを指定しています。

ルートテーブルにエンドポイントを追加

image.png

エンドポイントと追加方法とIAMのロール、ポリシーの設定方法については解説しません。
申し訳ございませんが、他の方の記事を参考にしてください。

S3のバケットポリシーの作成

json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::vpc-endpoint-test-bucket2/*",
            "Condition": {
                "StringEquals": {
                    "aws:sourceVpce": "vpce-004cca88eeb224ebf"
                }
            }
        }
    ]
}

Conditionで先ほど作成したVPCエンドポイントの通信を許可するように設定しています。

S3のオブジェクトをダウンロードできるか試してみる

sh-5.2$ aws s3 cp s3://vpc-endpoint-test-bucket2/test.txt /home/ssm-user/test
fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden

403エラーなので権限がない
たぶんS3のACL関係??(調査中)

とりあえずVPCエンドポイントのポリシーのコンディションを削除したらできた

json
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Principal": "*",
			"Action": "s3:GetObject",
			"Resource": "arn:aws:s3:::vpc-endpoint-test-bucket2/*"
		}
	]
}

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0