開発環境や検証環境、学習環境などで、金額の高いインスタンスタイプは使用しないように制限したいといった場合があります。
そのような場合のポリシー例です。nano から large までのインスタンスタイプは起動できますが、それ以上のサイズは起動できないポリシーです。
EC2 単体だけではなく、AutoScaling による起動も制限できます。また、明示的に”Deny” で指定することで、他のポリシーで許可されても無効化されます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Sid": "AllowPolicy", | |
"Effect": "Allow", | |
"Action": [ | |
"ec2:*", | |
"autoscaling:*" | |
], | |
"Resouce": "*" | |
}, | |
{ | |
"Sid": "DenySpecificInstanceTypeOfEC2", | |
"Effect": "Deny", | |
"Action": [ | |
"ec2:RunInstances", | |
"ec2:StartInstances" | |
], | |
"Resource": "arn:aws:ec2:*:*:instance/*", | |
"Condition": { | |
"StringNotLike": { | |
"ec2:InstanceType": [ | |
"*.nano", | |
"*.micro", | |
"*.small", | |
"*.medium", | |
"*.large" | |
] | |
} | |
} | |
}, | |
{ | |
"Sid": "DenySpecificInstanceTypeOfEC2ForLaunchConfig", | |
"Effect": "Deny", | |
"Action": [ | |
"autoscaling:CreateLaunchConfiguration" | |
], | |
"Resource": "*", | |
"Condition": { | |
"StringNotLike": { | |
"autoscaling:InstanceType": [ | |
"*.nano", | |
"*.micro", | |
"*.small", | |
"*.medium", | |
"*.large" | |
] | |
} | |
} | |
}, | |
{ | |
"Sid": "DenySpecificInstanceTypeOfEC2ForASG", | |
"Effect": "Deny", | |
"Action": [ | |
"autoscaling:CreateAutoScalingGroup" | |
], | |
"Resource": "*", | |
"Condition": { | |
"ForAnyValue:StringNotLikeIfExists": { | |
"autoscaling:InstanceTypes": [ | |
"*.nano", | |
"*.micro", | |
"*.small", | |
"*.medium", | |
"*.large" | |
] | |
} | |
} | |
} | |
] | |
} |
参考資料:
AWS のサービスのアクション、リソース、および条件キー – AWS Identity and Access Management
複数のキーまたは値による条件の作成 – AWS Identity and Access Management
IAM JSON ポリシーエレメント: 条件演算子 – AWS Identity and Access Management