Terraform未将公共ip分配给所有实例

Terraform未将公共ip分配给所有实例,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,我有以下地形代码: resource "aws_subnet" "public_subnet" { count = "3" vpc_id = "${aws_vpc.vpc.id}" cidr_block = "${cidrsubnet("${var.vpc_cidr}", 4, count.index)}" #count.index is 3 it creates 3 subnets availability_zone = "${elem

我有以下地形代码:

resource "aws_subnet" "public_subnet" {
  count  = "3"
  vpc_id = "${aws_vpc.vpc.id}"

  cidr_block              = "${cidrsubnet("${var.vpc_cidr}", 4, count.index)}" #count.index is 3 it creates 3 subnets
  availability_zone       = "${element(var.lst_azs, count.index)}"
  map_public_ip_on_launch = true
}
上面的代码创建了3个子网,3个不同的实例连接到这3个子网。 但只有第一个实例是获取公共ip。第二名和第三名未获得公共ip。 我在网上查过,尝试过很多东西,但都没能成功

虚拟机创建代码:

resource "aws_instance" "test" {
  instance_type        = "${var.micro}”

  network_interface {
    device_index         = 0
    network_interface_id = "${aws_network_interface.eth0.id}"
  }
}

resource "aws_network_interface" "eth0" {
  private_ips       = 10.0.0.1
  source_dest_check = "true"
  security_groups   = ["${aws_security_group.sg1.id}"]
  subnet_id = "${element(data.aws_subnet_ids.sub1.ids,0)}"
  lifecycle {
    ignore_changes = ["subnet_id"]
  }
}


resource "aws_subnet" "sub1" {
  count  = 3
  vpc_id = "${aws_vpc.test1.id}"
  cidr_block              = "${var.security_vpc_cidr_block}, count.index}"
  availability_zone       = "${element(var.lst_azs, count.index)}"
  map_public_ip_on_launch = true
}



data "aws_subnet_ids" "subnets1" {
  vpc_id = "${aws_vpc.test1.id}"
  depends_on = ["aws_subnet.subnets1"]
}

我在这里看到的主要问题是,在您提供的代码中创建的实例不超过1个,因此只创建了1个实例。它接收一个公共IP地址,因为它正在拾取创建并与公共IP关联的网络接口

Terraform 0.12.9中的工作示例:

Main.tf

variable "cidr_blocks" {
  type        = list(string)
  description = "List of CIDR Blocks to use in VPC"
}
variable "availability_zones" {
  type        = list(string)
  description = "List of AZs to use"
}
variable "create_count" {
  type        = number
  description = "How many resources to create"
  default     = 1
}

resource "aws_instance" "servers" {
  count         = var.create_count
  instance_type = "t3.micro"
  ami           = "ami-047bb4163c506cd98"

  network_interface {
    device_index         = 0
    network_interface_id = aws_network_interface.eth0[count.index].id
  }

  tags = {
    Name = join("-", ["Test", count.index])
  }
}

resource "aws_vpc" "testing" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "testing" {
  count                   = var.create_count
  vpc_id                  = aws_vpc.testing.id
  cidr_block              = var.cidr_blocks[count.index]
  availability_zone       = var.availability_zones[count.index]
  map_public_ip_on_launch = true
}

resource "aws_network_interface" "eth0" {
  count     = var.create_count
  subnet_id = aws_subnet.testing[count.index].id
}

output "instance_public_ips" {
  value = aws_instance.servers.*.public_ip
}
Main.auto.tfvars

cidr_blocks        = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
availability_zones = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
create_count       = 3
输出

Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments...

Outputs:

instance_public_ips = [
  "34.244.18.5",
  "52.213.153.230",
  "34.244.69.143",
]

我在这里看到的主要问题是,在您提供的代码中创建的实例不超过1个,因此只创建了1个实例。它接收一个公共IP地址,因为它正在拾取创建并与公共IP关联的网络接口

Terraform 0.12.9中的工作示例:

Main.tf

variable "cidr_blocks" {
  type        = list(string)
  description = "List of CIDR Blocks to use in VPC"
}
variable "availability_zones" {
  type        = list(string)
  description = "List of AZs to use"
}
variable "create_count" {
  type        = number
  description = "How many resources to create"
  default     = 1
}

resource "aws_instance" "servers" {
  count         = var.create_count
  instance_type = "t3.micro"
  ami           = "ami-047bb4163c506cd98"

  network_interface {
    device_index         = 0
    network_interface_id = aws_network_interface.eth0[count.index].id
  }

  tags = {
    Name = join("-", ["Test", count.index])
  }
}

resource "aws_vpc" "testing" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "testing" {
  count                   = var.create_count
  vpc_id                  = aws_vpc.testing.id
  cidr_block              = var.cidr_blocks[count.index]
  availability_zone       = var.availability_zones[count.index]
  map_public_ip_on_launch = true
}

resource "aws_network_interface" "eth0" {
  count     = var.create_count
  subnet_id = aws_subnet.testing[count.index].id
}

output "instance_public_ips" {
  value = aws_instance.servers.*.public_ip
}
Main.auto.tfvars

cidr_blocks        = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
availability_zones = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
create_count       = 3
输出

Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments...

Outputs:

instance_public_ips = [
  "34.244.18.5",
  "52.213.153.230",
  "34.244.69.143",
]

请显示创建虚拟机的代码位。它们还具有与公共IP相关的配置。能否将
map\u public\u IP\u on\u launch=“true”
更改为
map\u public\u IP\u on\u launch=true
,因为属性map\u public\u IP\u on\u launch需要布尔值而不是字符串。上述代码仅创建3个子网,且
map\u public\u IP\u on\u launch
设置为true,这意味着在这些子网中启动的任何实例都将获得公共IP。您能提供创建实例的代码以便我们进行调试吗further@cfelipe添加了虚拟机创建代码。@SajeerNoohukannu删除了true中的引号。没有任何区别。请显示创建虚拟机的代码位。它们还具有与公共IP相关的配置。能否将
map\u public\u IP\u on\u launch=“true”
更改为
map\u public\u IP\u on\u launch=true
,因为属性map\u public\u IP\u on\u launch需要布尔值而不是字符串。上述代码仅创建3个子网,且
map\u public\u IP\u on\u launch
设置为true,这意味着在这些子网中启动的任何实例都将获得公共IP。您能提供创建实例的代码以便我们进行调试吗further@cfelipe添加了虚拟机创建代码。@SajeerNoohukannu删除了true中的引号。没有任何区别。我发现了,问题是我没有创建3个eip。你仍然需要分配这些。我发现了,问题是我没有创建3个eip。您仍然需要分配这些任务。