IAMのポリシー設定を、AWSリソースに付与されたタグによって管理する方法
タグで管理できることを知らず、便利だと思ったので、メモしておきます。
EC2の場合
サンプルとなるのは、この公式サイトですね。
Condition Keyで指定できる条件一覧です。タグの他に、VolumeSizeとかでも指定できたんですね。
ポリシーの構造 - Amazon Elastic Compute Cloud
なお、Condition Keyで指定できるAPIには条件があります。該当のAPIが、利用したいCondition Keyに対応しているかどうか、以下のマニュアルより事前に確認しておいてください。
詳細に設定されたアクセスコントロールのための IAM ポリシー条件の使用 - Amazon Relational Database Service
以下のポリシーの例は、あるタグが設定されている場合に、EC2の開始/停止/再起動、および既存セキュリティグループの編集ができる権限です。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:Describe*"
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ec2:RevokeSecurityGroupIngress",
"ec2:RevokeSecurityGroupEgress",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:UpdateSecurityGroupRuleDescriptionsEgress",
"ec2:UpdateSecurityGroupRuleDescriptionsIngress",
"ec2:GetConsoleScreenshot",
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:RebootInstances"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"ec2:ResourceTag/TagKey": "TagValue"
}
}
}
]
}
RDSの場合
RDSにおいても、考え方は同じです。Condition Keyで利用できる値は同様にマニュアルに記載あり、以下はタグに関するものです。
詳細に設定されたアクセスコントロールのための IAM ポリシー条件の使用 - Amazon Relational Database Service
以下のポリシー例は、タグで指定されているRDSの開始/停止/再起動を実行するものと、タグで指定されたDBパラメータグループの編集権限をもったものになります。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"rds:Describe*",
"rds:ListTagsForResource"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"rds:RebootDBInstance",
"rds:StopDBInstance",
"rds:StartDBInstance"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"rds:db-tag/TagKey": "TagValue"
}
}
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"rds:ModifyDBParameterGroup"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"rds:pg-tag/TagKey": "TagValue"
}
}
}
]
}