0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

terraformのresource aws_ebs_volumeでattachしたいEC2 Instanceと同じAZに作成する方法

Last updated at Posted at 2015-12-12

俺です。

resource aws_instance で作成したEC2へAttachするEBSを作りたい場合、

EBSはEC2と同じAZに作る必要があります。

どうするのが楽かというと、 aws_instance リソースのプロパティを指定するとよいです。

相変わらずGHEの俺俺リポジトリから抜粋ですが、こんな感じになります。

  • EC2作成
resource "aws_instance" "prd-manage-1a" {
  ami = "${lookup(var.manage_ami, var.region)}"
  instance_type = "${lookup(var.manage_instance_type,"prd")}"
  key_name = "mykey-${var.region}"
  iam_instance_profile = "${lookup(var.manage_iam_instance_profile, "prd")}"
  source_dest_check = true
  ebs_optimized = "${lookup(var.manage_ebs_optimized, "prd")}"
  availability_zone = "${var.region}a"
  security_groups = ["${aws_security_group.prd-manage-1a.name}","${aws_security_group.common.name}"]
  root_block_device {
      volume_type = "gp2"
      volume_size = 64
  }
  count = "${lookup(var.manage_ec2_count, "prd")}"
  tags {
    Name  = "prd-manage${count.index + 1}-${var.az_1}"
    Role = "manage"
    Backup-Generation = "2"
    Environment = "prd"
    Service = "manage-demo"
    Attach_elb = "${lookup(var.manage_elb, "prd")}"
  }
}
  • EBS作成
resource "aws_ebs_volume" "prd-manage-1a" {
  availability_zone = "${aws_instance.prd-manage-1a.availability_zone}"
  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")}"
    Environment = "prd"
    Role = "manage"
  }
}

後は必要に応じてattachリソース書けばよいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?