0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EC2からS3にアクセスできなかった件について

Posted at

警告
小生、AWS初心者どころか、IT初心者のため内容に誤りがあるかもしれないです。

はじめに

S3バケット内にファイルをアップロードし、EC2インスタンスのセッションマネージャからアクセスしようと試みました。

しかし、ファイルのリストを表示しようとした際に、権限不足のエラーに遭遇したため、解決策を備忘録として残します。

問題

S3バケットにアクセスするためのIAMロールの設定が不十分だったため、以下のエラーが表示されました。

  • 実行コマンド
$ aws s3 ls s3://test-backet/
  • エラーメッセージ
An error occurred (AccessDenied) when calling the ListObjectsV2 operation: User: arn:aws:sts::412235697174:assumed-role/Only-SSM-Allow/i-0f5f93f33646cef3c is not authorized to perform: s3:ListBucket on resource: "arn:aws:s3:::test-backet" because no identity-based policy allows the s3:ListBucket action

解決方法

IAMロールにS3へのアクセス権限を付与すれば良い。
全てのS3バケットへのアクセス権限を付与する方法(①)と、指定バケットへの権限を付与する方法(②)の2通りがある。

方法①すべてのS3にアクセスを許可する

  • AWSが提供する管理ポリシーAmazonS3FullAccessをアタッチする
    セキュリティ的に問題がありそう?

方法②特定のS3バケットのみフルアクセスを許可する

セキュリティを考慮して、必要なバケット(とアクション)のみを許可するポリシーを作成する。

IAMロールの許可を追加からインラインポリシーを作成を選択する
スクリーンショット 2025-08-21 233850.png
jsonを選択し、下記のようなポリシーを記述して、アクセス許可する

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": "s3:*",
			"Resource": [
				"arn:aws:s3:::test-backet",
				"arn:aws:s3:::test-backet/*"
			]
		}
	]
}
  • 以下のjsonでは、バケットに加えてアクションも指定することが可能
    • "s3:GetObject":オブジェクトをダウンロード
    • "s3:PutObject":オブジェクトをアップロード
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",  
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::test-backet/*"
    }
  ]
}

おわりに

同じVPC内ということもあり、IAMを正しく設定するだけで、解決できました。
ひとまず、動いてなにより。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?