2016年11月19日土曜日

aws-cli で「削除保護」を解除してEC2インスタンスを削除する


削除保護されたインスタンスを削除しようとすると、以下のようにエラーになります。

[root@centos6 ~]# aws ec2 terminate-instances --instance-ids i-bca3812f

An error occurred (OperationNotPermitted) when calling the TerminateInstances operation: The instance 'i-bca3812f' may not be terminated. Modify its 'disableApiTermination' instance attribute and try again.

そこで、以下のようにして、「削除保護」を無効にします。

[root@centos6 ~]# aws ec2 modify-instance-attribute --instance-id i-bca3812f --no-disable-api-termination

これで、以下のようにインスタンスが削除できます。

[root@centos6 ~]# aws ec2 terminate-instances --instance-ids i-bca3812f
-------------------------------
|     TerminateInstances      |
+-----------------------------+
||   TerminatingInstances    ||
|+---------------------------+|
||        InstanceId         ||
|+---------------------------+|
||  i-bca3812f               ||
|+---------------------------+|
|||      CurrentState       |||
||+-------+-----------------+||
||| Code  |      Name       |||
||+-------+-----------------+||
|||  32   |  shutting-down  |||
||+-------+-----------------+||
|||      PreviousState      |||
||+---------+---------------+||
|||  Code   |     Name      |||
||+---------+---------------+||
|||  16     |  running      |||
||+---------+---------------+||


aws-cli を使ってインスタンスIDからボリュームを探す


あるインスタンスIDで使用しているボリュームを aws-cli で探します。

[root@centos6 ~]# aws ec2 describe-volumes --filters "Name=attachment.instance-id,Values=i-bca3812f" --output table
---------------------------------------------------------
|                    DescribeVolumes                    |
+-------------------------------------------------------+
||                       Volumes                       ||
|+---------------------+-------------------------------+|
||  AvailabilityZone   |  us-east-1b                   ||
||  CreateTime         |  2016-11-19T06:41:58.265Z     ||
||  Encrypted          |  False                        ||
||  Iops               |  100                          ||
||  Size               |  8                            ||
||  SnapshotId         |  snap-9dc0730b                ||
||  State              |  in-use                       ||
||  VolumeId           |  vol-c962b558                 ||
||  VolumeType         |  gp2                          ||
|+---------------------+-------------------------------+|
|||                    Attachments                    |||
||+----------------------+----------------------------+||
|||  AttachTime          |  2016-11-19T06:41:58.000Z  |||
|||  DeleteOnTermination |  True                      |||
|||  Device              |  /dev/sda1                 |||
|||  InstanceId          |  i-bca3812f                |||
|||  State               |  attached                  |||
|||  VolumeId            |  vol-c962b558              |||
||+----------------------+----------------------------+||


aws-cli で EC2 インスタンスを作成する


aws-cli でEC2インスタンスを作ります。

まず、--dry-run を指定して、引数などに間違いがないかチェックしてみます。

[root@centos6 ~]# aws ec2 run-instances --image-id ami-1c221e76 --key-name virginia_key --security-group-ids sg-bbf176df --instance-type t2.micro --placement "AvailabilityZone=us-east-1b,GroupName=,Tenancy=default" --block-device-mappings "DeviceName=/dev/sda1,Ebs={DeleteOnTermination=true,VolumeType=gp2}" --monitoring "Enabled=false" --disable-api-termination --private-ip-address 10.0.11.10 --associate-public-ip-address --subnet-id subnet-f528a2ad --count 1 --output table --dry-run

An error occurred (DryRunOperation) when calling the RunInstances operation: Request would have succeeded, but DryRun flag is set.


次に --dry-run を外して、実行します。
EC2インスタンスの作成に成功すると、以下のように表示されます。

[root@centos6 ~]# aws ec2 run-instances --image-id ami-1c221e76 --key-name virginia_key --security-group-ids sg-bbf176df --instance-type t2.micro --placement "AvailabilityZone=us-east-1b,GroupName=,Tenancy=default" --block-device-mappings "DeviceName=/dev/sda1,Ebs={DeleteOnTermination=true,VolumeType=gp2}" --monitoring "Enabled=false" --disable-api-termination --private-ip-address 10.0.11.10 --associate-public-ip-address --subnet-id subnet-f528a2ad --count 1 --output table
-----------------------------------------------------------
|                      RunInstances                       |
+----------------------------+----------------------------+
|  OwnerId                   |  544509011205              |
|  ReservationId             |  r-2497ec97                |
+----------------------------+----------------------------+
||                       Instances                       ||
|+------------------------+------------------------------+|
||  AmiLaunchIndex        |  0                           ||
||  Architecture          |  x86_64                      ||
||  ClientToken           |                              ||
||  EbsOptimized          |  False                       ||
||  Hypervisor            |  xen                         ||
||  ImageId               |  ami-1c221e76                ||
||  InstanceId            |  i-bca3812f                  ||
||  InstanceType          |  t2.micro                    ||
||  KeyName               |  virginia_key                ||
||  LaunchTime            |  2016-11-19T06:41:57.000Z    ||
||  PrivateDnsName        |  ip-10-0-11-10.ec2.internal  ||
||  PrivateIpAddress      |  10.0.11.10                  ||
||  PublicDnsName         |                              ||
||  RootDeviceName        |  /dev/sda1                   ||
||  RootDeviceType        |  ebs                         ||
||  SourceDestCheck       |  True                        ||
||  StateTransitionReason |                              ||
||  SubnetId              |  subnet-f528a2ad             ||
||  VirtualizationType    |  hvm                         ||
||  VpcId                 |  vpc-d480c3b1                ||
|+------------------------+------------------------------+|
|||                     Monitoring                      |||
||+----------------------+------------------------------+||
|||  State               |  disabled                    |||
||+----------------------+------------------------------+||
|||                  NetworkInterfaces                  |||
||+---------------------+-------------------------------+||
|||  Description        |                               |||
|||  MacAddress         |  0e:99:1e:0e:11:6a            |||
|||  NetworkInterfaceId |  eni-f27d1233                 |||
|||  OwnerId            |  544509011205                 |||
|||  PrivateDnsName     |  ip-10-0-11-10.ec2.internal   |||
|||  PrivateIpAddress   |  10.0.11.10                   |||
|||  SourceDestCheck    |  True                         |||
|||  Status             |  in-use                       |||
|||  SubnetId           |  subnet-f528a2ad              |||
|||  VpcId              |  vpc-d480c3b1                 |||
||+---------------------+-------------------------------+||
||||                    Attachment                     ||||
|||+----------------------+----------------------------+|||
||||  AttachTime          |  2016-11-19T06:41:57.000Z  ||||
||||  AttachmentId        |  eni-attach-ab648822       ||||
||||  DeleteOnTermination |  True                      ||||
||||  DeviceIndex         |  0                         ||||
||||  Status              |  attaching                 ||||
|||+----------------------+----------------------------+|||
||||                      Groups                       ||||
|||+-----------------------+---------------------------+|||
||||  GroupId              |  sg-bbf176df              ||||
||||  GroupName            |  default                  ||||
|||+-----------------------+---------------------------+|||
||||                PrivateIpAddresses                 ||||
|||+-------------------+-------------------------------+|||
||||  Primary          |  True                         ||||
||||  PrivateDnsName   |  ip-10-0-11-10.ec2.internal   ||||
||||  PrivateIpAddress |  10.0.11.10                   ||||
|||+-------------------+-------------------------------+|||
|||                      Placement                      |||
||+------------------------------+----------------------+||
|||  AvailabilityZone            |  us-east-1b          |||
|||  GroupName                   |                      |||
|||  Tenancy                     |  default             |||
||+------------------------------+----------------------+||
|||                   SecurityGroups                    |||
||+------------------------+----------------------------+||
|||  GroupId               |  sg-bbf176df               |||
|||  GroupName             |  default                   |||
||+------------------------+----------------------------+||
|||                        State                        |||
||+---------------------+-------------------------------+||
|||  Code               |  0                            |||
|||  Name               |  pending                      |||
||+---------------------+-------------------------------+||
|||                     StateReason                     |||
||+-------------------------+---------------------------+||
|||  Code                   |  pending                  |||
|||  Message                |  pending                  |||
||+-------------------------+---------------------------+||


2016年11月18日金曜日

aws-cli で Volume の Delete On Termination を操作する


EC2インスタンスを作成するときに、Delete On Termination を有効したい場合は、以下のように実行する。
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t2.micro --key-name default --security-groups test_group --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"DeleteOnTermination\": \"true\"}}]"


EC2インスタンスを作成した後で有効したい場合は、以下のように実行する。
aws ec2 modify-instance-attribute --instance-id i-12345678 --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"DeleteOnTermination\": true}}]"


2016年11月14日月曜日

root が使用できないので ssh と sudo でファイル転送したい


root が使用できない環境で、root がオーナのファイルを転送したい場合、よくやるのが以下の手順です。

  1. 一時的に /tmp などにファイルを scp する
  2. ssh で sudo cp する
  3. 上記1で/tmp に置いたファイルを削除する

これは、面倒です。。

以下のように ssh に標準入力経由でローカルファイルのデータを渡し、
sudo で root 権限を使用して dd コマンドで処理すると、
ローカルのファイルを1回でリモートサーバにファイル転送できます。

ssh user@server "sudo dd of=/etc/hosts" < my_hosts

リモートサーバのファイルをローカルに転送したい場合は、以下のとおり。

ssh user@server "sudo dd if=/etc/hosts" > hosts.backup

dd のかわりに、tar コマンドを使用して、リモートサーバのファイルを、直接ローカルにアーカイブすることもできます。

ssh user@server "sudo tar cvfz - /etc" > etc.tgz




2016年11月11日金曜日

ssh で sudo したいが、sudoers は変更できない場合は


"-t" を複数つけて強制的に仮想端末を割り当ててssh を実行します

ssh -t -t user@server "sudo cat /etc/hosts"

2016年11月10日木曜日

while ループの Here Document で ssh を使うと1回しかループしない


以下のようにして、複数のサーバにコマンドを実行しようとすると1回しかループしない。

while read ip
do
    ssh user@${ip} 'cat /etc/hosts'
done<<END
10.1.0.11
10.1.0.12
10.1.0.13
END

これは、ssh を実行することで 標準入力が切り替わることが影響しているらしい。

以下のように -n をつけると、3回ループして、期待どおりに動いた。

while read ip
do
    ssh -n user@${ip} 'cat /etc/hosts'
done<<END
10.1.0.11
10.1.0.12
10.1.0.13
END