我打算创建3 aws_vpc并使用Terraform进行对等

时间:2018-07-23 08:47:04

标签: amazon-web-services terraform amazon-vpc

我打算创建3个aws_vpc并使用terraform进行对等。我的问题是所有3个配置文件都位于不同的文件夹中。 WHenn我运行地形应用我得到这个错误 错误:资源'aws_vpc_peering_connection.transit2pco'配置:变量aws_vpc.Transit-VPC.id中引用的未知资源'aws_vpc.Transit-VPC'

#create a vpc in aws named PCO-VPC-Prod
resource "aws_vpc" "PCO-VPC-Prod" {
  cidr_block = "${var.pco_cidr_block}"
  enable_dns_support = true
  enable_dns_hostnames = true
  tags = {
    Name = "PCO-VPC-Prod"
  }
}

# Create a subnet to launch our instances into
resource "aws_subnet" "PCO-pub-sub-a" {
  availability_zone       = "us-west-1a"
  vpc_id                  = "${aws_vpc.PCO-VPC-Prod.id}"
  cidr_block              = "${var.pco-pub-sub-a}"
  map_public_ip_on_launch = true
  tags {
   Name = "PCO-pub-sub-a"
    Created = "terraform"
  }
}

# Create a subnet to launch our instances into
resource "aws_subnet" "PCO-pub-sub-b" {
  availability_zone       = "us-west-1b"
  vpc_id                  = "${aws_vpc.PCO-VPC-Prod.id}"
  cidr_block              = "${var.pco-pub-sub-b}"
  map_public_ip_on_launch = true
  tags {
    Name = "PCO-pub-sub-a"
    Created = "terraform"
  }
}

# Create a subnet to launch our instances into
resource "aws_subnet" "PCO-priv-sub-a" {
  availability_zone       = "us-west-1a"
  vpc_id                  = "${aws_vpc.PCO-VPC-Prod.id}"
  cidr_block              = "${var.pco-priv-sub-a}"
  map_public_ip_on_launch = false
  tags {
    Name = "PCO-priv-sub-a"
    Created = "terraform"
  }
}

# Create a subnet to launch our instances into
resource "aws_subnet" "PCO-priv-sub-b" {
  availability_zone       = "us-west-1b"
  vpc_id                  = "${aws_vpc.PCO-VPC-Prod.id}"
  cidr_block              = "${var.pco-priv-sub-b}"
  map_public_ip_on_launch = false
  tags {
   Name = "PCO-priv-sub-a"
    Created = "terraform"
  }
}

#create the public route table
resource "aws_route_table" "PCO-rt-pub" {
    vpc_id = "${aws_vpc.PCO-VPC-Prod.id}"

    tags {
        Name = "Pco Public route table"
    }
}

#create the private route table
resource "aws_route_table" "PCO-rt-priv" {
    vpc_id = "${aws_vpc.PCO-VPC-Prod.id}"

    tags {
        Name = "Pco Private route table"
    }
}


# Associate subnet PCO-pub-sub-a to public route table
resource "aws_route_table_association" "PCO-pub-sub-a-association" {
    subnet_id = "${aws_subnet.PCO-pub-sub-a.id}"
    route_table_id = "${aws_vpc.PCO-VPC-Prod.main_route_table_id}"
}

# Associate subnet PCO-pub-sub-b to public route table
resource "aws_route_table_association" "PCO-pub-sub-b-association" {
    subnet_id = "${aws_subnet.PCO-pub-sub-b.id}"
    route_table_id = "${aws_route_table.PCO-rt-pub.id}"
}

# Associate subnet PCO-priv-sub-a to private route table
resource "aws_route_table_association" "PCO-priv-sub-a-association" {
    subnet_id = "${aws_subnet.PCO-priv-sub-a.id}"
    route_table_id = "${aws_route_table.PCO-rt-priv.id}"
}

# Associate subnet PCO-priv-sub-b to private route table
resource "aws_route_table_association" "PCO-priv-sub-b-association" {
    subnet_id = "${aws_subnet.PCO-priv-sub-b.id}"
    route_table_id = "${aws_route_table.PCO-rt-priv.id}"
}


resource "aws_security_group" "PCO_public_subnet_security_group" {
 name = "PCO_public_sg"
 description = "PCO_public_sg"
 vpc_id = "${aws_vpc.PCO-VPC-Prod.id}"
 tags = { Name = "PCO_public_sg"}
 ingress {
   from_port = 22
   to_port = 22
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
  }
   ingress {
   from_port = 0
   to_port = 0
      protocol = "-1"
      cidr_blocks = ["${var.pco-priv-sub-a}"]
}
    egress {
   from_port = 0
   to_port = 0
      protocol = "-1"
      cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "PCO_private_subnet_security_group" {
 name = "vpc2_private_sg"
 description = "vpc2_private_sg"
 vpc_id = "${aws_vpc.PCO-VPC-Prod.id}"
 tags = { Name = "vpc2_private_sg"}
   ingress {
   from_port = 0
   to_port = 0
      protocol = "-1"
      cidr_blocks = ["${var.pco-pub-sub-a}"]
  }
    ingress {
   from_port = 0
   to_port = 0
      protocol = "-1"
      cidr_blocks = ["${var.transit-priv-sub-a}"]
  }
    egress {
   from_port = 0
   to_port = 0
      protocol = "-1"
      cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "pco_public_instance" {
    ami = "ami-b2527ad2"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.PCO_public_subnet_security_group.id}"]
    subnet_id = "${aws_subnet.PCO-pub-sub-a.id}"
    tags {
        Name = "pco"
    }
}

resource "aws_instance" "pco_private_instance" {
    ami = "ami-b2527ad2"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.PCO_private_subnet_security_group.id}"]
    subnet_id = "${aws_subnet.PCO-priv-sub-a.id}"
    tags {
        Name = "pco2"
    }
}


/**
 * VPC peering connection.
 *
 * Establishes a relationship resource between the transit and tx VPC.
 */
resource "aws_vpc_peering_connection" "transit2tx" {
  peer_vpc_id = "${aws_vpc.TX-VPC-Prod.id}"
  vpc_id = "${aws_vpc.Transit-VPC.id}"
  auto_accept = true

accepter {
      allow_remote_vpc_dns_resolution = true
    }
    requester {
      allow_remote_vpc_dns_resolution = true
    }
}

/**
 * Route rule.
 *
 * Creates a new route rule on the "transit" VPC main route table. All requests
 * to the "tx" VPC's IP range will be directed to the VPC peering
 * connection.
 */
resource "aws_route" "transit2tx" {
  route_table_id = "${aws_vpc.Transit-VPC.main_route_table_id}"
  destination_cidr_block = "${aws_vpc.TX-VPC-Prod.cidr_block}"
  vpc_peering_connection_id = "${aws_vpc_peering_connection.transit2tx.id}"
}

/**
 * Route rule.
 *
 * Creates a new route rule on the "pco" VPC main route table. All
 * requests to the "pco" VPC's IP range will be directed to the VPC
 * peering connection.
 */
resource "aws_route" "tx2transit" {
  route_table_id = "${aws_vpc.TX-VPC-Prod.main_route_table_id}"
  destination_cidr_block = "${aws_vpc.Transit-VPC.cidr_block}"
  vpc_peering_connection_id = "${aws_vpc_peering_connection.transit2tx.id}"
}

1 个答案:

答案 0 :(得分:0)

我相信您需要使用数据源来引用“ Transit-VPC”

https://www.terraform.io/docs/providers/aws/d/vpc.html