Open Hashbury1 opened 1 year ago
hi, I checked your code you made small mistakes in CIDR block allocation, VPC resource creation, etc. Here am updated your code. Pls check and test your environment and close to the issues. Note:Commented with mistakes portion Solution.txt
provider "aws" { region = "us-east-1" }
resource "aws_vpc" "vijayvpc" { cidr_block = "10.0.0.0/16" tags = { Name = "TestVPC" }
}
resource "aws_subnet" "pubsub" { vpc_id = "aws_vpc.vijayvpc.id" cidr_block = "10.0.1.0/24" }
resource "aws_subnet" "privsub" { vpc_id = "aws_vpc.vijayvpc.id" cidr_block = "10.0.2.0/24" }
resource "aws_route_table" "pubRT" { vpc_id = aws_vpc.vijayvpc.id
route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.newgw.id
} }
resource "aws_route_table_association" "pubRT" { subnet_id = aws_subnet.pubsub.id route_table_id = aws_route_table.pubRT.id
}
/resource "aws_route_table_association" "pubRT" { subnet_id = "aws_subnet.pubsub.id" route_table_id = "aws_route_table.pubRT" }/
/resource "aws_route_table_association" "PrivRT" { subnet_id = "aws_subnet.vijayprivsub.id" route_table_id = "aws_route_table.privRT.id" } /
resource "aws_route_table_association" "PrivRT" { subnet_id = "aws_subnet.privsub.id" route_table_id = "aws_route_table.PrivRT.id" }
resource "aws_internet_gateway" "newgw" { vpc_id = aws_vpc.vijayvpc.id tags = { Name = "New InterNet Gateway" } }
resource "aws_eip" "new-EIP" { vpc = true tags = { Name = "new-EIP" } }
resource "aws_nat_gateway" "natgt" { allocation_id = aws_eip.new-EIP.id subnet_id = aws_subnet.pubsub.id
tags = {
Name = "gw NAT"
}
}
pls chk
As per the last update this issue was fixed.
Error: error creating EC2 Subnet: InvalidVpcID.NotFound: The vpc ID 'aws_vpc.vijayvpc.id' does not exist │ status code: 400, request id: 21143344-be76-4d5d-bbd3-0145dfb616ce │ │ with aws_subnet.pubsub, │ on main.tf line 5, in resource "aws_subnet" "pubsub": │ 5: resource "aws_subnet" "pubsub" { │ ╵ ╷ │ Error: error creating Route Table (aws_route_table.privRT.id) Association: InvalidRouteTableID.NotFound: The routeTable ID 'aws_route_table.privRT.id' does not exist │ status code: 400, request id: 1cb32278-280a-4084-ad59-42d73e9e44a9 │ │ with aws_route_table_association.PrivRT, │ on main.tf line 41, in resource "aws_route_table_association" "PrivRT": │ 41: resource "aws_route_table_association" "PrivRT" { │ ╵
resource "aws_vpc" "vijayvpc" { cidr_block = "10.0.0.0/24" }
resource "aws_subnet" "pubsub" { vpc_id = "aws_vpc.vijayvpc.id" cidr_block = "10.0.1.0/24" }
resource "aws_subnet" "privsub" { vpc_id = "aws_vpc.vijayvpc.id" cidr_block = "10.0.2.0/24" }
resource "aws_route_table" "privRT" { vpc_id = aws_vpc.vijayvpc.id
route { cidr_block = "0.0.0.0/0" nat_gateway_id = "aws_internet_gateway.newgw" } }
resource "aws_route_table" "pubRT" { vpc_id = aws_vpc.vijayvpc.id
route { cidr_block = "0.0.0.0/0" gateway_id = "aws_internet_gateway.newgw.id" } }
associating the public RT with the public subnets
resource "aws_route_table_association" "pubRT" { subnet_id = "aws_subnet.pubsub.id" route_table_id = "aws_route_table.pubRT" }
associating the Private RT with the Private subnets
resource "aws_route_table_association" "PrivRT" { subnet_id = "aws_subnet.vijayprivsub.id" route_table_id = "aws_route_table.privRT.id" }
create NAT Gateway resource and attach it to the VPC
resource "aws_nat_gateway" "natnew" { allocation_id = "aws_eip.myEIP.id" subnet_id = aws_subnet.pubsub.id }
resource "aws_internet_gateway" "newgw" { vpc_id = aws_vpc.myvijayvpc.id }
resource "aws_eip" "myEIP" { vpc = true }