俺です。

remote-execというprovisionerがあります。

このprovisionerはresource内で定義できるのでaws_ebs_volumeで作成したEBSをaws_volume_attachmentでターゲットのEC2へAttachした後に、

mkfsとmountでオラオラできます。

chefとかansibleとか面倒になって作りっぱなしで完結するならremote-exec最高。

以下定義例

  • ebs.tf
resource "aws_ebs_volume" "prd-manage_volume-1a" {
  availability_zone = "${var.region}a"
  type = "${lookup(var.manage_extend_ebs_volume_type,"prd")}"
  size = "${lookup(var.manage_extend_ebs_volume_size,"prd")}"
  encrypted = "${lookup(var.manage_extend_ebs_volume_encrypted,"prd")}"
  count = "${lookup(var.manage_extend_ebs_count, "prd")}"
  tags {
    Name = "${lookup(var.manage_tag_Name, "prd")}"
  }
}

resource "aws_volume_attachment" "prd-manage_volume-1a" {
  device_name = "/dev/xvdh"
  volume_id = "${aws_ebs_volume.prd-manage_volume-1a.id}"
  instance_id = "${aws_instance.prd-manage-1a.id}"
  provisioner "remote-exec" {
    connection {
      type = "ssh"
      user = "ec2-user"
      key_file = "~/.ssh/id_rsa"
      host = "${aws_instance.prd-manage-1a.public_ip}"
    }
    inline = [
      "sudo mkfs -t ext4 /dev/xvdh",
      "sudo mkdir /mnt/data",
      "sudo mount /dev/xvdh /mnt/data"
    ]
  }
}
  • console out

EBSが接続された後にmkfsが実行されています。

aws_volume_attachment.prd-manage_volume-1a (remote-exec):   Host: **.**.**.**
aws_volume_attachment.prd-manage_volume-1a (remote-exec):   User: ec2-user
aws_volume_attachment.prd-manage_volume-1a (remote-exec):   Password: false
aws_volume_attachment.prd-manage_volume-1a (remote-exec):   Private key: true
aws_volume_attachment.prd-manage_volume-1a (remote-exec):   SSH Agent: true
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Connected!
aws_volume_attachment.prd-manage_volume-1a (remote-exec): mke2fs 1.42.12 (29-Aug-2014)aws_volume_attachment.prd-manage_volume-1a (remote-exec): Creating filesystem with 262
14400 4k blocks and 6553600 inodes
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Filesystem UUID: 028ef97c-0d
79-4a8e-9ce6-c058c0f0d008
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Superblock backups stored on
 blocks:
aws_volume_attachment.prd-manage_volume-1a (remote-exec):       32768, 98304, 163840,
229376, 294912, 819200, 884736, 1605632, 2654208,
aws_volume_attachment.prd-manage_volume-1a (remote-exec):       4096000, 7962624, 11239424, 20480000, 23887872
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Allocating group tables: done
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Writing inode tables: done  aws_volume_attachment.prd-manage_volume-1a (remote-exec): Creating journal (32768 blocks): done
aws_volume_attachment.prd-manage_volume-1a (remote-exec): Writing superblocks and file
system accounting information:
aws_volume_attachment.prd-manage_volume-1a (remote-exec): done

aws_volume_attachment.prd-manage_volume-1a: Creation complete

元記事はこちら

aws_volume_attachmentリソース実行後にEBSにファイルシステムを作りたい