英文:
Public IP not coming in instances from Terraform code
问题
要求上述代码如下所示:
公司希望体系结构具有以下服务:
- 创建具有VPC、2个子网和每个子网中的1个实例的模板。
 - 将安全组、互联网网关和网络接口附加到实例。
 
以下是 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:
- Create a template with a VPC, 2 subnets and 1 instance in each subnet
 - 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 = "hashicorp/aws"
}
}
}
# Provider
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"
}
}
# Subnets
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"
}
}
# Security Group
resource "aws_security_group" "TF_SG" {
vpc_id = aws_vpc.TF_VPC.id
# Allow SSH access from anywhere
ingress {
from_port   = 22
to_port     = 22
protocol    = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow HTTP access from anywhere
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"
}
}
# Internet Gateway
resource "aws_internet_gateway" "TF_IGW" {
vpc_id = aws_vpc.TF_VPC.id
tags = {
Name = "TF_IGW"
}
}
# Route Table
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"
}
}
# Route Table Association
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
}
# Network Interface
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 Instances
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"
}
}
All the things are getting created but EC2 instances not getting public IP.
If I use below code under instance:
  ami                         = "ami-024e6efaf93d85776"
instance_type               = "t2.micro"
associate_public_ip_address = true ## <-- Add this
key_name                    = "assign.ohio"
network_interface {
network_interface_id = aws_network_interface.TF_NI1.id
device_index         = 0
}
tags = {
Name = "TF_instance1"
}
}
Getting error as below:
│ 
│   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
答案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 "aws_instance" "TF_instance2" {
ami                         = "ami-024e6efaf93d85776"
instance_type               = "t2.micro"
associate_public_ip_address = true ## <-- Add this
key_name                    = "assign.ohio"
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index         = 0
}
tags = {
Name = "TF_instance2"
}
}
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 "aws_instance" "TF_instance2" {
ami                         = "ami-024e6efaf93d85776"
instance_type               = "t2.micro"
associate_public_ip_address = true ## <-- Add this
key_name                    = "assign.ohio"
network_interface {
network_interface_id = aws_network_interface.TF_NI2.id
device_index         = 1
}
tags = {
Name = "TF_instance2"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论