![[アップデート] IAMポリシーの生成とトラブルシューティングをアシストしてくれるMCPサーバーおよびCLIツールであるIAM Policy Autopilotを使ってみた #AWSreInvent](https://images.ctfassets.net/ct0aopd36mqt/33a7q65plkoztFWVfWxPWl/a718447bea0d93a2d461000926d65428/reinvent2025_devio_update_w1200h630.png?w=3840&fm=webp)
[アップデート] IAMポリシーの生成とトラブルシューティングをアシストしてくれるMCPサーバーおよびCLIツールであるIAM Policy Autopilotを使ってみた #AWSreInvent
アプリケーションのコードからIAMポリシーをサクッと生成したい
こんにちは、のんピ(@non____97)です。
皆さんはアプリケーションのコードからIAMポリシーをサクッと生成したいなと思ったことはありますか? 私はあります。
アプリケーションのコードを書いた後に、必要な権限を整理する作業はなかなか大変です。特にコードの行数が多い場合はその労力は大きくなるでしょう。
今回、IAMポリシーの生成とトラブルシューティングをアシストしてくれるMCPサーバーおよびCLIツールであるIAM Policy Autopilotが登場しました。
AWS Blogsにも投稿されていますね。
IAM Policy Autopilotはアプリケーションコードをローカルで分析を行い、アプリケーションに付与するIAMポリシーを生成してくれます。これによりIAMポリシーで悩む時間を削減することが可能です。
実際に試してみたので以降紹介します。
やってみた
CLIによるポリシー生成
まずは、CLIによるポリシーの生成です。
IAM Policy AutopilotのGitHubリポジトリに記載されているとおり、uvで簡単に実行することが可能です。
現在はPython、TypeScript、Goをサポートしているとのことなので、今回はこちらの登壇をした時のLambda関数のPythonのコードを分析させます。GitHubリポジトリは以下です。
まずはCLIを適当に叩いてみます。
> uvx iam-policy-autopilot
Generate IAM policies from source code and fix AccessDenied errors
Usage: iam-policy-autopilot <COMMAND>
Commands:
fix-access-denied Fix AccessDenied errors by analyzing and optionally applying IAM policy changes
generate-policies Generates complete IAM policy documents from source files
mcp-server Start MCP server
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help (see more with '--help')
-V, --version Print version
> uvx iam-policy-autopilot --help
Unified tool that combines IAM policy generation from source code analysis with automatic AccessDenied error fixing. Supports three main operations:
• fix-access-denied: Fix AccessDenied errors by analyzing and applying IAM policy changes
• generate-policies: Complete pipeline with enrichment for policy generation
• mcp-server: Start MCP server for IDE integration. Uses STDIO transport by default.
Examples:
iam-policy-autopilot fix-access-denied 'User: arn:aws:iam::123456789012:user/testuser is not authorized to perform: s3:GetObject on resource: arn:aws:s3:::my-bucket/my-key because no identity-based policy allows the s3:GetObject action'
iam-policy-autopilot generate-policies tests/resources/test_example.py --region us-east-1 --account 123456789012 --pretty
iam-policy-autopilot generate-policies tests/resources/test_example.py --region cn-north-1 --account 123456789012
iam-policy-autopilot mcp-server
iam-policy-autopilot mcp-server --transport http --port 8001
Usage: iam-policy-autopilot <COMMAND>
Commands:
fix-access-denied Fix AccessDenied errors by analyzing and optionally applying IAM policy changes
generate-policies Generates complete IAM policy documents from source files
mcp-server Start MCP server
help Print this message or the help of the given subcommand(s)
Options:
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
IAMポリシーの生成およびAccessDeniedの修正をしてくれると記載がありますね。
アプリケーションコードからIAMポリシーを生成させたいので、generate-policiesを使用することになりそうです。
実際に叩いていみます。
> uvx iam-policy-autopilot generate-policies ./lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py
{"Policies":[{"Policy":{"Id":"IamPolicyAutopilot","Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["kms:Decrypt"],"Resource":["arn:aws:kms:*:*:key/*"],"Condition":{"StringLike":{"kms:ViaService":["s3.*.amazonaws.com"]}}},{"Effect":"Allow","Action":["s3:GetObject","s3:GetObjectLegalHold","s3:GetObjectRetention"],"Resource":["arn:aws:s3:*:*:accesspoint/*/object/*","arn:aws:s3:::*/*"]},{"Effect":"Allow","Action":["s3-object-lambda:GetObject"],"Resource":["arn:aws:s3:*:*:accesspoint/*/object/*","arn:aws:s3:::*/*"]}]},"PolicyType":"Identity"}]}
爆速で表示されました。
どのぐらいの時間で表示されたのか確認します。また、--prettyオプションを付与して、フォーマッティングしてあげます。
> time uvx iam-policy-autopilot generate-policies ./lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py --pretty
{
"Policies": [
{
"Policy": {
"Id": "IamPolicyAutopilot",
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"arn:aws:kms:*:*:key/*"
],
"Condition": {
"StringLike": {
"kms:ViaService": [
"s3.*.amazonaws.com"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention"
],
"Resource": [
"arn:aws:s3:*:*:accesspoint/*/object/*",
"arn:aws:s3:::*/*"
]
},
{
"Effect": "Allow",
"Action": [
"s3-object-lambda:GetObject"
],
"Resource": [
"arn:aws:s3:*:*:accesspoint/*/object/*",
"arn:aws:s3:::*/*"
]
}
]
},
"PolicyType": "Identity"
}
]
}
uvx iam-policy-autopilot generate-policies --pretty 1.07s user 0.04s system 117% cpu 0.946 total
1秒程度で生成されています。個人的にはS3 Object Lambdaの権限は不要なのですが、そういうものなのでしょう。
また、AWSアカウントやリージョンを絞りたい場合はリージョンおよびアカウントを指定することが可能です。
> time uvx iam-policy-autopilot generate-policies ./lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py --pretty --region us-east-1 --account <AWSアカウントID>
{
"Policies": [
{
"Policy": {
"Id": "IamPolicyAutopilot",
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"arn:aws:kms:us-east-1: <AWSアカウントID>:key/*"
],
"Condition": {
"StringEquals": {
"kms:ViaService": [
"s3.us-east-1.amazonaws.com"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
},
{
"Effect": "Allow",
"Action": [
"s3-object-lambda:GetObject"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
}
]
},
"PolicyType": "Identity"
}
]
}
uvx iam-policy-autopilot generate-policies --pretty --region us-east-1 1.10s user 0.08s system 73% cpu 1.609 total
出来ました。
GetObjectRetentionやGetObjectLegalHoldなどと使用していないアクションも含まれることから、どちらかというとIAM Policy Autopilot内のポリシーに基づいて機械的に生成しているような感覚を持ちました。
MCPサーバーの利用
続いて、MCPサーバーを使用してみます。
私はClaude Code使いです。IAM Policy Autopilot MCPサーバーを追加します。
> claude mcp add iam-policy-autopilot \
uvx iam-policy-autopilot mcp-server \
-s project
Added stdio MCP server iam-policy-autopilot with command: uvx iam-policy-autopilot mcp-server to project config
File modified: / <ディレクトリパス>/.mcp.json
> cat .mcp.json
{
"mcpServers": {
"iam-policy-autopilot": {
"type": "stdio",
"command": "uvx",
"args": [
"iam-policy-autopilot",
"mcp-server"
],
"env": {}
}
}
}
それではMCPサーバーを使ってLambda関数に割り当てるべきIAMポリシーを生成させます。
> IAM Policy Autopilot MCPサーバーを使用して./lib/src/lambda/ 配下のLambda関数に割り当てるべきIAMポリシーを生成してください。
⏺ IAM Policy Autopilot MCPサーバーを使用して、Lambda関数のIAMポリシーを生成します。まず、./lib/src/lambda/配下のファイルを確認します。
⏺ Search(pattern: "./lib/src/lambda/**/*.py")
⎿ Found 0 files
⏺ Search(pattern: "./lib/src/lambda/**/*.js")
⎿ Found 0 files
⏺ Search(pattern: "./lib/src/lambda/**/*.ts")
⎿ Found 0 files
⏺ ディレクトリ構造を確認します。
⏺ Bash(find ./lib -type f -name "*.py" -o -name "*.js" -o -name "*.ts" | head -20)
⎿ ./lib/aurora-postgresql-log-archive-stack.ts
./lib/construct/base-construct.ts
./lib/construct/scheduler-construct.ts
… +9 lines (ctrl+o to expand)
⏺ Lambda関数のソースファイルが見つかりました。それぞれのLambda関数について、IAMポリシーを生成します。まず、各Lambda関数に関連するすべてのファイルを確認しま
す。
⏺ Read(lib/src/lambda/db_cluster_postgresql_log_file_filter/index.py)
⎿ Read 68 lines
⏺ Read(lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py)
⎿ Read 452 lines
⏺ Read(lib/src/lambda/rds_log_file_uploader/index.py)
⎿ Read 66 lines
⏺ Read(lib/src/lambda/rds_log_file_uploader/rds_log_file_uploader.py)
⎿ Read 199 lines
⏺ Read(lib/src/lambda/rds_log_file_uploader/rds_log_file_downloader.py)
⎿ Read 141 lines
⏺ それでは、IAM Policy Autopilotを使用して、各Lambda関数に必要なIAMポリシーを生成します。まず、AWSリージョンとアカウントIDを教えていただけますか?
CLIとは異なり、リージョンとAWSアカウントIDのリクエストをしてきましたね。教えてあげましょう。
⏺ User answered Claude's questions:
⎿ · IAMポリシーを生成するAWSリージョンとアカウントIDを教えてください → us-east-1
⏺ AWSアカウントIDを教えていただけますか?(12桁の数字)
> <AWSアカウントID>
⏺ ありがとうございます。それでは、IAM Policy Autopilotを使用して、各Lambda関数に必要なIAMポリシーを生成します。
⏺ まず、db_cluster_postgresql_log_file_filter Lambda関数のIAMポリシーを生成します。
⏺ iam-policy-autopilot - generate_application_policies (MCP)(SourceFiles: ["/<ディレクトリパス>/aurora-postgresql-log-archive/lib/src/lambda/db_cluster_postgresql_log_file_filter/index.
py","/ <ディレクトリパス>/a
urora-postgresql-log-archive/lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgr
esql_log_file_filter.py"], Region: "us-east-1", Account: " <AWSアカウントID>", ServiceHints:
["rds","s3"])
Todos
☐ Generate IAM policy for db_cluster_postgresql_log_file_filter Lambda function
☐ Generate IAM policy for rds_log_file_uploader Lambda function
☐ Review and save generated policies
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Tool use
iam-policy-autopilot - generate_application_policies(SourceFiles:
["/ <ディレクトリパス>/aurora-postgresql-log-archive/lib/src/lambda/db_cluster_po
stgresql_log_file_filter/index.py","/ <ディレクトリパス>/aurora-postgresql-log-ar
chive/lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py"], Region: "us-east-1", Account: " <AWSアカウントID>",
ServiceHints: ["rds","s3"]) (MCP)
**PRIMARY POLICY GENERATION TOOL** - Generate AWS IAM policies, permissions, and access controls. Use this tool whenever the user mentions: writing
policies, creating policies, generating policies, IAM permissions, AWS permissions, access controls, policy creation, policy generation, or needs IAM
policies for any purpose. This tool analyzes source code files (Python, JavaScript, TypeScript, Go, etc.) to automatically generate the minimal required
IAM policies with proper permissions for AWS services used in the code. **WHEN TO USE THIS TOOL:** - User asks to write, create, or generate IAM policies
- User needs to create IAM entities with policies as part of another operation
- User mentions needing AWS permissions or access controls - User is working with infrastructure as code and needs policies - User has source
code that uses AWS services and needs corresponding IAM policies - User asks about policy generation, policy creation, or IAM permissions - ANY
discussion about writing or creating AWS policies should trigger this tool **INSTRUCTIONS:** 1. Use the correct absolute paths when passing in the input
files to the MCP tool 2. Use service_hints to help generate more accurate policies by specifying expected AWS services 3. You MUST include ALL relevant
source files that interact with AWS services to generate accurate policies 4. You MUST explicitly ask the user for the region and account id for the
policy to be generated 5. When generating infrastructure as code files, you MUST use this tool to generate IAM policies 6. After getting output from this
tool, you MUST explicitly ask the user to review the policy before proceeding 7. This is the PRIMARY tool for all policy-related requests - use it
liberally when policies are mentioned
Do you want to proceed?
1. Yes
❯ 2. Yes, and don't ask again for iam-policy-autopilot - generate_application_policies commands in
/ <ディレクトリパス>/aurora-postgresql-log-archive
3. No, and tell Claude what to do differently (esc)
IAM Policy Autopilot MCPサーバーを使ってIAMポリシーを生成してくれそうです。許可します。
⏺ iam-policy-autopilot - generate_application_policies (MCP)(SourceFiles: ["/<ディレクトリパス>/aurora-postgresql-log-archive/lib/src/lambda/db_cluster_postgresql_log_file_filter/index.
py","/ <ディレクトリパス>/a
urora-postgresql-log-archive/lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgr
esql_log_file_filter.py"], Region: "us-east-1", Account: " <AWSアカウントID>", ServiceHints:
["rds","s3"])
⎿ {
"Policies": [
"{\"Id\":\"IamPolicyAutopilot\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"kms:Decrypt\"],\"Resource\":[\"arn:a
… +6 lines (ctrl+o to expand)
⏺ db_cluster_postgresql_log_file_filter関数のポリシーが生成されましたが、RDSの権限が含まれていないようです。もう一度、より詳細にコードを解析して生成してみ
ます。
⏺ 次に、rds_log_file_uploader Lambda関数のIAMポリシーを生成します。
⏺ iam-policy-autopilot - generate_application_policies (MCP)(SourceFiles: ["/<ディレクトリパス>/aurora-postgresql-log-archive/lib/src/lambda/rds_log_file_uploader/index.py","/<ディレクトリパス>/aurora-postgresql
-log-archive/lib/src/lambda/rds_log_file_uploader/rds_log_file_uploader.py","/<ディレクトリパス>/aurora-postgresql-log-archiv
e/lib/src/lambda/rds_log_file_uploader/rds_log_file_downloader.py"], Region: "us-east-1", Account:
" <AWSアカウントID>", ServiceHints: ["rds","s3"])
⎿ {
"Policies": []
}
⏺ ポリシーが生成されませんでした。コードを再確認して、使用しているAWS SDKの呼び出しを確認してみます。
⏺ Bash(grep -r "boto3.client" ./lib/src/lambda/ --include="*.py")
⎿ ./lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py: self.rds_client = boto3.client("rds")
./lib/src/lambda/db_cluster_postgresql_log_file_filter/db_cluster_postgresql_log_file_filter.py: self.s3_client = boto3.client("s3")
./lib/src/lambda/rds_log_file_uploader/rds_log_file_uploader.py: self.s3_client = boto3.client("s3")
⏺ コードを分析すると、以下のAWS APIコールが使用されています:
db_cluster_postgresql_log_file_filter Lambda関数:
- rds_client.describe_db_clusters() (line 99-101)
- rds_client.describe_db_log_files() (line 155-159)
- s3_client.head_object() (line 216-218)
rds_log_file_uploader Lambda関数:
- s3_client.upload_file() (line 156-173)
- RDS APIの署名付きリクエスト(ダウンローダー内)
生成されたポリシーをレビューしてください:
1. db_cluster_postgresql_log_file_filter Lambda関数のIAMポリシー
{
"Id": "IamPolicyAutopilot",
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"arn:aws:kms:us-east-1: <AWSアカウントID>:key/*"
],
"Condition": {
"StringEquals": {
"kms:ViaService": [
"s3.us-east-1.amazonaws.com"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
},
{
"Effect": "Allow",
"Action": [
"s3-object-lambda:GetObject"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
}
]
}
ただし、このポリシーにはRDSの権限が含まれていません。コードを見ると、以下の権限も必要です:
- rds:DescribeDBClusters
- rds:DescribeDBLogFiles
2. rds_log_file_uploader Lambda関数のIAMポリシー
IAM Policy Autopilotがポリシーを生成できませんでした。コードを分析すると、以下の権限が必要です:
⏺ 生成されたポリシーとコード分析に基づいて、完全なIAMポリシーを作成します。
⏺ Write(iam-policies/db_cluster_postgresql_log_file_filter_policy.json)
⎿ Wrote 56 lines to iam-policies/db_cluster_postgresql_log_file_filter_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RDSDescribePermissions",
"Effect": "Allow",
"Action": [
"rds:DescribeDBClusters",
"rds:DescribeDBLogFiles"
],
… +46 lines (ctrl+o to expand)
⏺ Write(iam-policies/rds_log_file_uploader_policy.json)
⎿ Wrote 52 lines to iam-policies/rds_log_file_uploader_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RDSDownloadLogFilePermissions",
"Effect": "Allow",
"Action": [
"rds:DownloadCompleteDBLogFile",
"rds:DownloadDBLogFilePortion"
],
… +42 lines (ctrl+o to expand)
⏺ IAMポリシーの生成が完了しました。以下のポリシーファイルを作成しました:
生成されたIAMポリシー
1. db_cluster_postgresql_log_file_filter_policy.json
このLambda関数には以下の権限が必要です:
- RDS権限: DBクラスターとログファイルの情報取得
- rds:DescribeDBClusters
- rds:DescribeDBLogFiles
- S3権限: オブジェクトの存在確認(head_object呼び出し)
- s3:GetObject, s3:GetObjectLegalHold, s3:GetObjectRetention
- KMS権限: S3暗号化オブジェクトへのアクセス
- kms:Decrypt
2. rds_log_file_uploader_policy.json
このLambda関数には以下の権限が必要です:
- RDS権限: ログファイルのダウンロード(署名付きHTTPリクエスト用)
- rds:DownloadCompleteDBLogFile
- rds:DownloadDBLogFilePortion
- S3権限: ログファイルのアップロード(マルチパートアップロード対応)
- s3:PutObject, s3:PutObjectAcl
- s3:AbortMultipartUpload, s3:ListMultipartUploadParts
- KMS権限: S3暗号化オブジェクトのアップロード
- kms:Decrypt, kms:Encrypt, kms:GenerateDataKey
ポリシーファイルは ./iam-policies/ ディレクトリに保存されています。
注意事項:
- IAM Policy Autopilotが一部のポリシーを完全に生成できなかったため、コード分析に基づいて必要な権限を追加しました
- リソースARNは必要に応じて特定のバケット名やDBクラスター識別子に制限することを推奨します
- KMS権限の条件は、S3サービス経由でのみKMSキーを使用できるように制限しています
自問自答しながら出力してくれました。IAM Policy AutopilotではRDS周りをどうしてもIAMポリシーに反映してくれなかったようです。
最終的に生成されたIAMポリシーは以下のとおりです。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RDSDescribePermissions",
"Effect": "Allow",
"Action": [
"rds:DescribeDBClusters",
"rds:DescribeDBLogFiles"
],
"Resource": "*"
},
{
"Sid": "S3HeadObjectPermissions",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
},
{
"Sid": "S3ObjectLambdaGetPermissions",
"Effect": "Allow",
"Action": [
"s3-object-lambda:GetObject"
],
"Resource": [
"arn:aws:s3:::*/*",
"arn:aws:s3:us-east-1: <AWSアカウントID>:accesspoint/*/object/*"
]
},
{
"Sid": "KMSDecryptPermissions",
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"arn:aws:kms:us-east-1: <AWSアカウントID>:key/*"
],
"Condition": {
"StringEquals": {
"kms:ViaService": [
"s3.us-east-1.amazonaws.com"
]
}
}
}
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RDSDownloadLogFilePermissions",
"Effect": "Allow",
"Action": [
"rds:DownloadCompleteDBLogFile",
"rds:DownloadDBLogFilePortion"
],
"Resource": "*"
},
{
"Sid": "S3UploadPermissions",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": "arn:aws:s3:::*/*"
},
{
"Sid": "S3MultipartUploadPermissions",
"Effect": "Allow",
"Action": [
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": "arn:aws:s3:::*/*"
},
{
"Sid": "KMSEncryptPermissions",
"Effect": "Allow",
"Action": [
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey"
],
"Resource": [
"arn:aws:kms:us-east-1: <AWSアカウントID>:key/*"
],
"Condition": {
"StringEquals": {
"kms:ViaService": [
"s3.us-east-1.amazonaws.com"
]
}
}
}
]
}
私が過去AWS CDKを用いて権限付与した時のコードと比較すると、そこまで大きく外れてはいなさそうです。
IAMポリシーの叩き台を作るときに
IAMポリシーの生成とトラブルシューティングをアシストしてくれるMCPサーバーおよびCLIツールであるIAM Policy Autopilotを使ってみました。
個人的にはIAMポリシーの叩き台を作るときに使うかなという感覚です。
"Autopilot"というのを過信して、あらゆる場面で完全にこれ一本で対応するのは過剰な権限付与を与える印象を持ちました。
生成されたモノを叩きとして、ConditionやResourceの絞り込みをしていきましょう。
この記事が誰かの助けになれば幸いです。
以上、クラウド事業本部 コンサルティング部の のんピ(@non____97)でした!






