AWS Identity and Access Management
誰が何をできるか
ID(user、role)と、誰がどのリソースに対してどの API を呼び出せるかを決めるポリシー(policy)を管理します。
- コスト$ 安い
- 難易度●●●●●
- 未使用でも課金?いいえ
- AWS マネージド
- 出題試験CLF · SAA · DVA · SOA
たとえるならビルのカードキーシステムのようなものです。各カードで特定のドアだけを開けられます。
IAM はシステムのどこにある?
具体例でステップごとに
- AWS アカウントを開設したばかりの会社で、全員がルートユーザーを共有しており、とても危険です。
- ルートに MFA を設定して使わないようにし、EC2、S3、Lambda だけを扱える Developers グループを作ります。
- EC2 上のアプリが S3 を読む必要があるので、設定ファイルにアクセスキーを書かず、インスタンスに IAM ロールを付けます。
- AWS がロールに一時的な認証情報を渡して定期的に入れ替えるので、コードから固定のキーが漏れることはありません。
- 本番バケットの削除を Deny するポリシーがあれば、他の権限が許可していても削除はブロックされます。
いつ使う?
✓ 使うべき場面
常に必要: AWS へのすべての呼び出しは IAM を経由します。
✗ 避けるべき場面
アプリケーションのエンドユーザーのログイン → Cognito。
よくある使い方
- EC2/Lambda/ECS が access key なしで AWS API を呼び出すための Role
- 人とアプリケーションへの最小権限(least privilege)の付与
- AssumeRole によるクロスアカウントアクセス
IAM・IAM Identity Center・Cognito の違い
| 対象 | 1 つの AWS アカウント内の権限(人、サービス) | 多数のアカウントに 1 回でサインインする従業員 | アプリのエンドユーザー |
|---|---|---|---|
| ID | ユーザー、グループ、ロール | 社内ディレクトリの人 | ユーザープール、ソーシャルサインイン |
| 認証情報 | パスワード、アクセスキー、ロールの一時認証情報 | 権限セットによる一時的な権限 | JWT トークン |
| 料金 | 無料 | 無料 | 月間アクティブユーザー単位 |
料金はいくら?
- 課金方式: 無料です。
- 無料枠: 無料です。
直接の費用はかかりませんが、権限が広すぎると、キーが漏洩した際に莫大な請求につながる可能性があります(コインのマイニングに悪用される)。長期の access key の代わりに role を使いましょう。
AWS サービス(EC2、Lambda)に権限が必要なら IAM ロールを使い、コードにアクセスキーを置いてはいけません。明示的な Deny は常に Allow より優先されます。別アカウントからのアクセスはクロスアカウントで引き受けられるロール、アプリのエンドユーザーのサインインは IAM ではなく Cognito です。
本番の試験問題に挑戦
1A company is planning to run a group of Amazon EC2 instances that connect to an Amazon Aurora database. The company has built an AWS CloudFormation template to deploy the EC2 instances and the Aurora DB cluster. The company wants to allow the instances to authenticate to the database in a secure way. The company does not want to maintain static database credentials. Which solution meets these requirements with the LEAST operational effort?
- ACreate a database user with a user name and password. Add parameters for the database user name and password to the CloudFormation template. Pass the parameters to the EC2 instances when the instances are launched.
- BCreate a database user with a user name and password. Store the user name and password in AWS Systems Manager Parameter Store. Configure the EC2 instances to retrieve the database credentials from Parameter Store.
- CConfigure the DB cluster to use IAM database authentication. Create a database user to use with IAM authentication. Associate a role with the EC2 instances to allow applications on the instances to access the database.
- DConfigure the DB cluster to use IAM database authentication with an IAM user. Create a database user that has a name that matches the IAM user. Associate the IAM user with the EC2 instances to allow applications on the instances to access the database.
答えを見る
正解: C
Aurora で IAM データベース認証を有効にし、EC2 に IAM ロールを付ければ、アプリは固定のパスワードを保存せずに一時的なトークンを取得できます。IAM ユーザーを EC2 に関連付けるのは誤りで、EC2 はロールを通じて権限を受け取ります。
2An administrator is using Amazon CloudFormation to deploy a three tier web application that consists of a web tier and application tier that will utilize Amazon DynamoDB for storage. When creating the CloudFormation template, which of the following would allow the application instance access to the DynamoDB tables without exposing API credentials?
- ACreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and associate the Role to the application instances by referencing an instance profile.
- BUse the Parameter section in the Cloud Formation template to have the user input Access and Secret Keys from an already created IAM user that has the permissions required to read and write from the required DynamoDB table.
- CCreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and reference the Role in the instance profile property of the application instance.
- DCreate an identity and Access Management user in the CloudFormation template that has permissions to read and write from the required DynamoDB table, use the GetAtt function to retrieve the Access and secret keys and pass them to the application instance through user-data.
答えを見る
正解: C
DynamoDB テーブルを読み書きできる IAM ロールを作り、テンプレート内のインスタンスのインスタンスプロファイルプロパティで参照します。アクセスキーをパラメータに入れたり、テンプレートで生成したりしてはいけません。
よくある質問
IAM ユーザー、グループ、ロールの違いは何ですか?
ユーザーはパスワードやアクセスキーを持つ長期的な ID です。グループはユーザーをまとめて共通の権限を付与します。ロールは固定の認証情報を持たず、引き受けることで一時的な権限を得るもので、AWS サービス(EC2、Lambda など)、他のアカウント、SSO でサインインする人が使います。
ルートユーザーを日常的に使ってもよいですか?
使うべきではありません。ルートユーザーはすべての権限を持ち、ポリシーで制限できません。必要な一部の作業だけに使い、MFA を有効にし、アクセスキーは作らず、日常の作業には最小権限のユーザーや SSO を使いましょう。
IAM に料金はかかりますか?
かかりません。IAM は無料で、それらの ID が使うリソースの料金のみが発生します。
AWS 認定試験では IAM について何が問われますか?
最小権限の原則、ポリシーの評価方法(明示的な Deny が常に優先)、コードにアクセスキーを置かず EC2 や Lambda にロールを使うこと、ロールによるクロスアカウントアクセス、MFA、アクセス許可の境界などです。