0
votes

I am trying to create a simple infrastructure which includes EC2, VPC and internet connectivity with Internet Gateway, but while the infrastructure is being created through terraform apply the terminal output gets stuck in creating mode for approximately 5-6 minutes for route table association using subnet id and then finally throws error that vpc-id, routetableid, subnet id does not exist and not found. Sharing some specific code below :

resource "aws_route_table" "dev-public-crt" {
    vpc_id = "aws_vpc.main-vpc.id"
    
    route {
        cidr_block = "0.0.0.0/0"        
        gateway_id = "aws_internet_gateway.dev-igw.id" 
    }
    
    tags = {
        Name = "dev-public-crt"
    }
}

resource "aws_route_table_association" "dev-crta-public-subnet-1"{
    subnet_id = "aws_subnet.dev-subnet-public-1.id"
    route_table_id = "aws_route_table.dev-public-crt.id"
}

resource "aws_vpc" "dev-vpc" {
    cidr_block = "10.0.0.0/16"
    tags = {
        Name = "dev-vpc"
    }    
}

resource "aws_subnet" "dev-subnet-public-1" {
    vpc_id = "aws_vpc.dev-vpc.id"
    cidr_block = "10.0.1.0/24"
    map_public_ip_on_launch = "true"  
    tags = {
        Name = "dev-subnet-public-1"
    }
}
1

1 Answers

0
votes

You need to remove the " around all the reference values you have there: vpc_id = "aws_vpc.main-vpc.id" should be vpc_id = aws_vpc.main-vpc.id, etc. Otherwise you try to create a aws_route_table in the vpc with the literal id "aws_vpc.main-vpc.id".
Whenever you want to reference variables or resources or data sources either do not wrap in " at all, or interpolate using "something ${aws_vpc.main-vpc.id} ..."

The result should probably look like:

resource "aws_route_table" "dev-public-crt" {
    vpc_id = aws_vpc.main-vpc.id
    
    route {
        cidr_block = "0.0.0.0/0"        
        gateway_id = aws_internet_gateway.dev-igw.id
    }
    
    tags = {
        Name = "dev-public-crt"
    }
}

resource "aws_route_table_association" "dev-crta-public-subnet-1"{
    subnet_id = aws_subnet.dev-subnet-public-1.id
    route_table_id = aws_route_table.dev-public-crt.id
}

resource "aws_vpc" "dev-vpc" {
    cidr_block = "10.0.0.0/16"
    tags = {
        Name = "dev-vpc"
    }    
}

resource "aws_subnet" "dev-subnet-public-1" {
    vpc_id = aws_vpc.dev-vpc.id
    cidr_block = "10.0.1.0/24"
    map_public_ip_on_launch = "true"  
    tags = {
        Name = "dev-subnet-public-1"
    }
}

No guarantee that this works because now there could be invalid references, but those need to cleaned up by you