IAM完全ガイド
IAM (Identity and Access Management) 完全ガイド
Section titled “IAM (Identity and Access Management) 完全ガイド”IAMは、AWSリソースへのアクセスを管理するサービスです。実務で使える権限管理とベストプラクティスを詳しく解説します。
1. IAMとは
Section titled “1. IAMとは”IAMの役割
Section titled “IAMの役割”IAMは、AWSリソースへのアクセスを制御するサービスです。ユーザー、グループ、ロール、ポリシーを管理します。
ユーザー/ロール ↓(IAMポリシー)AWSリソースへのアクセス制御なぜIAMが必要か
Section titled “なぜIAMが必要か”問題のある構成(ルートアカウントの使用):
# 問題: ルートアカウントで直接操作# 1. すべての権限を持つ# 2. 監査が困難# 3. 権限の細かい制御ができない# 4. セキュリティリスクが高い
# 問題点:# 1. 過剰な権限# 2. 監査の困難さ# 3. セキュリティリスク解決: IAMによる細かい権限管理
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject" ], "Resource": "arn:aws:s3:::my-bucket/*", "Condition": { "IpAddress": { "aws:SourceIp": "203.0.113.0/24" } } } ]}2. IAMの基本概念
Section titled “2. IAMの基本概念”# ユーザーの作成aws iam create-user --user-name developer
# ユーザーの一覧表示aws iam list-users
# ユーザーの削除aws iam delete-user --user-name developer# グループの作成aws iam create-group --group-name developers
# ユーザーをグループに追加aws iam add-user-to-group --user-name developer --group-name developers
# グループにポリシーをアタッチaws iam attach-group-policy \ --group-name developers \ --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessResources: EC2Role: Type: AWS::IAM::Role Properties: RoleName: EC2S3AccessRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: ec2.amazonaws.com Action: sts:AssumeRole Policies: - PolicyName: S3Access PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:GetObject - s3:PutObject Resource: 'arn:aws:s3:::my-bucket/*' Tags: - Key: Name Value: EC2S3AccessRole
InstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Roles: - !Ref EC2Role{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowS3Access", "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:DeleteObject" ], "Resource": "arn:aws:s3:::my-bucket/*" }, { "Sid": "DenyS3Delete", "Effect": "Deny", "Action": [ "s3:DeleteObject" ], "Resource": "arn:aws:s3:::my-bucket/production/*" } ]}3. IAMポリシーの詳細
Section titled “3. IAMポリシーの詳細”基本的なポリシー構造
Section titled “基本的なポリシー構造”{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", // Allow または Deny "Action": [ // 許可するアクション "s3:GetObject", "s3:PutObject" ], "Resource": [ // 対象リソース "arn:aws:s3:::my-bucket/*" ], "Condition": { // 条件(オプション) "IpAddress": { "aws:SourceIp": "203.0.113.0/24" } } } ]}{ "Condition": { "IpAddress": { "aws:SourceIp": "203.0.113.0/24" }, "StringEquals": { "aws:RequestedRegion": "us-east-1" }, "DateGreaterThan": { "aws:CurrentTime": "2024-01-01T00:00:00Z" }, "StringLike": { "s3:prefix": "uploads/*" } }}ワイルドカードの使用
Section titled “ワイルドカードの使用”{ "Resource": [ "arn:aws:s3:::my-bucket/*", // すべてのオブジェクト "arn:aws:s3:::my-bucket/images/*", // images/配下のすべて "arn:aws:s3:::my-bucket/*.jpg" // .jpgで終わるすべてのファイル ]}4. IAMロールの実践例
Section titled “4. IAMロールの実践例”EC2インスタンス用ロール
Section titled “EC2インスタンス用ロール”Resources: EC2Role: Type: AWS::IAM::Role Properties: RoleName: EC2ApplicationRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: ec2.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy Policies: - PolicyName: S3Access PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:GetObject - s3:PutObject Resource: 'arn:aws:s3:::my-bucket/*' - Effect: Allow Action: - s3:ListBucket Resource: 'arn:aws:s3:::my-bucket'
InstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Roles: - !Ref EC2RoleLambda関数用ロール
Section titled “Lambda関数用ロール”Resources: LambdaExecutionRole: Type: AWS::IAM::Role Properties: RoleName: LambdaExecutionRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Policies: - PolicyName: DynamoDBAccess PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - dynamodb:GetItem - dynamodb:PutItem - dynamodb:UpdateItem - dynamodb:DeleteItem Resource: 'arn:aws:dynamodb:*:*:table/my-table'クロスアカウントアクセス
Section titled “クロスアカウントアクセス”Resources: CrossAccountRole: Type: AWS::IAM::Role Properties: RoleName: CrossAccountAccessRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: AWS: 'arn:aws:iam::123456789012:root' # 別のAWSアカウント Action: sts:AssumeRole Condition: StringEquals: sts:ExternalId: 'unique-external-id' Policies: - PolicyName: S3ReadOnlyAccess PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:GetObject - s3:ListBucket Resource: - 'arn:aws:s3:::my-bucket' - 'arn:aws:s3:::my-bucket/*'5. IAMポリシーのベストプラクティス
Section titled “5. IAMポリシーのベストプラクティス”最小権限の原則
Section titled “最小権限の原則”{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" // 必要なアクションのみ ], "Resource": [ "arn:aws:s3:::my-bucket/specific-folder/*" // 特定のリソースのみ ] } ]}{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject" ], "Resource": "arn:aws:s3:::my-bucket/*", "Condition": { "StringEquals": { "s3:x-amz-server-side-encryption": "AES256" }, "IpAddress": { "aws:SourceIp": "203.0.113.0/24" }, "DateGreaterThan": { "aws:CurrentTime": "2024-01-01T00:00:00Z" } } } ]}明示的なDeny
Section titled “明示的なDeny”{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:*" ], "Resource": "arn:aws:s3:::my-bucket/*" }, { "Effect": "Deny", "Action": [ "s3:DeleteObject" ], "Resource": "arn:aws:s3:::my-bucket/production/*" } ]}6. MFA(多要素認証)
Section titled “6. MFA(多要素認証)”MFAの設定
Section titled “MFAの設定”# MFAデバイスの作成aws iam create-virtual-mfa-device \ --virtual-mfa-device-name my-mfa-device \ --outfile QRCode.png \ --bootstrap-method QRCodePNG
# ユーザーにMFAを有効化aws iam enable-mfa-device \ --user-name developer \ --serial-number arn:aws:iam::123456789012:mfa/my-mfa-device \ --authentication-code-1 123456 \ --authentication-code-2 654321MFA必須のポリシー
Section titled “MFA必須のポリシー”{ "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": "*", "Resource": "*", "Condition": { "BoolIfExists": { "aws:MultiFactorAuthPresent": "false" } } } ]}7. IAMポリシーのシミュレーション
Section titled “7. IAMポリシーのシミュレーション”ポリシーのテスト
Section titled “ポリシーのテスト”# ポリシーのシミュレーションaws iam simulate-principal-policy \ --policy-source-arn arn:aws:iam::123456789012:user/developer \ --action-names s3:GetObject \ --resource-arns arn:aws:s3:::my-bucket/file.txt8. 実務でのベストプラクティス
Section titled “8. 実務でのベストプラクティス”パターン1: 環境別の権限管理
Section titled “パターン1: 環境別の権限管理”# 開発環境用ロールResources: DevRole: Type: AWS::IAM::Role Properties: RoleName: DevRole Policies: - PolicyName: DevAccess PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: "*" Resource: "*" Condition: StringLike: "aws:ResourceTag/Environment": "dev"
# 本番環境用ロールResources: ProdRole: Type: AWS::IAM::Role Properties: RoleName: ProdRole Policies: - PolicyName: ProdAccess PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:GetObject - s3:PutObject Resource: "*" Condition: StringEquals: "aws:ResourceTag/Environment": "prod"パターン2: 時間制限付きアクセス
Section titled “パターン2: 時間制限付きアクセス”{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:*" ], "Resource": "*", "Condition": { "DateGreaterThan": { "aws:CurrentTime": "09:00Z" }, "DateLessThan": { "aws:CurrentTime": "18:00Z" } } } ]}9. よくある問題と解決策
Section titled “9. よくある問題と解決策”問題1: アクセスが拒否される
Section titled “問題1: アクセスが拒否される”原因:
- ポリシーに権限がない
- リソースのARNが間違っている
- 条件が満たされていない
解決策:
# ユーザーのポリシーを確認aws iam list-attached-user-policies --user-name developer
# ポリシーの内容を確認aws iam get-policy --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessaws iam get-policy-version \ --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \ --version-id v1
# ポリシーのシミュレーションaws iam simulate-principal-policy \ --policy-source-arn arn:aws:iam::123456789012:user/developer \ --action-names s3:GetObject \ --resource-arns arn:aws:s3:::my-bucket/file.txt問題2: 過剰な権限
Section titled “問題2: 過剰な権限”原因:
- ワイルドカードの多用
- 最小権限の原則に従っていない
解決策:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" // 必要なアクションのみ ], "Resource": [ "arn:aws:s3:::my-bucket/specific-folder/*" // 特定のリソースのみ ] } ]}これで、IAMの基礎知識と実務での権限管理を理解できるようになりました。