0
votes

I have created a VPC peering between 2 AWS accounts. One VPC for account A is in us-east-1 and second VPC for account B is in us-west-2.

  • The peering connection is active and is working fine!

  • I need to now add it in my terraform code for both Accounts terraform codes.

  • I have adding it in ACCOUNT B first right now! This is what I have done till yet:

# VPC peering connection #
# (3)                    #
##########################

resource "aws_vpc_peering_connection" "this_3" {
  count         = var.create_peering_3 ? 1 : 0
  peer_owner_id = var.peer_account_id_3
  peer_vpc_id   = var.vpc_peer_id_3
  vpc_id        = module.vpc-us-west-2.vpc_id
  auto_accept   = var.auto_accept_peering_3
}

and these are the variables:

##########################
# VPC peering connection #
# (3)         #
##########################

variable "peer_account_id_3" {
  description = "AWS owner account ID"
  default     = "**account*A**"
}

variable "vpc_peer_id_3" {
  description = "Peer VPC ID"
  default     = "vpc-029***"
}

variable "peer_cidr_block_3" {
  description = "Peer VPC CIDR block"
  default     = "192.168.0.0/16"
}

variable "auto_accept_peering_3" {
  description = "Auto accept peering connection"
  default     = true
}

variable "create_peering_3" {
  description = "Create peering connection, 0 to not create"
  default     = true
  type        = bool
}

variable "this_vpc_id_3" {
  description = "This VPC ID"
  default     = "vpc-0e2**"
}

variable "private_route_table_ids_3" {
  type        = list(string)
  description = "A list of private route tables"
  default     = ["rtb-0**, rtb-04**"]
}

variable "public_route_table_ids_3" {
  type        = list(string)
  description = "A list of public route tables"
  default     = ["rtb-0f**"]
}

variable "peering_id_3" {
  description = "Provide already existing peering connection id"
  default     = "pcx-0878***"
}

Now when I run tf plan it is creating it.. which I do not want it to do, as it is already made!

  • I want to see no changes in my plan!

  • I have also tried using the tf import command:

terraform import aws_vpc_peering_connection.this_3 pcx-0878******

but it gives me this error:

Error: Cannot import non-existent remote object

While attempting to import an existing object to
aws_vpc_peering_connection.this_3, the provider detected that no object exists
with the given id. Only pre-existing objects can be imported; check that the
id is correct and that it is associated with the provider's configured region
or endpoint, or use "terraform apply" to create a new remote object for this
resource.
  • I do not know how to fix this
1
Which account is creating the VPC connection, A or B?Marcin
@John Rotenstein account Amgb

1 Answers

0
votes

Confirm if you are using the right credentials from account B.

provider "aws" {
  alias      = "account_b"
  region     = "us-west-2"
  access_key = "my-access-key"
  secret_key = "my-secret-key"
}

resource "aws_vpc_peering_connection" "this_3" {
  provider      = "aws.account_b"
  count         = var.create_peering_3 ? 1 : 0
  peer_owner_id = var.peer_account_id_3
  peer_vpc_id   = var.vpc_peer_id_3
  vpc_id        = module.vpc-us-west-2.vpc_id
  auto_accept   = var.auto_accept_peering_3
}

And try to run the import again