前回S3にhtmlをアップロードしてブラウザから表示するところまで、
でも、
今日はサーバを起動して、
CLIをパワーアップ
補完をきかす
前回S3を使ってみた時に思ったことだけど、
気持ちよくコマンドを操りたい俺はどうにかしてこのコマンドを効率的にかっこ良く打てる方法がないかを探した。
普段気持よく打てている時はそう、
というわけで、
やり方は簡単で、
$ complete -C aws_completer aws
これで大丈夫だった。同じページにはtcshとzshでの方法も書いてあったけど、
jsonを処理する
補完はきいた。AWS CLIのgithubのREADMEに書いてあった、
ここからダウンロードして、
EC2のインスタンスを起動する。
AWSには今まで触ってきたサーバと同じように、
とりあえずインスタンスを1台起動してみて遊んでみることにする。
AMIってなに?
コマンドに補完が効くので、
$ aws ec2 [Tab連打]
Tabを押すと153個ものサブコマンドが出てくる。でも、
aws ec2 run-instances [Tab連打]
Tabを連打したら、
$ aws ec2 run-instances usage: aws [options] <command> <subcommand> [parameters] aws: error: argument --image-id is required
--image-idというのが必須だということがわかった。ちゃんと必要な項目がエラーで出てくるこの安心感。必要な項目はわかったけれど、
EC2と言うのはOSのイメージを指定して起動するらしいので、
$ aws ec2 [Tab連打]
それらしきものを探す。aws ec2 describe-images というのがある。どうもAWSはdescribeという言葉が好きらしく、
$ aws ec2 describe-images
いっぱい出てきた。見るのが面倒くさい位わんさか出てきた。ちょっと出過ぎなので、
すると--fileters というのがあった。これでなんかフィルタリングできそうな気がするけど、
さっきよりはだいぶ短くなったけれど、
ここで、
$ aws ec2 describe-images --owners amazon |jq .[][].ImageId
とするとImageIdだけが取得できることがわかった。これでも結果が沢山あって、
とりあえず、
なんとなくわかった気がするけど、
$ aws ec2 describe-images --owners amazon |jq '.[][] |select(.ImageType == "machine") | {"Name" :.Name,"ImageId":.ImageId }' 〜略〜 { "Name": "Windows_Server-2012-RTM-English-64Bit-SQL_2008_R2_SP2_Express-2014.12.10", "ImageId": "ami-fe1815ff" } { "Name": "Windows_Server-2003-R2_SP2-Language_Packs-64Bit-SQL_2005_SP4_Express-2014.12.10", "ImageId": "ami-fe727fff" } { "Name": "Windows_Server-2008-R2_SP1-Portuguese_Brazil-64Bit-SQL_2008_R2_SP2_Standard-2014.12.10", "ImageId": "ami-fe7974ff" } { "Name": "Windows_Server-2012-R2_RTM-Brazilian-64Bit-Base-2014.12.10", "ImageId": "ami-fe7b76ff" } { "Name": "suse-sles-11-sp3-byos-v20141023-hvm-ssd-x86_64", "ImageId": "ami-fffdcefe" }
このコマンドで、
{ "Name": "amzn-ami-hvm-2012.09.1.x86_64-ebs", "ImageId": "ami-fd7ef9fc" } $ aws ec2 run-instances --image-id ami-fd7ef9fc 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.
なんだかhvmというvirtualization typeだとこのinstance typeは使えないと言っておられる。だけどinstance typeは指定していない。ということはデフォルトで何か指定されていて、
少し調べてみると、
t1とかt2というのは、
というわけで、
$ aws ec2 run-instances --image-id ami-fd7ef9fc --instance-type t2.micro
今度はエラーではなく、
結局散々悩んだ結果、
Security Group
これで無事に起動はできたから、
$ ssh root@ec2-**-**-**-***.ap-northeast-1.compute.amazonaws.com ssh: connect to host ec2-**-**-***-***.ap-northeast-1.compute.amazonaws.com port 22: Operation timed out
正直もう心が折れそうです。このSSHのエラーを見る限りだと、
AWSにはSecurity GroupというFirewallのようなものがある。Security Groupはプロトコルとポートを開いて、
$ aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxx --port 22 --cidr 0.0.0.0/0 --protocol tcp
ポートとプロトコル
ここまで来たら、
$ ssh root@ec2-**-**-**-***.ap-northeast-1.compute.amazonaws.com Warning: Permanently added 'ec2-**-**-***-**.ap-northeast-1.compute.amazonaws.com,**.**.**.***' (RSA) to the list of known hosts. Permission denied (publickey).
そろそろ本気で、
今度はKey Pairだ
sshでログインできないと何も始まらないから、
ちょっとだけ調べてみると、
というわけで、
aws ec2 create-key-pair --key-name id_rsa-cli { "KeyMaterial": "-----BEGIN RSA PRIVATE KEY-----〜略〜 -----END RSA PRIVATE KEY-----", "KeyName": "id_rsa-cli", "KeyFingerprint": "45:4e:28:02:7b:9c:da:a2:35:c0:b4:aa:5c:b4:f8:dd:cf:3d:e7:00" }
KeyMaterialというところが秘密鍵になっているので、
$ chmod 600 .ssh/id_rsa_cli
としたら、
$ ssh -i .ssh/id_rsa_cli ec2-user@ec2-**-**-***-**.ap-northeast-1.compute.amazonaws.com Warning: Permanently added 'ec2-54-64-102-48.ap-northeast-1.compute.amazonaws.com,54.64.102.48' (RSA) to the list of known hosts. Permission denied (publickey).
あと少しなのに、
というわけで、
$ aws ec2 terminate-instances --instance-ids i-xxxxxxxxx
とinstance-idを指定してこのコマンドを叩けばよい。これでお金がかからないと思うと恐ろしい時代になったなと思う。減価償却とかサーバ利用率とは何だったんだろうか。
改めてEC2を立ち上げる。
$ aws ec2 run-instances --image-id ami-fd7ef9fc --instance-type t2.micro --key-name id_rsa-cli
--key-nameにさっき作ったkeypairの名前を指定して、
やっとSSHでつなげた
そして…、
Last login: Wed Jan 7 05:15:30 2015 from kd106155124035.au-net.ne.jp __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2012.09-release-notes/ There are 50 security update(s) out of 227 total update(s) available Run "sudo yum update" to apply all updates. Amazon Linux version 2014.09 is available.
ミドルウェアをどうこうしようと思ったけれど、
今日は深く調べなかったけど、
心が折れそうと言いながらも、
