Search code examples
amazon-web-servicesterraformterraform-provider-awsterraform0.12+terraform-modules

How to create attachments in transit gateway module terraform


I have created a transit gateway using the terraform tgw module as shown below.

module "transit-gateway" {
  source          = "terraform-aws-modules/transit-gateway/aws"
  version         = "1.4.0"
  name            = "tgw-nprod"
  description     = "My TGW shared with several other AWS accounts"
  amazon_side_asn = 64532

  enable_auto_accept_shared_attachments = true
  vpc_attachments = {
    vpc1 = {
      vpc_id                                          = module.vpc.vpc_id
      subnet_ids                                      = module.vpc.private_subnets
      dns_support                                     = true
      ipv6_support                                    = false
      transit_gateway_default_route_table_association = false
      transit_gateway_default_route_table_propagation = false
    }
  }

  ram_allow_external_principals = true
  ram_principals                = [1234567890, 0987654321]

  tags = {
    Purpose = "tgw-testing"
  }
}

I have created vpc using the terraform vpc module.

When I run the above terraform Iam getting error "Error: error creating EC2 Transit Gateway VPC Attachment: DuplicateSubnetsInSameZone: Duplicate Subnets for same AZ"

I have 2 private subnet in ap-south-1 and 1 public in ap-south-1.


Solution

  • The AWS docs write that you can have your gateway in only one subnet per AZ:

    You must select at least one subnet. You can select only one subnet per Availability Zone.

    Your error msg suggests that your module.vpc.private_subnets are in same AZ. You have to redefine your VPC so that module.vpc.private_subnets are in two different AZs, or just use one subnet in your subnet_ids.

    To use one subnet:

    subnet_ids                                      = [module.vpc.private_subnets[0]]