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?

IAMグループ単位でS3のアクセス制御をCloudFormationで自動化する

Last updated at Posted at 2025-05-26

🎯 この資料は CloudFormation でグループ毎のアクセス制御を自動構築するテンプレート群です。

以下の 3 つのテンプレートから構成されます。

  • グループを作成するテンプレート
  • グループに所属するユーザを作成するテンプレート
  • グループ毎に更新 / 読み取り / 参照不可となる S3 の作成テンプレート

🎯 サンプルとして次の操作が行えます

目的 内容
IAM グループ作成 group-a, group-b, group-c を自動作成(create_groups.sh 実行)
IAM ユーザー作成 user-a1, user-b1, user-b2, user-c1 を自動作成し、所属を設定(create_users.sh 実行)
S3 バケット作成 & 権限付与 S3バケットを作成し、同時にグループ別権限を付与
  • group-a: 更新 + 一覧
  • group-b: 読み取り + 一覧
  • group-c: 一覧のみ
deploy_s3_and_policies.sh 実行)

📂 ファイル構成と役割

├── cf_create_group.yaml                         # IAM グループ作成
├── cf_create_user_in_group.yaml                 # IAM ユーザー作成 + 所属設定
├── cf_create_s3.yaml                            # S3 バケット作成
├── cf_attach_s3_write_policy_to_group.yaml      # group-a 用: s3:* 権限
├── cf_attach_s3_read_policy_to_group.yaml       # group-b 用: 読み取り権限
├── cf_attach_listallbuckets_policy_to_group.yaml # 全グループ: バケット一覧権限
├── create_groups.sh                             # グループ作成
├── create_users.sh                              # ユーザー作成
├── deploy_s3_and_policies.sh                    # S3 + ポリシー一括適用
├── delete_all_stacks.sh                         # 全リソース削除(S3 中身削除込み)

🚀 デプロイ手順

1. リポジトリ取得 & 実行権限付与

# クローン
git clone https://github.com/hirayama-yuuichi/cloudformation-iam-s3-access.git
cd cloudformation-iam-s3-access

# Linux / macOS
chmod +x *.sh

2. IAM グループ & ユーザー作成

./create_groups.sh   # group-a / group-b / group-c
./create_users.sh    # user-a1 / user-b1 / user-b2 / user-c1

3. S3 バケット + ポリシー一括適用

BUCKET="s3-$(date +%Y%m%d-%H%M)"   # バケット名はグローバルで一意
./deploy_s3_and_policies.sh "$BUCKET"

✅ 動作確認ポイント

ユーザー 期待される操作 確認方法
user-a1 アップロード/削除可 CLI または S3 コンソールで Put/Delete
user-b1 / user-b2 オブジェクト参照可(更新不可) GetObject は成功するが PutObject は拒否される
user-c1 バケット一覧のみ可 コンソールでバケットは見えるがオブジェクト閲覧は拒否

🔍 バケットが一覧に出ない場合s3:ListAllMyBuckets が付与されているか確認。


🧹 クリーンアップ手順

./delete_all_stacks.sh "$BUCKET"   # デプロイ時と同じバケット名を渡す

スクリプトは

  1. バケット内オブジェクトを aws s3 rm s3://$BUCKET --recursive で削除
  2. CloudFormation スタックを依存順に削除
    を自動実行します。

⚠️ はまりどころ

  1. バケット名競合:S3 バケット名はグローバルで一意。s3-日付-時刻 などで衝突を回避。
  2. 削除失敗:バケット内にオブジェクトが残っているとスタック削除エラー。
  3. 一覧表示不可s3:ListAllMyBuckets 欠如でコンソールに何も出ない。

📝 補足

  • 初期パスワード管理as456A!we はサンプル。本番環境では必ず変更してください。

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?