share facebook facebook2 twitter menu hatena pocket slack

2017.01.23 MON

CLIでEC2をローンチする際にエフェメラルディスクをつけない方法

若松 剛志

WRITTEN BY 若松 剛志

Webコンソール以外でEC2をローンチする際にエフェメラルディスク(インスタンスストア)が付いてしまう仕様があるようです。
少なくともCLIとTerraformではこの現象が起きます。

この現象の解消方法は現在のところBlockDeviceMappingのエフェメラルディスクに片っ端からNoDeviceを指定する他ありません。

以下にCLIとTerraformでの例を記載します。

CLI

BlockDeviceMapping用のjsonファイルを以下のように記述します。

block_device_mappings.json

[
    {
        "DeviceName": "/dev/sda1",
        "Ebs": {
            "VolumeSize": 50,
            "DeleteOnTermination": true,
            "VolumeType": "gp2"
        }
    },
    {"DeviceName": "xvdca", "NoDevice": ""},
    {"DeviceName": "xvdcb", "NoDevice": ""},
    {"DeviceName": "xvdcc", "NoDevice": ""},
    {"DeviceName": "xvdcd", "NoDevice": ""},
    {"DeviceName": "xvdce", "NoDevice": ""},
    {"DeviceName": "xvdcf", "NoDevice": ""},
    {"DeviceName": "xvdcg", "NoDevice": ""},
    {"DeviceName": "xvdch", "NoDevice": ""},
    {"DeviceName": "xvdci", "NoDevice": ""},
    {"DeviceName": "xvdcj", "NoDevice": ""},
    {"DeviceName": "xvdck", "NoDevice": ""},
    {"DeviceName": "xvdcl", "NoDevice": ""},
    {"DeviceName": "xvdcm", "NoDevice": ""},
    {"DeviceName": "xvdcn", "NoDevice": ""},
    {"DeviceName": "xvdco", "NoDevice": ""},
    {"DeviceName": "xvdcp", "NoDevice": ""},
    {"DeviceName": "xvdcq", "NoDevice": ""},
    {"DeviceName": "xvdcr", "NoDevice": ""},
    {"DeviceName": "xvdcs", "NoDevice": ""},
    {"DeviceName": "xvdct", "NoDevice": ""},
    {"DeviceName": "xvdcu", "NoDevice": ""},
    {"DeviceName": "xvdcv", "NoDevice": ""},
    {"DeviceName": "xvdcw", "NoDevice": ""},
    {"DeviceName": "xvdcx", "NoDevice": ""},
    {"DeviceName": "xvdcy", "NoDevice": ""},
    {"DeviceName": "xvdcz", "NoDevice": ""}
]

そして、以下のようにCLIを実行します。

ec2_run-instances.sh

aws ec2 run-instances \
    --image-id ami-12345678 \
    --key-name example \
    --instance-type c3.large \
    --subnet-id subnet-12345678 \
    --iam-instance-profile Arn=arn:aws:iam::123456789123:role/example-role \
    --security-group-ids sg-12345678 \
    --block-device-mappings file://block_device_mappings.json \
    --desable-api-termination

Terraform

TerraformもVer0.8からNoDeviceをサポートしています。
tfファイルには以下のように記述します。

aws_instance.tf

resource "aws_instance" "example-instance" {
    ami = "ami-12345678"
    instance_type = "c3.large"
    key_name = "example"
    subnet_id = "${aws_subnet.example.id}"
    security_groups = ["${aws_security_group.example.id}"]
    disable_api_termination = true
    tags {
        Name = "example-instance"
    }
    iam_instance_profile = "example-role"
    root_block_device {
        volume_type = "gp2"
        volume_size = 50
    }
    ephemeral_block_device { device_name = "xvdca" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcb" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcc" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcd" no_device = "true" }
    ephemeral_block_device { device_name = "xvdce" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcf" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcg" no_device = "true" }
    ephemeral_block_device { device_name = "xvdch" no_device = "true" }
    ephemeral_block_device { device_name = "xvdci" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcj" no_device = "true" }
    ephemeral_block_device { device_name = "xvdck" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcl" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcm" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcn" no_device = "true" }
    ephemeral_block_device { device_name = "xvdco" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcp" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcq" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcr" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcs" no_device = "true" }
    ephemeral_block_device { device_name = "xvdct" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcu" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcv" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcw" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcx" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcy" no_device = "true" }
    ephemeral_block_device { device_name = "xvdcz" no_device = "true" }
}

まとめ

書式としてはあまりスマートとは言えませんが、現状はこうするしかないようです。
SDKでは検証していませんが、おそらく同じかと思います。
もっとよい書き方があったら教えてください。

元記事はこちら

CLIでEC2をローンチする際にエフェメラルディスクをつけない方法

若松 剛志

若松 剛志

AWS環境の構築、運用を担当。インフラを中心に雑食な感じで生きてます。JAWS-UGでも活動中。