无法使用 Terraform 创建 EC2。路线 Table 协会卡在创建模式

Unable to create EC2 using Terraform. Route Table Association stuck in creating mode

我正在尝试创建一个简单的基础设施,其中包括 EC2、VPC 和与 Internet 网关的 Internet 连接,但是当通过 terraform apply 创建基础设施时,终端输出卡在创建模式大约 5-6 分钟route table association using subnet id 然后最后抛出 vpc-id, routetableid, subnet id does not exist and not found 的错误。 下面分享一些具体的代码:

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"
    }
}

您需要删除所有参考值周围的 "vpc_id = "aws_vpc.main-vpc.id" 应该是 vpc_id = aws_vpc.main-vpc.id,等等。否则您会尝试创建一个 aws_route_table在具有文字 ID "aws_vpc.main-vpc.id".
的 VPC 中 每当你想引用变量或资源或数据源时,要么根本不包装在 " 中,要么使用 "something ${aws_vpc.main-vpc.id} ..."

进行插值

结果可能如下所示:

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"
    }
}

不能保证这有效,因为现在可能存在无效引用,但需要您清理这些引用