首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我计划使用terraform创建3个aws_vpc和peering

我计划使用terraform创建3个aws_vpc和peering
EN

Stack Overflow用户
提问于 2018-07-23 16:47:04
回答 1查看 254关注 0票数 0

我计划用terraform创建3个aws_vpc和peering。我的问题是所有3个配置文件都在不同的文件夹中。WHenn I运行terraform apply时出现此错误:资源'aws_vpc_peering_connection.transit2pco‘配置:变量aws_vpc.Transport-VPC中引用的未知资源’aws_vpc.Transport-VPC‘。

代码语言:javascript
复制
#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}"
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-23 18:35:59

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51474817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档