Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
タグ付きの指定された VPC で EC2 インスタンスを作成および管理するためのアクセス権限を明示的に付与する IAM ポリシーを作成する方法を教えてください。
仮想プライベートクラウド (VPC) 内の Amazon Elastic Compute Cloud (Amazon EC2) インスタンスを管理するためのアクセス権限を IAM アイデンティティに明示的に付与する AWS Identity and Access Management (IAM) ポリシーを作成したいと考えています。
簡単な説明
IAM ポリシーでは、タグが付いている VPC 内のインスタンスを管理するための IAM アイデンティティの権限を制限する必要があります。
Amazon EC2 では resource-level permissions の一部が提供されますが、使用できるサービスレベルのアクション、リソース、条件がいくつかあります。EC2 リソースへのアクセスを制御するには、リソースタグを使用します。
解決策
VPC でインスタンスを起動する権限を付与する一方で、インスタンスを管理するための IAM アイデンティティの権限を制限するポリシーを作成します。インスタンスの管理を制限するには、ec2:ResourceTag ポリシー条件を使用します。
インスタンスを起動する IAM アイデンティティにアタッチする管理ポリシーを作成する
次の手順を実行します。
-
IAM コンソールを開きます。
-
[ポリシー] を選択し、[ポリシーの作成] を選択します。
-
[JSON] タブを選択し、以下のカスタムポリシーを入力します。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "GrantIAMPassRoleOnlyForEC2", "Action": [ "iam:PassRole" ], "Effect": "Allow", "Resource": [ "arn:aws:iam::account_number:role/role_name", "arn:aws:iam::account_number:role/role_nameE" ], "Condition": { "StringEquals": { "iam:PassedToService": "ec2.amazonaws.com" } } }, { "Sid": "ReadOnlyEC2WithNonResource", "Action": [ "ec2:Describe*", "iam:ListInstanceProfiles" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "ModifyingEC2WithNonResource", "Action": [ "ec2:CreateKeyPair", "ec2:CreateSecurityGroup" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "RunInstancesWithTagRestrictions", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:region:a:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "aws:RequestTag/TAG-KEY": "tag_value" } } }, { "Sid": "RemainingRunInstancePermissionsNonResource", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:REGION::image/*", "arn:aws:ec2:REGION::snapshot/*", "arn:aws:ec2:region:account_number:network-interface/*", "arn:aws:ec2:region:account_number:key-pair/*", "arn:aws:ec2:region:account_number:security-group/*" ] }, { "Sid": "EC2RunInstancesVpcSubnet", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": "arn:aws:ec2:region:account_number:subnet/*", "Condition": { "StringEquals": { "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPC-ID" } } }, { "Sid": "EC2VpcNonResourceSpecificActions", "Effect": "Allow", "Action": [ "ec2:DeleteNetworkAcl", "ec2:DeleteNetworkAclEntry", "ec2:DeleteRoute", "ec2:DeleteRouteTable", "ec2:AuthorizeSecurityGroupEgress", "ec2:AuthorizeSecurityGroupIngress", "ec2:RevokeSecurityGroupEgress", "ec2:RevokeSecurityGroupIngress", "ec2:DeleteSecurityGroup", "ec2:CreateNetworkInterfacePermission", "ec2:CreateRoute", "ec2:UpdateSecurityGroupRuleDescriptionsEgress", "ec2:UpdateSecurityGroupRuleDescriptionsIngress" ], "Resource": "*", "Condition": { "StringEquals": { "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPCID" } } }, { "Sid": "AllowInstanceActionsTagBased", "Effect": "Allow", "Action": [ "ec2:RebootInstances", "ec2:StopInstances", "ec2:TerminateInstances", "ec2:StartInstances", "ec2:AttachVolume", "ec2:DetachVolume", "ec2:AssociateIamInstanceProfile", "ec2:DisassociateIamInstanceProfile", "ec2:GetConsoleScreenshot", "ec2:ReplaceIamInstanceProfileAssociation" ], "Resource": [ "arn:aws:ec2:region:account_number:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "ec2:ResourceTag/TAG-KEY": "Tag_key" } } }, { "Sid": "AllowCreateTagsOnlyLaunching", "Effect": "Allow", "Action": [ "ec2:CreateTags" ], "Resource": [ "arn:aws:ec2:region:account_number:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "ec2:CreateAction": "RunInstances" } } } ] }注: account_number、region、tag_key、tag_value、VPC-ID、role_name パラメータは実際の値に置き換えてください。
-
[ポリシーの確認] を選択して名前を入力します。
-
[ポリシーを作成] を選択します。
注: 一部の項目は、環境内の特定のリソースに置き換える必要があります。詳細については、「Amazon リソースネーム (ARN) で AWS リソースを識別する」を参照してください。
ポリシーをグループにアタッチする
次の手順を実行します。
- IAM コンソールを開きます。
- ナビゲーションペインで、[ユーザーグループ] を選択し、グループの名前を選択します。
注: ユーザーグループにポリシーをアタッチし、ユーザーを適切なグループに追加するのがベストプラクティスです。 - [アクセス許可を追加] を選択してから [ポリシーをアタッチ] を選択します。
- ポリシーの名前を入力し、ポリシーを選択します。
- [ポリシーをアタッチ] を選択します。
カスタムポリシーをアタッチすると、IAM ID は AWS マネジメントコンソールにアクセスできます。その後、IAM ID は EC2 ダッシュボードを開いて EC2 インスタンスを起動できます。IAM アイデンティティでは、EC2 インスタンスを起動するためのサブネット、VPC、およびタグを指定する必要があります。
ec2:ResourceTags 条件は、以下のアクションを制限します。
- インスタンスを起動する。
- インスタンスを停止する。
- インスタンスを再起動する。
- インスタンスを終了する。
- インスタンスにボリュームをアタッチする。
- インスタンスからボリュームをデタッチする。
- IAM インスタンスプロファイルとインスタンスの関連付けを解除する。
- インスタンスプロファイルを関連付ける。
- インスタンスの IAM インスタンスプロファイルの関連付けを置き換える。
- インスタンスのコンソールのスクリーンショットを取得する。
ポリシーは、VPC に対する以下のアクションを制限します。
- セキュリティグループを削除する。
- ルートを作成、削除する。
- ルートテーブルを削除する。
- ネットワークアクセスコントロールリスト (ネットワーク ACL) を削除する。
- ネットワーク ACL エントリを削除する。
- セキュリティグループの受信ルールと送信ルールを承認する、または無効にする。
- ネットワークインターフェースの権限を作成する。
- 受信ルールと送信ルールのセキュリティグループの説明を更新する。
関連情報
PrincipalTag、ResourceTag、RequestTag、TagKeys 条件キーは IAM ポリシーのアクセスをどのように制御しますか。
- 言語
- 日本語

関連するコンテンツ
- 質問済み 6年前