開発メンバーの分だけEC2インスタンスを起動する


variable developer の分だけインスタンスの起動とhostnameなどを設定する


variable.tf

```
variable "developer" {
  default = [
    "sonic883b",
   "developer02",
   "developer03",
   "developer04",
   "developer05",
  ]
}

variable "ec2_options" {
  type = "map"

  default = {
    default.ami  = "ami-8e8847f1"
    default.instance_type = "t3.micro"
    default.subnet_id = "subnet-16d1764e"
    default.hostname = "dev"

    bastion.hostname = "bastion"
    bastion.name = "bastion"
  }
  
}
```


ec2.tf

```
resource "aws_instance" "dev-developer_name-001" {
    count                       = "${ length( var.developer ) }"
    ami                         = "${lookup(var.ec2_options, "default.ami")}"
    availability_zone           = "ap-northeast-1c"
    ebs_optimized               = true
    instance_type               = "${lookup(var.ec2_options, "default.instance_type")}"
    monitoring                  = false
    key_name                     = "${aws_key_pair.sonic883b.key_name}"
    subnet_id                   = "${lookup(var.ec2_options, "default.subnet_id")}"
    #vpc_security_group_ids      = ["sg-0daff01d3e386427e", "sg-0a803e810cfb05f42"]
    associate_public_ip_address = true
    source_dest_check           = true

    root_block_device {
        volume_type           = "gp2"
        volume_size           = 32
        delete_on_termination = true
    }

    tags {
        "Hostname" = "${lookup(var.ec2_options, "default.hostname")}-${element(var.developer,count.index)}-001.domain.jp"
        "Components" = "test-dev"
        "Service" = "sonic883b"
        "SshKey" = "${aws_key_pair.sonic883b.key_name}"
        "Name" = "${lookup(var.ec2_options, "bastion.name")}-${element(var.developer,count.index)}-001"
        "Stage" = "develop"
    }
}
```

terraform plan

```
$ terraform  plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_key_pair.sonic883b: Refreshing state... (ID: sonic883b)

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_instance.dev-developer_name-001[0]
      id:                                        <computed>
      ami:                                       "ami-8e8847f1"
      arn:                                       <computed>
      associate_public_ip_address:               "true"
      availability_zone:                         "ap-northeast-1c"
      cpu_core_count:                            <computed>
      cpu_threads_per_core:                      <computed>
      ebs_block_device.#:                        <computed>
      ebs_optimized:                             "true"
      ephemeral_block_device.#:                  <computed>
      get_password_data:                         "false"
      host_id:                                   <computed>
      instance_state:                            <computed>
      instance_type:                             "t3.micro"
      ipv6_address_count:                        <computed>
      ipv6_addresses.#:                          <computed>
      key_name:                                  "sonic883b"
      monitoring:                                "false"
      network_interface.#:                       <computed>
      network_interface_id:                      <computed>
      password_data:                             <computed>
      placement_group:                           <computed>
      primary_network_interface_id:              <computed>
      private_dns:                               <computed>
      private_ip:                                <computed>
      public_dns:                                <computed>
      public_ip:                                 <computed>
      root_block_device.#:                       "1"
      root_block_device.0.delete_on_termination: "true"
      root_block_device.0.volume_id:             <computed>
      root_block_device.0.volume_size:           "32"
      root_block_device.0.volume_type:           "gp2"
      security_groups.#:                         <computed>
      source_dest_check:                         "true"
      subnet_id:                                 "subnet-16d1764e"
      tags.%:                                    "8"
      tags.Components:                           "test-dev"
      tags.Hostname:                             "dev-sonic883b-001.domain.jp"
      tags.Name:                                 "bastion-sonic883b-001"
      tags.Service:                              "sonic883b"
      tags.SshKey:                               "sonic883b"
      tags.SshUser:                              "centos"
      tags.Stage:                                "develop"
      tags.isSchedule:                           "true"
      tenancy:                                   <computed>
      volume_tags.%:                             <computed>
      vpc_security_group_ids.#:                  <computed>

  + aws_instance.dev-developer_name-001[1]
      id:                                        <computed>
      ami:                                       "ami-8e8847f1"
      arn:                                       <computed>
      associate_public_ip_address:               "true"
      availability_zone:                         "ap-northeast-1c"
      cpu_core_count:                            <computed>
      cpu_threads_per_core:                      <computed>
      ebs_block_device.#:                        <computed>
      ebs_optimized:                             "true"
      ephemeral_block_device.#:                  <computed>
      get_password_data:                         "false"
      host_id:                                   <computed>
      instance_state:                            <computed>
      instance_type:                             "t3.micro"
      ipv6_address_count:                        <computed>
      ipv6_addresses.#:                          <computed>
      key_name:                                  "sonic883b"
      monitoring:                                "false"
      network_interface.#:                       <computed>
      network_interface_id:                      <computed>
      password_data:                             <computed>
      placement_group:                           <computed>
      primary_network_interface_id:              <computed>
      private_dns:                               <computed>
      private_ip:                                <computed>
      public_dns:                                <computed>
      public_ip:                                 <computed>
      root_block_device.#:                       "1"
      root_block_device.0.delete_on_termination: "true"
      root_block_device.0.volume_id:             <computed>
      root_block_device.0.volume_size:           "32"
      root_block_device.0.volume_type:           "gp2"
      security_groups.#:                         <computed>
      source_dest_check:                         "true"
      subnet_id:                                 "subnet-16d1764e"
      tags.%:                                    "8"
      tags.Components:                           "test-dev"
      tags.Hostname:                             "dev-developer02-001.domain.jp"
      tags.Name:                                 "bastion-developer02-001"
      tags.Service:                              "sonic883b"
      tags.SshKey:                               "sonic883b"
      tags.SshUser:                              "centos"
      tags.Stage:                                "develop"
      tags.isSchedule:                           "true"
      tenancy:                                   <computed>
      volume_tags.%:                             <computed>
      vpc_security_group_ids.#:                  <computed>

  + aws_instance.dev-developer_name-001[2]
      id:                                        <computed>
      ami:                                       "ami-8e8847f1"
      arn:                                       <computed>
      associate_public_ip_address:               "true"
      availability_zone:                         "ap-northeast-1c"
      cpu_core_count:                            <computed>
      cpu_threads_per_core:                      <computed>
      ebs_block_device.#:                        <computed>
      ebs_optimized:                             "true"
      ephemeral_block_device.#:                  <computed>
      get_password_data:                         "false"
      host_id:                                   <computed>
      instance_state:                            <computed>
      instance_type:                             "t3.micro"
      ipv6_address_count:                        <computed>
      ipv6_addresses.#:                          <computed>
      key_name:                                  "sonic883b"
      monitoring:                                "false"
      network_interface.#:                       <computed>
      network_interface_id:                      <computed>
      password_data:                             <computed>
      placement_group:                           <computed>
      primary_network_interface_id:              <computed>
      private_dns:                               <computed>
      private_ip:                                <computed>
      public_dns:                                <computed>
      public_ip:                                 <computed>
      root_block_device.#:                       "1"
      root_block_device.0.delete_on_termination: "true"
      root_block_device.0.volume_id:             <computed>
      root_block_device.0.volume_size:           "32"
      root_block_device.0.volume_type:           "gp2"
      security_groups.#:                         <computed>
      source_dest_check:                         "true"
      subnet_id:                                 "subnet-16d1764e"
      tags.%:                                    "8"
      tags.Components:                           "test-dev"
      tags.Hostname:                             "dev-developer03-001.domain.jp"
      tags.Name:                                 "bastion-developer03-001"
      tags.Service:                              "sonic883b"
      tags.SshKey:                               "sonic883b"
      tags.SshUser:                              "centos"
      tags.Stage:                                "develop"
      tags.isSchedule:                           "true"
      tenancy:                                   <computed>
      volume_tags.%:                             <computed>
      vpc_security_group_ids.#:                  <computed>

  + aws_instance.dev-developer_name-001[3]
      id:                                        <computed>
      ami:                                       "ami-8e8847f1"
      arn:                                       <computed>
      associate_public_ip_address:               "true"
      availability_zone:                         "ap-northeast-1c"
      cpu_core_count:                            <computed>
      cpu_threads_per_core:                      <computed>
      ebs_block_device.#:                        <computed>
      ebs_optimized:                             "true"
      ephemeral_block_device.#:                  <computed>
      get_password_data:                         "false"
      host_id:                                   <computed>
      instance_state:                            <computed>
      instance_type:                             "t3.micro"
      ipv6_address_count:                        <computed>
      ipv6_addresses.#:                          <computed>
      key_name:                                  "sonic883b"
      monitoring:                                "false"
      network_interface.#:                       <computed>
      network_interface_id:                      <computed>
      password_data:                             <computed>
      placement_group:                           <computed>
      primary_network_interface_id:              <computed>
      private_dns:                               <computed>
      private_ip:                                <computed>
      public_dns:                                <computed>
      public_ip:                                 <computed>
      root_block_device.#:                       "1"
      root_block_device.0.delete_on_termination: "true"
      root_block_device.0.volume_id:             <computed>
      root_block_device.0.volume_size:           "32"
      root_block_device.0.volume_type:           "gp2"
      security_groups.#:                         <computed>
      source_dest_check:                         "true"
      subnet_id:                                 "subnet-16d1764e"
      tags.%:                                    "8"
      tags.Components:                           "test-dev"
      tags.Hostname:                             "dev-developer04-001.domain.jp"
      tags.Name:                                 "bastion-developer04-001"
      tags.Service:                              "sonic883b"
      tags.SshKey:                               "sonic883b"
      tags.SshUser:                              "centos"
      tags.Stage:                                "develop"
      tags.isSchedule:                           "true"
      tenancy:                                   <computed>
      volume_tags.%:                             <computed>
      vpc_security_group_ids.#:                  <computed>

  + aws_instance.dev-developer_name-001[4]
      id:                                        <computed>
      ami:                                       "ami-8e8847f1"
      arn:                                       <computed>
      associate_public_ip_address:               "true"
      availability_zone:                         "ap-northeast-1c"
      cpu_core_count:                            <computed>
      cpu_threads_per_core:                      <computed>
      ebs_block_device.#:                        <computed>
      ebs_optimized:                             "true"
      ephemeral_block_device.#:                  <computed>
      get_password_data:                         "false"
      host_id:                                   <computed>
      instance_state:                            <computed>
      instance_type:                             "t3.micro"
      ipv6_address_count:                        <computed>
      ipv6_addresses.#:                          <computed>
      key_name:                                  "sonic883b"
      monitoring:                                "false"
      network_interface.#:                       <computed>
      network_interface_id:                      <computed>
      password_data:                             <computed>
      placement_group:                           <computed>
      primary_network_interface_id:              <computed>
      private_dns:                               <computed>
      private_ip:                                <computed>
      public_dns:                                <computed>
      public_ip:                                 <computed>
      root_block_device.#:                       "1"
      root_block_device.0.delete_on_termination: "true"
      root_block_device.0.volume_id:             <computed>
      root_block_device.0.volume_size:           "32"
      root_block_device.0.volume_type:           "gp2"
      security_groups.#:                         <computed>
      source_dest_check:                         "true"
      subnet_id:                                 "subnet-16d1764e"
      tags.%:                                    "8"
      tags.Components:                           "test-dev"
      tags.Hostname:                             "dev-developer05-001.domain.jp"
      tags.Name:                                 "bastion-developer05-001"
      tags.Service:                              "sonic883b"
      tags.SshKey:                               "sonic883b"
      tags.SshUser:                              "centos"
      tags.Stage:                                "develop"
      tags.isSchedule:                           "true"
      tenancy:                                   <computed>
      volume_tags.%:                             <computed>
      vpc_security_group_ids.#:                  <computed>


Plan: 5 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

```