我想为分布在不同可用区的 10 个不同类型的 EC2 实例分配 10 个私有 IP(已选择),请帮助。
答案 0 :(得分:2)
根据您的简短问题和“terraform”标签,我认为您正在寻找文档中解释的内容 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance。
要使用您选择的某些 IP 设置 ec2 实例,您应该在将附加到它的 aws_network_interface 之前创建 (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface)。
仔细检查您想要的所有 IP 是否在同一个 aws_subnet(这是另一个首先要定义的 terraform 资源)
编辑
让我尝试添加一个示例(我目前无法对其进行测试,但应该很容易验证)
示例:
我将从文档中选择值,我们假设您已经选择的 3 个 IP 是 172.16.10.100、172.16.10.101、172.16.10.102。它们都包含在子网范围内。
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags = {
Name = "tf-example"
}
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "172.16.10.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "tf-example"
}
}
resource "aws_network_interface" "foo" {
subnet_id = aws_subnet.my_subnet.id
private_ips = ["172.16.10.100", "172.16.10.101", "172.16.10.102"]
tags = {
Name = "primary_network_interface"
}
}
resource "aws_instance" "foo" {
ami = "ami-005e54dee72cc1d00" # us-west-2
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.foo.id
device_index = 0
}
credit_specification {
cpu_credits = "unlimited"
}
}
resource "aws_instance" "foo_first" {
ami = "ami-005e54dee72cc1d00" # us-west-2
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.foo.id
device_index = 1
}
credit_specification {
cpu_credits = "unlimited"
}
}
resource "aws_instance" "foo_second" {
ami = "ami-005e54dee72cc1d00" # us-west-2
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.foo.id
device_index = 2
}
credit_specification {
cpu_credits = "unlimited"
}
}