Amazon EC2用Centos5イメージを作って、EC2上で動作させてみる(5)

logo_aws マシンイメージの実行、停止、マシンイメージの廃棄

さて、いよいよ前回までに作成・登録したAMIを使って、インスタンスを起動してみる。また用済み後の停止、マシンイメージの削除手順も記載しておく。

5.イメージ実行準備

(1)秘密鍵の生成


$ ec2-add-keypair centos5| sed -e "1d" > ~/.ssh/id_rsa-centos5
$ chmod 400 ~/.ssh/id_rsa-centos5

$ ec2-describe-keypairs
KEYPAIR centos5 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

(2)F/W設定

以下では、ssh,http,httpsについて、xxx.xxx.xxx.xxx/32 と yyy.yyy.yyy.yyy/29 からの接続を許可する。


$ ec2-authorize default -p 22 -s xxx.xxx.xxx.xxx/32 -s yyy.yyy.yyy.yyy/29
GROUP           default
PERMISSION              default ALLOWS  tcp     22      22      FROM    CIDR    xxx.xxx.xxx.xxx/32
PERMISSION              default ALLOWS  tcp     22      22      FROM    CIDR    yyy.yyy.yyy.yyy/29

$ ec2-authorize default -p 80 -s xxx.xxx.xxx.xxx/32 -s yyy.yyy.yyy.yyy/29
PERMISSION              default ALLOWS  tcp     80      80      FROM    CIDR    xxx.xxx.xxx.xxx/32
PERMISSION              default ALLOWS  tcp     80      80      FROM    CIDR    yyy.yyy.yyy.yyy/29

$ ec2-authorize default -p 443 -s xxx.xxx.xxx.xxx/32 -s yyy.yyy.yyy.yyy/29
PERMISSION              default ALLOWS  tcp     443     443     FROM    CIDR    xxx.xxx.xxx.xxx/32
PERMISSION              default ALLOWS  tcp     443     443     FROM    CIDR    yyy.yyy.yyy.yyy/29

6.イメージ実行

(1)インスタンスの起動

登録済みイメージのリスト


$ ec2-describe-images -o self
IMAGE   ami-xxxxxxxx    my-private-bucket/centos5.img.manifest.xml     938654150775    available       private         i386    machine

イメージの実行


$ ec2-run-instances ami-xxxxxxxx -k centos5
RESERVATION     r-xxxxxxxx      938654150775    default
INSTANCE        i-xxxxxxxx      ami-xxxxxxxx                    pending centos5 0               m1.small        2009-01-15T02:50:31+0000        us-east-1a

イメージの実行状態の確認

しばらくすると、ホスト名が設定される


$ ec2-describe-instances
RESERVATION     r-xxxxxxxx      938654150775    default
INSTANCE        i-xxxxxxxx      ami-xxxx    ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com       domU-xx-xx-xx-xx-xx-xx.compute-1.internal       running centos5 0               m1.small        2009-01-15T09:29:39+0000        us-east-1a

(2)インスタンスへの接続

ssh接続


$ ssh -l root -i ~/.ssh/id_rsa-centos5 ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com
[root@domU-xx-xx-xx-xx-xx-xx ~]#

マウント確認

/dev/sda2には、ちゃんと150GBのHDDがマウントされている。


[root@domU-xx-xx-xx-xx-xx-xx ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1              4128448    904192   3014544  24% /
none                    870472         0    870472   0% /dev/shm
/dev/sda2            153899044    192072 145889348   1% /mnt

7.インスタンスの終了とイメージの廃棄

(1)インスタンスの終了

実行中インスタンスをリスト


$ ec2-describe-instances

インスタンスを終了


$ ec2-terminate-instances i-xxxxxxxx
INSTANCE        i-xxxxxxxx      running shutting-down

(2)AMIの廃棄

不要になったマシンイメージは、紛らわしいので、登録解除しておく。

amiの登録解除


$ ec2-deregister ami-xxxxxxxx

S3からの削除

不要になったマシンイメージは、ストレージ費用節約のために削除しておく。
0.1$/GB・月だから、余り神経質に消さなくても大丈夫だけどね。


$ ec2-delete-bundle -b my-private-bucket -p centos5.img \
-a aaaaaaaaaaaaaaaaaaaa -s ssssssssssssssssssssssssssssssssssssssss
Deleting files:
   - centos5.img.manifest.xml
   - centos5.img.part.00
   - centos5.img.part.01
   - centos5.img.part.02
   - centos5.img.part.03
   - centos5.img.part.04
   - centos5.img.part.05
   - centos5.img.part.06
   - centos5.img.part.07
   - centos5.img.part.08
   - centos5.img.part.09
   - centos5.img.part.10
   - centos5.img.part.11
   - centos5.img.part.12
   - centos5.img.part.13
   - centos5.img.part.14
   - centos5.img.part.15
   - centos5.img.part.16
   - centos5.img.part.17
   - centos5.img.part.18
   - centos5.img.part.19
   - centos5.img.part.20
   - centos5.img.part.21
   - centos5.img.part.22
   - centos5.img.part.23
   - centos5.img.part.24
   - centos5.img.part.25
   - centos5.img.part.26
   - centos5.img.part.27
Continue [y/N]: y
Deleted centos5.img.manifest.xml
Deleted centos5.img.part.00
Deleted centos5.img.part.01
Deleted centos5.img.part.02
Deleted centos5.img.part.03
Deleted centos5.img.part.04
Deleted centos5.img.part.05
Deleted centos5.img.part.06
Deleted centos5.img.part.07
Deleted centos5.img.part.08
Deleted centos5.img.part.09
Deleted centos5.img.part.10
Deleted centos5.img.part.11
Deleted centos5.img.part.12
Deleted centos5.img.part.13
Deleted centos5.img.part.14
Deleted centos5.img.part.15
Deleted centos5.img.part.16
Deleted centos5.img.part.17
Deleted centos5.img.part.18
Deleted centos5.img.part.19
Deleted centos5.img.part.20
Deleted centos5.img.part.21
Deleted centos5.img.part.22
Deleted centos5.img.part.23
Deleted centos5.img.part.24
Deleted centos5.img.part.25
Deleted centos5.img.part.26
Deleted centos5.img.part.27
ec2-delete-bundle complete.

以上で作業は終了。

8.費用の確認

ここまでに要した費用は、おおむね2ドルくらいだった。
費用は、
Account Activity
で、また、費用の内訳は、
Usage Reports
で確認できる。


1.AWSのアカウントを取得して、ツールのセットアップを行う 
2.Centos5のディスクイメージを作成する。
3.Centos5のイメージを、EC2向けにカスタマイズする。
4.作成したOSイメージを加工して、S3にアップし、EC2のマシンイメージとして登録する。
5.マシンイメージの実行、停止、マシンイメージの廃棄。

Leave a Reply