AWS CLIãæ´»ç¨ãã¦ãã¾ããï¼AWSèªå® DevOps Engineer Professional ãã¨ãã¿ã£ã¡ããã§ããï¼2ã¶æã¶ã2åç®ï¼
以åãAWS CLIã®æ¦è¦ã«ã¤ãã¦ã®ããã°è¨äºãæ稿ãã¾ããã ä»æ¥ã¯ããã§ãä¼ãããããªãã£ã便å©æ©è½ãã¹ã±ã«ãã³ã«ã¤ãã¦ãç´¹ä»ãããã¨æãã¾ããæãã®ã§å¢ãããããã¾ãããã¡ãªã¿ã«ãããæ¸ããªããéä¸ã§3ç®æããããã¤é¡ãã¦ãã®ã§ãèªã¿ãªãããããããã¤å¤åããæ¸ããªãããã¤é¡ãã¦ããªããªã©ã¨åæãªäºãèããªããèªã¿é²ãã¦é ããã°å¹¸ãã§ãã
ããã§ãããã¾ãã¯ãã£ããEC2ã¤ã³ã¹ã¿ã³ã¹ãä¸å°ç«ã¦ãã¨ãã¾ãããã
ããã§ç«ã¦ãEC2ã®è¦ä»¶ã¯ãããªæãã«è¨å®ãã¦ã¿ã¾ããã
- Amazon Linux 2015.03ããã¬ã¼ã³ãªã¾ã¾èµ·å
- ã¤ã³ã¹ã¿ã³ã¹ã¿ã¤ã㯠m4.large
- rootããªã¥ã¼ã (/dev/xvda) ã¯30GB SSD(gp2)
- 詳細ã¢ãã¿ãªã³ã°ã¯ä½¿ããªã
- Terminate Protection 㯠On ã«ãã¦ãã
- ãããªãã¯IPã®èªåã¢ãµã¤ã³ãæå¹ã«ãã
- Security Group㯠"sg-xxxxxxxx" 㨠"sg-yyyyyyyy" ãå²ãå½ã¦ã
- "worker-ec2" ã¨ãã IAM Role ãå²ãå½ã¦ã
ã¾ãã¯ãããæºããã¤ã³ã¹ã¿ã³ã¹ãCLIã§èµ·åãã¦ã¿ã¾ãã
aws ec2 run-instances --profile mydev --count 1 --image-id ami-cbf90ecb --instance-type m4.large --block-device-mappings '[{"DeviceName":"/dev/xvda","Ebs":{"VolumeSize":30,"DeleteOnTermination":true,"VolumeType": "gp2"}}]' --monitoring Enabled=false --disable-api-termination --associate-public-ip-address --iam-instance-profile Name=worker-ec2 --subnet-id subnet-xxxxxxxx --security-group-ids "sg-xxxxxxxx" "sg-yyyyyyyy" --key-name my-key-pair
ã¾ããã¡ã¾ã¡ã¾æ¸ãã®çµæ§ããã©ãããã§ãããCLIã®ã¾ã¾ã ã¨ãã©ã¡ã¼ã¿ãã¡ããã£ã¨å¤ãã¦æµç¨ãããå ´åã«ãä¸ä¾¿ã§ããã³ãããã¦ããã¹ãã¨ãã£ã¿ã§æ¸ãæãã¦ãã¾ãã·ã§ã«ã«æµãè¾¼ãã§ãã¨ãã£ããªãã¬ã¼ã·ã§ã³ã«ãªãã§ãããããã§ããã°ãããããªãã§ãã
ãããè¨å®ãã¡ã¤ã«ã¿ããã«ãã¦ç½®ãã¦ãããã便å©ã§ãããããã¡ã¤ã«ãªãå
±æããã¼ã¸ã§ã³ç®¡çãç°¡åã ããããããããã§ãããã§ããAWS CLIãªãã
--cli-input-jsonãªãã·ã§ã³
ã³ãã³ãè¡ã®ä¸ã§ãªãã·ã§ã³ã¨ãã©ã¡ã¼ã¿ã®ãã¢ãéä¸æå®ãã代ããã«ãJSONå½¢å¼ã§æ¸ããããã©ã¡ã¼ã¿ãã¡ã¤ã«ãæå®ãããã¨ãã§ãã¾ããå
ã®EC2ã®ä¾ã ã¨ãã¾ãã¯ä»¥ä¸ã®ãããªJSONãç¨æãã¾ãã
{ "ImageId": "ami-cbf90ecb", "KeyName": "my-key-pair", "SecurityGroupIds": [ "sg-xxxxxxxx", "sg-yyyyyyyy" ], "InstanceType": "m4.large", "BlockDeviceMappings": [ { "DeviceName": "/dev/xvda", "Ebs": { "VolumeSize": 30, "DeleteOnTermination": true, "VolumeType": "gp2" }, "NoDevice": "" } ], "Monitoring": { "Enabled": false }, "SubnetId": "subnet-xxxxxxxx", "DisableApiTermination": true, "NetworkInterfaces": [ { "DeviceIndex": 0, "AssociatePublicIpAddress": true } ], "IamInstanceProfile": { "Name": "worker-ec2" } }
ä¸è¨ãã¡ã¤ã«ãé©å½ãªãã¹ã«ä¿åãã¦ããã¾ããã³ãã³ãå®è¡æã«ã--cli-input-jsonãªãã·ã§ã³ã®ãã©ã¡ã¼ã¿ã¨ãã¦ãã®ãã¡ã¤ã«ãæå®ãããã¨ã§ããã¡ã¤ã«ã«è¨è¼ããéãã«ãä¸è¨ã®CLIã³ãã³ãã¨åãè¨å®ã®ã¤ã³ã¹ã¿ã³ã¹ãèµ·åãããã¨ãã§ãã¾ãã
aws ec2 run-instances --profile mydev --cli-input-json file://ec2_run-instance.json
ã¡ãªã¿ã«ãCLIã®ãªãã·ã§ã³ã¨JSONãã¡ã¤ã«ã®ä¸¡æ¹ã§ãåããªãã·ã§ã³ã«å¯¾ãéããã©ã¡ã¼ã¿å¤ãåæã«å
¥åããå ´åãCLIã®æ¹ã§æå®ãããã®ãåªå
ããã¾ããã§ãã®ã§ãJSONãã¡ã¤ã«ããã³ãã¬ã¼ã代ããã«ãã¦ãåå¥ã®è¨å®ãCLIãªãã·ã§ã³ã§è£è¶³ãããã¨ãã使ãæ¹ãå½ç¶ããã§ãããã©ã¡ã¼ã¿ã¯åãã§ã¤ã³ã¹ã¿ã³ã¹ã¿ã¤ãã ãå¤ããããã¨ãã£ããã¨ãã§ãã¾ãã
aws ec2 run-instances --profile mydev --cli-input-json file://ec2_run-instance.json --instance-type t2.small
--generate-cli-skeltonãªãã·ã§ã³
ãã¦ãå
ã®--cli-input-jsonãªãã·ã§ã³ã«ä¸ããJSONå½¢å¼ã®ãã©ã¡ã¼ã¿ãã¡ã¤ã«ã§ãããå½ç¶AWSã®APIãæå¾
ãã¦ããããã¥ã¡ã³ãæ§é ã«ãªã£ã¦ããªããã°ä½ãããã®ã¨ã©ã¼ãè¿ããã¾ããã¾ãããã©ã¡ã¼ã¿ããµã¼ãã¹æ¯ã«ããããããã®ã§ããã¡ãã¡èª¿ã¹ãã®ãé¢åã§ããããããå
ã«ææ¸ãããã®ã¯ãããã«éª¨ãæãã¾ãããã¾ã©ãææ¸ãã§å³ãåºãã®ã¯å¹´è³ç¶ããããªããã§ãã
ã¨ããããã§ãAWS CLIã®ãªãã·ã§ã³ã¨ããã©ã¡ã¼ã¿ã®ãã¨ã¯é æ
®ãªãAWS CLIã«èãã¾ããããã³ãã³ããæå®ãã¦ãªãã·ã§ã³ã«--generate-cli-skeltonã渡ãã¨ããã®ã³ãã³ãã«ä¸ãããã¨ã®ã§ãããã©ã¡ã¼ã¿ã®ä¸è¦§ããæ´å½¢ãããJSONå½¢å¼ã¨ããã¼å¤ä»ãã§åºåããã¾ããé©å½ãªãã¡ã¤ã«ã«ãªãã¤ã¬ã¯ããã¦æ¸ãæãã¾ãããã
aws ec2 run-instances --generate-cli-skeleton > ec2run-instance.json
注æç¹ãããã¨ããã°ã--generate-cli-skeltonãªãã·ã§ã³ã¯ããã®ã³ãã³ãã®å®è¡ã«æä½éå¿
è¦ãªãã©ã¡ã¼ã¿ãåæãããã®ã§ã¯ãªããä¸ãããã¨ãã§ããå
¨ã¦ã®ãã©ã¡ã¼ã¿ãããã£ã¨æ¸ã並ã¹ã¦ããããã®ãã¨ããç¹ã§ããã
æä»çãªé¢ä¿ã«ãããã©ã¡ã¼ã¿ãå
¨é¨åºã¦ãã¦ãã¾ãã¾ããä¾ãã°ãec2 run-instanceã®ã¹ã±ã«ãã³ãåºåããã¨ã--iam-instance-profileãªãã·ã§ã³ã«ä¸ãããããã©ã¡ã¼ã¿ã¨ãã¦ã以ä¸ã®ããã«ARNã¨Nameã®2ã¤ãåºåããã¾ãã
"IamInstanceProfile": { "Arn": "", "Name": "" },
ãããããé常ã¯ã©ã¡ããä¸æ¹ãæå®ãããã®ã§ãåæã«æå®ããã¨APIã¯ç¡æ
æ²ã«ã¨ã©ã¼ãè¿ãã¾ãããã®ãããã¯--dry-runãªãã·ã§ã³ï¼APIãªã¯ã¨ã¹ãã®æå¦å¤å®ã ããè¡ããå®éã®ãªã½ã¼ã¹æä½ãè¡ããªããªãã·ã§ã³ï¼ãªã©ãé§ä½¿ãã¦ã®ãã©ã¤ã¢ã³ãã¨ã©ã¼ãå¿
è¦ã«ãªã£ã¦ããã¨ããã§ãã
ã¨ããããã§ãå
¨ã¦ãæ示çã«æå®ããå¿
è¦ã¯ãªãã®ã§ãæä½éæå®ããããã©ã¡ã¼ã¿ãæ®ããæ示çãªæå®ãä¸è¦ãªãã©ã¡ã¼ã¿ãåé¤ãã¦ãããã¨ãã使ãæ¹ãæ£ããã®ã§ã¯ãªããã¨æãã¾ããå¿
è¦ãªãã®ã1ã¤ãã¤èª¿ã¹ã¦ããããæ¸ãã¦ããã®ãããã§ãããä¸è¦ãªãã®ãåé¤ãã¦ããæ¹ãæ ¼æ®µã«æ¥½ã§ãããã¨ã¯JSONå½¢å¼ã®ãã©ã¼ãããããå´©ããªããã°ä½ã¨ããªãã¾ãã
æå¾ã«
--generate-cli-skeltonãªãã·ã§ã³ã¨--cli-input-jsonãªãã·ã§ã³ãä¸æã«ä½¿ããã¨ã§ãAWS CLIã¯ããä¸å±¤ä¾¿å©ã«ä½¿ãããã¨æãã¾ããåè¿°ã®éããã©ã¡ã¼ã¿ãã¡ã¤ã«ãå°ããªãã³ãã¬ã¼ãã«è¦ç«ã¦ã使ãæ¹ãã§ããã®ã§ãä¸å®æçã«æ¢åç°å¢ã«åããããªã³ã³ãã¼ãã³ãã追å ãããªã©ãããããå°åããå¹ãããã«ãªãã¾ããã©ãã©ãæ´»ç¨ãã¦ããããã§ããã