LoginSignup
52
52

More than 5 years have passed since last update.

AWS-CLI EC2でおなじものをつくるよ

Last updated at Posted at 2016-02-01

ある日のこと。
現在稼動しているEC2インスタンスと同じものを作ってね。という依頼がきました。

「GUIは中学生まで・・」※
という言葉を胸にとりあえず、CLIでチャレンジしてみました。

※正しくは・・・「本番環境で変更を伴う作業をマネジメントコンソールでやって良いのは中学生かAWS経験半年まで」です!

bohebohe_ec2.jpg

EC2のコマンドライン操作

こちらのリファレンスを片手にやるとよいです。最新の使えるコマンドが列挙されています。
http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html#cli-aws-ec2

コマンドは、いきなりやるとコワイので、ドライランをつけてやりましょう。

--dry-run

おなじものをつくるよSTEP

やり方は複数あると思いますが、今回はコピー元のEC2インスタンスのAMIを取得して、それを元にインスタンスを作成します。

AMIイメージを作成する

まず、コピーしたいEC2インスタンスのAMIイメージを作成します。
このときに、マシンはとめておきましょう。メモリ上にデータが残っていたりします。

aws ec2 create-image --instance-id i-XXXXXX --no-reboot --name test_20160106100726

この時、うーん自分のコピーしたいAMIインスタンスのインスタンスIDはなんだろうっていうのを調べる時は、以下のコマンドを使います。

  • リージョン内で稼働している全インスタンスの情報を取得する場合
$ aws ec2 describe-instances
  • また、インスタンスIDがわかって、そのインスタンスの情報を知りたい場合は、以下のコマンドで情報を取得していきます。
$ aws ec2 describe-instances --instance-ids ${instance-id}

参考URL: http://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html

さて実行

dry-runをつけていると、いろんなエラーにあたりつつ、前進していけます。

  • エラーその1:セキュリティグループとサブネットがあっていないようです。
% aws ec2 run-instances  --dry-run --image-id test_20160106100726 --key-name demo --count 1 --security-group-ids sg-XXXX  --instance-type t1.micro

A client error (InvalidParameter) occurred when calling the RunInstances operation: Security group sg-XXXX and subnet subnet-XXXX belong to different networks.
  • エラーその2:指定するAMIの名前がちがうようです
% aws ec2 run-instances  --dry-run --image-id test_20160106100726 --key-name demo --count 1 --security-group-ids sg-XXXX  --subnet-id subnet-XXXX --instance-type t1.micro

A client error (InvalidAMIID.Malformed) occurred when calling the RunInstances operation: Invalid id: "test_20160106100726" (expecting "ami-...")
  • エラーその3:どうやら、このインスタンスタイプ( --instance-type t1.micro)の実行はGUIからのみのようです。
% aws ec2 run-instances  --dry-run --image-id ami-XXXX  --key-name demo --count 1 --security-group-ids sg-XXXX  --subnet-id subnet-XXXX --instance-type t1.micro

A client error (InvalidParameterCombination) occurred when calling the RunInstances operation: Non-Windows instances with a virtualization type of 'hvm' are currently not supported for this instance type.
  • お、うまく通ったようです。
% aws ec2 run-instances  --dry-run --image-id ami-XXXX  --key-name demo --count 1 --security-group-ids sg-XXXX  --subnet-id subnet-XXXX --instance-type t2.micro

A client error (DryRunOperation) occurred when calling the RunInstances operation: Request would have succeeded, but DryRun flag is set.
  • では、dry-runを外して実行してみましょう。
% aws ec2 run-instances  --image-id ami-XXXX --key-name demo --count 1 --security-group-ids sg-XXXX  --subnet-id subnet-XXXX --instance-type t2.micro

813931856455    r-cf4d9167
INSTANCES   0   x86_64      False   xen ami-XXXX    i-df1e7d5e  t2.micro    demo    2016-01-06T01:34:05.000Z    ip-10-0-0-57.ec2.internal   10.0.0.57       /dev/xvda   ebs True        subnet-XXXX hvm vpc-XXXX
MONITORING  disabled
NETWORKINTERFACES       12:1d:84:4d:fe:83   eni-XXXX    813931856455    ip-10-0-0-57.ec2.internal   10.0.0.XX   True    in-use  subnet-XXXX vpc-XXXX
ATTACHMENT  2016-01-06T01:34:05.000Z    eni-attach-XXXX True    0   attaching
GROUPS  sg-XXXX berio-api-security-group
PRIVATEIPADDRESSES  True    ip-10-0-0-XX.ec2.internal   10.0.0.XX
PLACEMENT   us-east-1b      default
SECURITYGROUPS  sg-XXXX     XXXX-security-group
STATE   0   pending
STATEREASON pending pending
OK  ~/.aws

インスタンスの情報設定

  • できあがったインスタンスの情報を取得してみましょう
aws ec2 describe-instances --instance-id i-XXXX --output json
  • タグの名前をつけてみましょう
aws ec2 create-tags --resources  i-XXXX --tags '[{"Key": "Name", "Value": "Test"}]'
  • 固定IPを付与してみましょう
aws ec2 associate-address --allocation-id eipalloc-XXX --network-interface-id eni-XXXXX

最後に

dry-runをつけながら、すこしずつコマンドを実行してデバッグしながら、前進しましょう。
最初はうまくいかないけど、やっていくうちにアタリをつける能力がすこしづつあがります。

Let's enjoy your AWS-CLI Life!

52
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
52
52