公共IP未从Terraform代码中提取到实例中

huangapple go评论83阅读模式
英文:

Public IP not coming in instances from Terraform code

问题

要求上述代码如下所示:

公司希望体系结构具有以下服务:

  1. 创建具有VPC、2个子网和每个子网中的1个实例的模板。
  2. 将安全组、互联网网关和网络接口附加到实例。

以下是 Terraform 代码,未在实例中获取公共 IP:

 # 块设置
    terraform {
      required_providers {
        aws = {
          source = "hashicorp/aws"
        }
      }
    }

    # 提供者
    provider "aws" {
      profile = "default"
      region = "us-east-2"
    }

    # VPC
    resource "aws_vpc" "TF_VPC" {
      cidr_block = "170.31.0.0/16"
    tags = {
    Name = "TF_VPC"
      }
    }

    # 子网
    resource "aws_subnet" "TF_Subnet1" {
      vpc_id     = aws_vpc.TF_VPC.id
      cidr_block = "170.31.1.0/24"
      availability_zone = "us-east-2a"
    tags = {
    Name = "TF_Subnet1"
      }
    }

    resource "aws_subnet" "TF_Subnet2" {
      vpc_id     = aws_vpc.TF_VPC.id
      cidr_block = "170.31.2.0/24"
      availability_zone = "us-east-2b"
    tags = {
    Name = "TF_Subnet2"
      }
    }

    # 安全组
    resource "aws_security_group" "TF_SG" {
      vpc_id = aws_vpc.TF_VPC.id

      # 允许来自任何地方的 SSH 访问
      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }

      # 允许来自任何地方的 HTTP 访问
      ingress {
        from_port   = 80
        to_port     = 80
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }

      egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
     }
    tags = {
    Name = "TF_SG"
      }
    }

    # 互联网网关
    resource "aws_internet_gateway" "TF_IGW" {
      vpc_id = aws_vpc.TF_VPC.id
    tags = {
    Name = "TF_IGW"
      }
    }

    # 路由表
    resource "aws_route_table" "TF_RT" {
      vpc_id = aws_vpc.TF_VPC.id

        route {
          cidr_block = "0.0.0.0/0"
          gateway_id = aws_internet_gateway.TF_IGW.id
    }

    tags = {
    Name = "TF_RT"
      }
    }

    # 路由表关联
    resource "aws_route_table_association" "TF_RTA1" {
      subnet_id      = aws_subnet.TF_Subnet1.id
      route_table_id = aws_route_table.TF_RT.id
} 

    resource "aws_route_table_association" "TF_RTA2" {
      subnet_id      = aws_subnet.TF_Subnet2.id
      route_table_id = aws_route_table.TF_RT.id
}

    # 网络接口
    resource "aws_network_interface" "TF_NI1" {
      subnet_id   = aws_subnet.TF_Subnet1.id
      private_ips = ["170.31.1.5"]
      security_groups = [aws_security_group.TF_SG.id]
    tags = {
    Name = "TF_NI1"
      }
    }

    resource "aws_network_interface" "TF_NI2" {
      subnet_id   = aws_subnet.TF_Subnet2.id
      private_ips = ["170.31.2.5"]
      security_groups = [aws_security_group.TF_SG.id]
    tags = {
    Name = "TF_NI2"
      }
    }

    # EC2 实例
    resource "aws_instance" "TF_instance1" {
      ami           = "ami-024e6efaf93d85776"
      instance_type = "t2.micro"
      key_name      = "assign.ohio"
        network_interface {
        network_interface_id = aws_network_interface.TF_NI1.id
        device_index         = 0
      }
    tags = {
    Name = "TF_instance1"
      }
    }

    resource "aws_instance" "TF_instance2" {
      ami           = "ami-024e6efaf93d85776"
      instance_type = "t2.micro"
      key_name      = "assign.ohio"
        network_interface {
        network_interface_id = aws_network_interface.TF_NI2.id
        device_index         = 0

      }
    tags = {
    Name = "TF_instance2"
      }
    }

所有的东西都被创建,但 EC2 实例没有获取公共 IP。如果我在实例下使用以下代码:

resource "aws_instance" "TF_instance1" {
  ami                         = "ami-024e6efaf93d85776"
  instance_type               = "t2.micro"
  associate_public_ip_address = true ## <-- 添加此行
  key_name                    = "assign.ohio"
  network_interface {
    network_interface_id = aws_network_interface.TF_NI1.id
    device_index         = 0

  }
  tags = {
    Name = "TF_instance1"
  }
}

会出现以下错误:

错误:冲突的配置参数
│   with aws_instance.TF_instance1,
│   on main.tf line 127, in resource "aws_instance" "TF_instance1":
│  127:     resource "aws_instance" "TF_instance1" {
│ "network_interface": conflicts with associate_public_ip_address
英文:

Requirement for above code is as below:
The company wants the Architecture to have the following services:

  1. Create a template with a VPC, 2 subnets and 1 instance in each subnet
  2. Attach Security groups, internet gateway and network interface to the instance

**Public IP not coming in instances from below Terraform code:
**

 # Block Settings
terraform {
required_providers {
aws = {
source = &quot;hashicorp/aws&quot;
}
}
}
# Provider
provider &quot;aws&quot; {
profile = &quot;default&quot;
region = &quot;us-east-2&quot;
}
# VPC
resource &quot;aws_vpc&quot; &quot;TF_VPC&quot; {
cidr_block = &quot;170.31.0.0/16&quot;
tags = {
Name = &quot;TF_VPC&quot;
}
}
# Subnets
resource &quot;aws_subnet&quot; &quot;TF_Subnet1&quot; {
vpc_id     = aws_vpc.TF_VPC.id
cidr_block = &quot;170.31.1.0/24&quot;
availability_zone = &quot;us-east-2a&quot;
tags = {
Name = &quot;TF_Subnet1&quot;
}
}
resource &quot;aws_subnet&quot; &quot;TF_Subnet2&quot; {
vpc_id     = aws_vpc.TF_VPC.id
cidr_block = &quot;170.31.2.0/24&quot;
availability_zone = &quot;us-east-2b&quot;
tags = {
Name = &quot;TF_Subnet2&quot;
}
}
# Security Group
resource &quot;aws_security_group&quot; &quot;TF_SG&quot; {
vpc_id = aws_vpc.TF_VPC.id
# Allow SSH access from anywhere
ingress {
from_port   = 22
to_port     = 22
protocol    = &quot;tcp&quot;
cidr_blocks = [&quot;0.0.0.0/0&quot;]
}
# Allow HTTP access from anywhere
ingress {
from_port   = 80
to_port     = 80
protocol    = &quot;tcp&quot;
cidr_blocks = [&quot;0.0.0.0/0&quot;]
}
egress {
from_port   = 0
to_port     = 0
protocol    = &quot;-1&quot;
cidr_blocks = [&quot;0.0.0.0/0&quot;]
}
tags = {
Name = &quot;TF_SG&quot;
}
}
# Internet Gateway
resource &quot;aws_internet_gateway&quot; &quot;TF_IGW&quot; {
vpc_id = aws_vpc.TF_VPC.id
tags = {
Name = &quot;TF_IGW&quot;
}
}
# Route Table
resource &quot;aws_route_table&quot; &quot;TF_RT&quot; {
vpc_id = aws_vpc.TF_VPC.id
route {
cidr_block = &quot;0.0.0.0/0&quot;
gateway_id = aws_internet_gateway.TF_IGW.id
}
tags = {
Name = &quot;TF_RT&quot;
}
}
# Route Table Association
resource &quot;aws_route_table_association&quot; &quot;TF_RTA1&quot; {
subnet_id      = aws_subnet.TF_Subnet1.id
route_table_id = aws_route_table.TF_RT.id
} 
resource &quot;aws_route_table_association&quot; &quot;TF_RTA2&quot; {
subnet_id      = aws_subnet.TF_Subnet2.id
route_table_id = aws_route_table.TF_RT.id
}
# Network Interface
resource &quot;aws_network_interface&quot; &quot;TF_NI1&quot; {
subnet_id   = aws_subnet.TF_Subnet1.id
private_ips = [&quot;170.31.1.5&quot;]
security_groups = [aws_security_group.TF_SG.id]
tags = {
Name = &quot;TF_NI1&quot;
}
}
resource &quot;aws_network_interface&quot; &quot;TF_NI2&quot; {
subnet_id   = aws_subnet.TF_Subnet2.id
private_ips = [&quot;170.31.2.5&quot;]
security_groups = [aws_security_group.TF_SG.id]
tags = {
Name = &quot;TF_NI2&quot;
}
}
# EC2 Instances
resource &quot;aws_instance&quot; &quot;TF_instance1&quot; {
ami           = &quot;ami-024e6efaf93d85776&quot;
instance_type = &quot;t2.micro&quot;
key_name      = &quot;assign.ohio&quot;
network_interface {
network_interface_id = aws_network_interface.TF_NI1.id
device_index         = 0
}
tags = {
Name = &quot;TF_instance1&quot;
}
}
resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
ami           = &quot;ami-024e6efaf93d85776&quot;
instance_type = &quot;t2.micro&quot;
key_name      = &quot;assign.ohio&quot;
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index         = 0
}
tags = {
Name = &quot;TF_instance2&quot;
}
}

All the things are getting created but EC2 instances not getting public IP.
If I use below code under instance:

  ami                         = &quot;ami-024e6efaf93d85776&quot;
instance_type               = &quot;t2.micro&quot;
associate_public_ip_address = true ## &lt;-- Add this
key_name                    = &quot;assign.ohio&quot;
network_interface {
network_interface_id = aws_network_interface.TF_NI1.id
device_index         = 0
}
tags = {
Name = &quot;TF_instance1&quot;
}
}

Getting error as below:

│ 
│   with aws_instance.TF_instance1,
│   on main.tf line 127, in resource &quot;aws_instance&quot; &quot;TF_instance1&quot;:
│  127:     resource &quot;aws_instance&quot; &quot;TF_instance1&quot; {
│ 
│ &quot;network_interface&quot;: conflicts with associate_public_ip_address

答案1

得分: 1

你需要在你的EC2实例上关联公共IP,示例如下:

resource "aws_instance" "TF_instance2" {
  ami                         = "ami-024e6efaf93d85776"
  instance_type               = "t2.micro"
  associate_public_ip_address = true ## <-- 添加这一行
  key_name                    = "assign.ohio"
  network_interface {
    network_interface_id = aws_network_interface.TF_NI2.id
    device_index         = 0
  }
  tags = {
    Name = "TF_instance2"
  }
}

查看文档以供参考。

英文:

You need to associate public IP on your EC2 instance, example below:

resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
ami                         = &quot;ami-024e6efaf93d85776&quot;
instance_type               = &quot;t2.micro&quot;
associate_public_ip_address = true ## &lt;-- Add this
key_name                    = &quot;assign.ohio&quot;
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index         = 0
}
tags = {
Name = &quot;TF_instance2&quot;
}
}

Check the docs for reference.

答案2

得分: 0

以下是翻译好的部分:

  • 如果您能ping一下您收到的错误,那将是很好的。
  • 或者,您可以创建弹性 IP 并附加到实例上。
  • 尝试以下代码:
resource "aws_instance" "TF_instance2" {
  ami                         = "ami-024e6efaf93d85776"
  instance_type               = "t2.micro"
  associate_public_ip_address = true ## <-- 添加这一行
  key_name                    = "assign.ohio"
  network_interface {
    network_interface_id = aws_network_interface.TF_NI2.id
    device_index         = 1
  }
  tags = {
    Name = "TF_instance2"
  }
}

请注意,我已将HTML实体编码(如")还原为原始文本。

英文:

It would be good if you ping the error you are receiving.
Alternatively, you can create Elastic IP and attach to the instance.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip

Try the below code:

resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
ami                         = &quot;ami-024e6efaf93d85776&quot;
instance_type               = &quot;t2.micro&quot;
associate_public_ip_address = true ## &lt;-- Add this
key_name                    = &quot;assign.ohio&quot;
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index         = 1
}
tags = {
Name = &quot;TF_instance2&quot;
}
}

huangapple
  • 本文由 发表于 2023年7月10日 18:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定