AWS IAMã«ãã権éè¨å®ã®ãããã©ããã¨ãå¹ççãªãããã°æ¹æ³
Amazon Web Serviceã®IAM(Identity and Access Management)ã¯ãAWSã®å種ãµã¼ãã¹ã«å¯¾ãã¦ã®ã¢ã¯ã»ã¹å¶å¾¡ã(çµæ§ç´°ãã)è¨å®ããããã®ã·ã¹ãã ã§ãã
ãã ãããã¤ãæããããå¶ç´ã«ãå¶éãããããããããããã¨ããããã£ãã®ã§ãã¡ã¢ãã
ã·ããªãª
Jenkinsçµç±ã§ãç¹å®ã®AMIããã®ã¿ãEC2ã¤ã³ã¹ã¿ã³ã¹ãä¸æçã«ç«ã¡ä¸ããã(run-instances)ãæ¶ãããããã(terminate-instances)ã åæ¢(stop-instances)ãããåé(start-instances)ããããã¨ããã©ã¤ããµã¤ã¯ã«ã§ã¯ãªãã 誤ã£ã¦å ¨ç¶é¢ä¿ãªãã¤ã³ã¹ã¿ã³ã¹ãæ¶ããªãããã«å¶ç´ãã¤ãããããé¢ä¿ãªãAMIããç«ã¡ãããããªãããã«å¶éãããã ã¤ãã§ã«ã¤ã³ã¹ã¿ã³ã¹ã¿ã¤ãã«ãã¼ãããã ã¾ããå®å ¨ã®ããç¹å®ã®IPããã®ã¿ã¢ã¯ã»ã¹ã§ããããã«ãããã
ãã®ãããªã·ããªãªã§ããä¸å¿ããã£ã½ã権éã§è¨å®ãããã¨ãã§ãã¾ãã
IAMããªã·ã¼ãã¡ã¤ã«ã®èª¬æ
IAMã®ããªã·ã¼ãã¡ã¤ã«ã¯ãä¸è¨ã®ãããªæãã§ãã
{ "Version": "2012-10-17", // ãã¼ã¸ã§ã³ã¯2012-10-17ã§åºå® "Statement": [ { "Effect": "Allow", // 許å¯ããã®ãæå¦ããã®ããããã©ã«ãã¯å ¨ã¦"Deny"ãªã®ã§ã"Allow"ãè¨è¼ãã¦ããæã "Action": [ "ec2:TerminateInstances" ], // ä½ã®ã¢ã¯ã·ã§ã³ã許å¯|æå¦ããã®ãã "Resource": [ "arn:aws:ec2:ap-northeast-1:1234567890:instance/*" ], // ãã®ã¢ã¯ã·ã§ã³ã¯ã©ã®ãªã½ã¼ã¹(ã¤ã³ã¹ã¿ã³ã¹ã¨ãã»ãã¥ãªãã£ã¼ã°ã«ã¼ãã¨ã)ã¸ã¢ã¯ã»ã¹å¯è½ã "Condition": { // ã©ã®ãããªæ¡ä»¶ä¸ã§ã®ã¿ããã®Statementãæå¹ã "StringEquals": { "ec2:InstanceType": "t2.micro" } } } ] }
Statementãé åãªã®ã§ããã£ã±ãå¢ããã¦ããæãã
ãªã½ã¼ã¹æ¡ä»¶ã¯ã¢ã¹ã¿ãªã¹ã¯( "*"
ã¨ã "arn:aws:ec2:ap-northeast-1:1234567890:instance/*"
)ã§æå®ãããã¨ãã§ãããããªã½ã¼ã¹ãçµãè¾¼ãã°ããã®ãªã½ã¼ã¹ã«ããã¢ã¯ã»ã¹ã§ããªããã¨ããç¶æ
ãæ
ä¿ã§ãã( "arn:aws:ec2:ap-northeast-1:1234567890:instance/id-hogehoge"
)ã
ãªã½ã¼ã¹æå®ã§ããã¢ã¯ã·ã§ã³ã«å¶ç´ããã
ãªã½ã¼ã¹æå®ã«ãã£ã¦IAMã«å¶ç´ããããããã»ã»ã»ã¨æããããå ¨ã¦ã®ã¢ã¯ã·ã§ã³ããªã½ã¼ã¹æå®ã§ããããã§ã¯ããã¾ããã
ç¹ã«ãDescribeInstancesãªã©ã®GETç³»ã¯ã ããããã¡ã§ãä»ã«ãCreateTagsããã¡ã§ãããã®ä¸è¦§ã¯ãã¡ã
ä¸è¨ã®ãããªresourceæå®ãã¦ãã¾ãã¨ããã®Statementã¯ç¡å¹ã«ãªãããã®æä½ã¯è¨±å¯ãããªããªã£ã¦ãã¾ãã¾ãã
ãªã½ã¼ã¹ã«æå®ã§ããã®ã¯ "*"
ã ãã§ãã
{ "Effect": "Allow", "Action": "ec2:DescribeInstances", "Resource": [ "arn:aws:ec2:ap-northeast-1:1234567890:instance/*" ], // NGãªä¾ }
ã¢ã¯ã·ã§ã³ã¨ãªã½ã¼ã¹ã¨æ¡ä»¶ã®çµã¿åããã«å¶ç´ããã
ããã«æ¸ãã¦ããéãã§ãããã¢ã¯ã·ã§ã³ãã¨ã«æå®ã§ãããªã½ã¼ã¹ã«ç¨®é¡ãããããªã½ã¼ã¹ãã¨ã«æå®ã§ããæ¡ä»¶ãã¼ã決ã¾ã£ã¦ãã¾ãã
ä¾ãã°ãTerminateInstancesã®ã¢ã¯ã·ã§ã³ã¯ãã¤ã³ã¹ã¿ã³ã¹IDã«é¢ãããªã½ã¼ã¹ã®ã¿æå®ã§ãã¾ãã RunInstancesã¯ã¤ã¡ã¼ã¸ãã¤ã³ã¹ã¿ã³ã¹ããã¼ãã¢ãetcããããæå®ã§ãã¾ããéã«ãæå®ããªãã¨ãã®ãªã½ã¼ã¹ã使ããã¨ã¯ã§ãã¾ãããä¾ãã°ãæ¢åã®ãã¼ãã¢ã使ã£ã¦ã¤ã³ã¹ã¿ã³ã¹ãä½æããããªãããã¼ãã¢ã«å¯¾ãããªã½ã¼ã¹æå®ãå¿ è¦ã«ãªãã¾ãã
RunInstancesã®ããã«è¤æ°ã®ãªã½ã¼ã¹æå®ãããå ´åã«æ³¨æãå¿ è¦ãªã®ããè¤æ°ã®ãªã½ã¼ã¹ã§è¨å®ã§ããæ¡ä»¶ãã¼ãç°ãªããã¨ã§ãã ä¾ãã°ãã¤ã¡ã¼ã¸ã«ã¯ãInstanceTypeããæå®ã§ãã¾ããããã¼ãã¢ã«ã¯ãRegionãããæå®ã§ãã¾ããã ãã®ããã次ã®ãããªè¨å®ãããã¨ãããã®ãã¼ãã¢ã«ã¢ã¯ã»ã¹ã§ããªããï¼ï¼ãã£ã¦è¨ããã¾ãã
{ "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:ap-northeast-1:1234567890:instance/*", "arn:aws:ec2:ap-northeast-1:1234567890:key-pair/test-key-pair" ], "Condition": { "StringEquals": { "ec2:InstanceType": "t2.micro" } } }
ãã®å ´åã¯ããã¨ãªããï¼ã¤ã®Statementã«åå²ãã¾ãã
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:ap-northeast-1:1234567890:instance/*" ], "Condition": { "StringEquals": { "ec2:InstanceType": "t2.micro" } } }, { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:ap-northeast-1:1234567890:key-pair/test-key-pair", // ãã®ä»ã®ãªã½ã¼ã¹è¨±å¯ã®æå® ] } ] }
ãã®ã·ããªãªãããªã½ã¼ã¹ã¿ã°ãã§ã¯ãªã¢ããã®ã¯å³ãã
AWSã®ããã°ã«ã¦è¨åããã¦ãã¾ããããªã½ã¼ã¹ã¿ã°ãæ´»ç¨ããã°ãç¹å®ã®AMIãããç¹å®ã®ã¿ã°ãè¨å®ããã¤ã³ã¹ã¿ã³ã¹ãä½æããç¹å®ã®ã¿ã°ã®ã¤ã³ã¹ã¿ã³ã¹ã®ã¿åé¤ã§ãããã¨ãããã¨ãã§ãããã§ã(ã¾ãã§ããªããã§ããã©)ã ä½ãStatementã¨ãã¦ã¯ã
- ç¹å®ã®AMIããã®ã¿
run-instances
ã§ããStatement - ç¹å®ã®ResourceTagãæã¤ã¤ã³ã¹ã¿ã³ã¹ã®ã¿
terminate-instances
ã§ããStatement
ã®ï¼ã¤ãä½ããã¨ããæãã
ãããã run-instances
ããéã«ResourceTagã¯è¨å®ã§ãã(ä¾ãã°ã¤ã³ã¹ã¿ã³ã¹åã¨ã)ã create-tags
ããªãã¨ãããªãã®ã§ãããCreateTagsã¢ã¯ã·ã§ã³ã«ã¯ãªã½ã¼ã¹æ¡ä»¶ãªã©ãè¨å®ã§ããªãã®ã§ã(ï¼å
¨è¨±å¯ããã§ããªã)ãããã°ã§ãã³ã¡ã³ãæ¬ã§çªã£è¾¼ã¾ãã¦ãã¾ãã
create / terminate ã®ã©ã¤ããµã¤ã¯ã«ã§ã¯ãªããstart / stop ã®ã©ã¤ããµã¤ã¯ã«ã§ããã°ããã®æ¹å¼ã¯è¯ãããã§ãããã ä»åã®å ´åã¯ãã¤ã³ã¹ã¿ã³ã¹ã¯æ¶ãå»ãããã£ãã®ã§ããã®æ¹å¼ã¯åãã¾ããã§ããã
IPå¶å¾¡ãã§ããããIPã¯ã°ãã¼ãã«IP
ec2ã«éããããã©ãã®IPãããªã¯ã¨ã¹ãããã£ããï¼ããæ¡ä»¶ã¨ã§ããã aws:SourceIp
ã¨ããæ¡ä»¶ãã¼ãããã¾ããæå®ããå ´åã¯ãä¸çªæåã«Denyãã¦ããã¨è¯ãã§ãã
{ "Effect": "Deny", "Action": "*", "Resource": "*", "Condition": { "NotIpAddress": { "aws:SourceIp": "123.45.67.89" } } }
ä¸ç¹æ³¨æç¹ãªã®ããIPã¯ã°ãã¼ãã«IPã¨ãã¦å±éããã¾ãããªã®ã§ãEC2ã¤ã³ã¹ã¿ã³ã¹ä¸ããã®ã¿ã俺ãTerminateãã¦ãããã¨ããå¦çãã§ããããã«å¶éãããå ´åãã°ãã¼ãã«IPãäºåã«ããã£ã¦ããªãã¨ããã¾ããã
IAMPolicy Simulatorã使ãã¥ãã
IAM Policy Simulatorã¯æ£ç´ä½¿ãã¥ããã§ããæ¡ä»¶ã«åè´ããªãã¨ãã«ã¯ããä½ãä¸è´ãã¾ããã§ãããã¨ããè¨ããã¾ãããä½ããã¡ã ã£ãã®ããããããªãã§ãã
ããã§ãAWS CLIããå®éã«dry-runããã¨è¯ãã§ãã
aws ec2 run-instances --dry-run --image-id=ami-abcdefg --count=1 --instance-type t2.micro
ããããã¨ã権éãä¸è¶³ãã¦ãã°ãã¨ã³ã³ã¼ããããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºã¦ãã¾ããã¨ã©ã¼ã¡ãã»ã¼ã¸ã®ãã³ã¼ã㯠Management Consoleの権限不足エラーをデコードする | Developers.IO ãåèã«ãresource
ã¨ããé
ç®ãè¦ãã¨è¯ãã§ãããã®resourceã«å¯¾ãã¦ãé©åãªæ¡ä»¶ãè¨å®ããã¦ããªããã¨ãããã¨ããããã¾ãã
zshã®å ´åã¯ããããªæãã§é¢æ°ä½ãã¨ãããã°ã楽ã§ã(bashã¯ç¥ãã)ã
function sts() { aws sts decode-authorization-message --encoded-message $1 | jq -r ".DecodedMessage" | jq -c ".context.resource" } sts "ã¨ã³ã³ã¼ããããã¨ã©ã¼ã¡ãã»ã¼ã¸"
æçµçã«åã£ãæ段
çµå±ããã¾ãTerminateInstancesã®æ¡ä»¶ãæå®ã§ããªããããTerminateInstancesã®æ¨©éãä¸ããã®ã¯ããã¾ããã 代ããã«ã次ã®æ段ãåãã¾ããã
- EC2ã®ä½ææãshutdownæã®åä½ããterminate(åé¤)ãã«ãªãããã«ãã
- AWS CLIçµç±ã§
terminate-instances
ããã®ããããEC2ã¤ã³ã¹ã¿ã³ã¹å ã§shutdown -h now
ãã
shutdownæã®åä½ãterminateã«ããã«ã¯ã
aws ec2 run-instances --image-id=ami-abcdefg --count=1 --instance-type t2.micro --instance-initiated-shutdown-behavior=terminate
ã¨ããããã«ãã --instance-initiated-shutdown-behavior=terminate
ããã¤ãã¾ããããããã°ãterminate-instances
ããªãã¦ããshutdownããã ãã§åããã¨ãã§ãã¾ãã
æçµçãªããªã·ã¼ãã¡ã¤ã«ã¯ãä¸è¨ã®ãããªæããæãåºããªããæ¸ãã¦ãããããããééã£ã¦ããããããããã©ã
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": "*", "Resource": "*", "Condition": { "NotIpAddress": { "aws:SourceIp": "123.45.67.89" } } }, { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": "arn:aws:ec2:ap-northeast-1:1234567890:instance/*", "Condition": { "StringEquals": { "ec2:InstanceType": "t2.micro" } } }, { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:ap-northeast-1::image/ami-abcdefg", "arn:aws:ec2:ap-northeast-1:1234567890:security-group/*", "arn:aws:ec2:ap-northeast-1:1234567890:network-interface/*", "arn:aws:ec2:ap-northeast-1:1234567890:subnet/*", "arn:aws:ec2:ap-northeast-1:1234567890:volume/*", "arn:aws:ec2:ap-northeast-1:1234567890:key-pair/test-key-pair" ] } ] }