InvalidVpcID.NotFound while VPC is created

I have a fairly simple Terraform script for AWS, based on an example from the terraform site. Whenever I run this I get an error:

Error: error attaching EC2 Internet Gateway (igw-xxxxx): Error attaching internet gateway: InvalidVpcID.NotFound: The vpc ID 'aws_vpc.test.id' does not exist status code: 400, request id: xxxx

The Terraform console log before it, does show the creation of the vpc:

ws_internet_gateway.default: Creating…
aws_vpc.test: Creating…
aws_instance.dev: Creating…
aws_vpc.test: Creation complete after 2s [id=vpc-xxxx]
aws_instance.dev: Still creating… [10s elapsed]
aws_instance.dev: Still creating… [20s elapsed]
aws_instance.dev: Still creating… [30s elapsed]
aws_instance.dev: Creation complete after 32s [id=i-xxxx]

And when I check the state with terraform state show 'aws_vpc.test' it returns the proper segment with ID. In the AWS console I can see that the VPC has been created. So I am a bit at a loss what is wrong here? :no_mouth:

Using:

  • Terraform: v0.12.20
  • AWS provider: v2.48.0
  • On OSX 10.15.2

Script as used (region is set the eu-central-1, and stripped down so the created VPC & gateway are not used):

provider "aws" {
  profile    = "default"
  region     = var.region
}

# Create a VPC to launch our instances into
resource "aws_vpc" "test" {
  cidr_block = "10.0.0.0/16"
  assign_generated_ipv6_cidr_block = true

  tags = {
    Name = "test"
  }
}

# Create an internet gateway to give subnet access to the outside world
resource "aws_internet_gateway" "default" {
  vpc_id = "aws_vpc.test.id"
}

resource "aws_instance" "dev" {
  ami           = var.amis[var.region]
  instance_type = "t2.micro"
}

I can provide the answer myself:
vpc_id = "aws_vpc.test.id" should read vpc_id = aws_vpc.test.id without the quotes.

When starting with a 0.11 version I misinterpreted the error: Terraform 0.11 and earlier required all non-constant expressions to be provided via interpolation syntax, but this pattern is now deprecated. To silence this warning, remove the "${ sequence from the start and the }" sequence from the end of this expression, leaving just the inner expression.

Ah well…