Error: Provider configuration not present (vpc-peering accept only)

main.tf

resource "aws_vpc_peering_connection_accepter" "accepter" {
  provider                  = aws.accepter
  vpc_peering_connection_id = aws_vpc_peering_connection.owner.id
  auto_accept               = true
  tags = {
    Name = "peer_to_owner"
  }
}
resource "aws_route" "accepter" {
  provider                  = aws.accepter
  count                     = length(data.aws_route_tables.accepter.ids)
  route_table_id            = tolist(data.aws_route_tables.accepter.ids)[count.index]
  destination_cidr_block    = data.aws_vpc.owner.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.owner.id
}

backend.tf

terraform {
  backend "s3" {
    bucket         = "xyzzy"
    key            = "aws-vpc"
    region         = "eu-west-1"
    encrypt        = true
  }
}

provider.tf

provider "aws" {
  region = var.region
}

variable.tf

variable "region" {
  description = "Infrastructure region"
  type        = string
  default     = "us-east-1"
}
$ terraform validate

79╷

80│ Error: Provider configuration not present

81│

82│ To work with aws_vpc_peering_connection_accepter.accepter its original

83│ provider configuration at

84│ provider["registry.terraform.io/hashicorp/aws"].accepter is required, but

85│ it has been removed. This occurs when a provider configuration is removed

86│ while objects created by that provider still exist in the state. Re-add the

87│ provider configuration to destroy

88│ aws_vpc_peering_connection_accepter.accepter, after which you can remove

89│ the provider configuration again.

90╵

91╷

92│ Error: Provider configuration not present

93│

94│ To work with aws_route.accepter its original provider configuration at

95│ provider["registry.terraform.io/hashicorp/aws"].accepter is required, but

96│ it has been removed. This occurs when a provider configuration is removed

97│ while objects created by that provider still exist in the state. Re-add the

98│ provider configuration to destroy aws_route.accepter, after which you can

99│ remove the provider configuration again.

100╵

Can someone please help to understand what I am missing?

You previously created resources using a hashicorp/aws provider with alias accepter.

You have removed the provider configuration with that alias, despite still having resources in the Terraform state, as well as still referencing it in your code:

Since you’re still using it, you need to add it back.

thanks @maxb , but now I see different issue :slight_smile:

main.tf

data "aws_vpc" "owner" {
  provider = aws.owner
  id       = var.owner_vpc_id
}
data "aws_vpc" "accepter" {
  provider = aws.accepter
  id       = var.accepter_vpc_id
}
data "aws_route_tables" "owner" {
  provider = aws.owner
  vpc_id   = var.owner_vpc_id
}
data "aws_route_tables" "accepter" {
  provider = aws.accepter
  vpc_id   = data.aws_vpc.accepter.id
}
locals {
  accepter_account_id = element(split(":", data.aws_vpc.accepter.arn), 4)
  owner_account_id    = element(split(":", data.aws_vpc.owner.arn), 4)
}
resource "aws_vpc_peering_connection" "owner" {
  provider      = aws.owner
  vpc_id        = var.owner_vpc_id
  peer_region  = "ap-northeast-1"
  peer_vpc_id   = data.aws_vpc.accepter.id
  peer_owner_id = local.accepter_account_id
  tags = {
    Name = "peer_to_accepter"
  }
}
resource "aws_vpc_peering_connection_accepter" "accepter" {
  provider                  = aws.accepter
  vpc_peering_connection_id = aws_vpc_peering_connection.owner.id
  auto_accept               = true
  tags = {
    Name = "peer_to_owner"
  }
}
resource "aws_route" "owner" {
  provider                  = aws.owner
  count                     = length(data.aws_route_tables.owner.ids)
  route_table_id            = tolist(data.aws_route_tables.owner.ids)[count.index]
  destination_cidr_block    = data.aws_vpc.accepter.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.owner.id
}
resource "aws_route" "accepter" {
  provider                  = aws.accepter
  count                     = length(data.aws_route_tables.accepter.ids)
  route_table_id            = tolist(data.aws_route_tables.accepter.ids)[count.index]
  destination_cidr_block    = data.aws_vpc.owner.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.owner.id
} 


provider.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.42.0"
    }
  }
}
provider "aws" {
  region     = "us-east-1"
}
provider "aws" {
  region     = "ap-northeast-1"
  alias      = "owner"
}
provider "aws" {
  region     = "us-east-1"
  alias      = "accepter"
}

variable "region" {
  description = "Infrastructure region"
  type        = string
  default     = "us-east-1"
}
variable "owner_vpc_id" {
  description = "owner vpc"
  default     = "vpc-abc"
}
variable "accepter_vpc_id" {
  description = "acceptor vpc"
  default     = "vpc-xyz"
}

Error:

aws_vpc_peering_connection.owner: Creating…
79╷
80│ Error: Error creating VPC Peering Connection: InvalidVpcID.NotFound: The vpc ID ‘vpc-xyz’ does not exist
81│ status code: 400, request id: blabla
82│
83│ with aws_vpc_peering_connection.owner,
84│ on main.tf line 21, in resource “aws_vpc_peering_connection” “owner”:
85│ 21: resource “aws_vpc_peering_connection” “owner” {

resolved it
peer_region = “us-east-1” fixed the issue, as I was setting wrong region

aws_vpc_peering_connection.owner: Creating…

79aws_vpc_peering_connection.owner: Still creating… [10s elapsed]

80aws_vpc_peering_connection.owner: Still creating… [20s elapsed]

81aws_vpc_peering_connection.owner: Still creating… [30s elapsed]

82aws_vpc_peering_connection.owner: Still creating… [40s elapsed]

83aws_vpc_peering_connection.owner: Still creating… [50s elapsed]

84aws_vpc_peering_connection.owner: Still creating… [1m0s elapsed]

85╷

86│ Error: Error waiting for VPC Peering Connection to become available: Error waiting for VPC Peering Connection (pcx-xyz) to become available: timeout while waiting for state to become ‘pending-acceptance, active’ (last state: ‘initiating-request’, timeout: 1m0s)

Is there way to set longer timeout or wait until “aws_vpc_peering_connection.owner: Creating…” completes?

I can’t help with that, but it might be worth making a new post with a relevant title, to see if it catches anyone else’s attention.