Skip to main content

Module: transit-gateway

Terraform module to provision:

  • AWS Transit Gateway
  • AWS Resource Access Manager (AWS RAM) Resource Share to share the Transit Gateway with the Organization or another AWS Account (configurable via the variables ram_resource_share_enabled and ram_principal)
  • Transit Gateway route table
  • Transit Gateway VPC attachments to connect multiple VPCs via the Transit Gateway
  • Transit Gateway route table propagations to create propagated routes and allow traffic from the Transit Gateway to the VPC attachments
  • Transit Gateway route table associations to allow traffic from the VPC attachments to the Transit Gateway
  • Transit Gateway static routes (static routes have a higher precedence than propagated routes)
  • Subnet routes to route traffic from the subnets in each VPC to the other Transit Gateway VPC attachments

Introduction

This module is configurable via the variable transit_gateway_config - see usage and examples below.

The variable transit_gateway_config is a map of environment names (e.g. prod, staging, dev) to the environment configurations.

Each environment configuration contains the following fields:

  • vpc_id - The ID of the VPC for which to create a VPC attachment and route table associations and propagations.
  • vpc_cidr - VPC CIDR block.
  • subnet_ids - The IDs of the subnets in the VPC.
  • static_routes - A list of Transit Gateway static route configurations. Note that static routes have a higher precedence than propagated routes.
  • subnet_route_table_ids - The IDs of the subnet route tables. The route tables are used to add routes to allow traffix from the subnets in one VPC to the other VPC attachments.
  • route_to - A set of environment names to route traffic from the current environment to the specified environments. In the example below, in the prod environment we create subnet routes to route traffic from the prod subnets to the VPC attachments in the staging and dev environments. Specify either route_to or route_to_cidr_blocks. route_to_cidr_blocks supersedes route_to.
  • route_to_cidr_blocks - A set of VPC CIDR blocks to route traffic from the current environment to the specified VPC CIDR blocks. In the example below, in the staging environment we create subnet routes to route traffic from the staging subnets to the dev VPC CIDR. Specify either route_to or route_to_cidr_blocks. route_to_cidr_blocks supersedes route_to.
  • transit_gateway_vpc_attachment_id - An existing Transit Gateway Attachment ID. If provided, the module will use it instead of creating a new one.

You now have the option to have Terraform manage route table entries by key, whereas previously they were only managed by index. The advantage of managing them by key is that if a route table ID or destination CIDR changes, only that entry is affected, whereas when managed by index, all the entries after the first affected index may be destroyed and re-created at a different index. The reason this is left as an option, with the default being to manage the entries by index, is that if you are creating the VPC or subnets at the same time you are creating the Transit Gateway, then Terraform will not be able to generate the keys during the plan phase and the plan will fail with the error The "for_each" value depends on resource attributes that cannot be determined until apply.... We recommend setting route_keys_enabled to true unless you get this error, in which case you must leave it set to its default value of false.

NOTE: This module requires Terraform 0.13 and newer since it uses module expansion with for_each.

Usage

Here's how to invoke this module in your projects:

locals {
transit_gateway_config = {
prod = {
vpc_id = module.vpc_prod.vpc_id
vpc_cidr = module.vpc_prod.vpc_cidr_block
subnet_ids = module.subnets_prod.private_subnet_ids
subnet_route_table_ids = module.subnets_prod.private_route_table_ids
route_to = ["staging", "dev"]
route_to_cidr_blocks = null
transit_gateway_vpc_attachment_id = null

static_routes = [
{
blackhole = true
destination_cidr_block = "0.0.0.0/0"
},
{
blackhole = false
destination_cidr_block = "172.16.1.0/24"
}
]
},

staging = {
vpc_id = module.vpc_staging.vpc_id
vpc_cidr = module.vpc_staging.vpc_cidr_block
subnet_ids = module.subnets_staging.private_subnet_ids
subnet_route_table_ids = module.subnets_staging.private_route_table_ids
route_to = null
route_to_cidr_blocks = [module.vpc_dev.vpc_cidr_block]
transit_gateway_vpc_attachment_id = null

static_routes = [
{
blackhole = false
destination_cidr_block = "172.32.1.0/24"
}
]
},

dev = {
vpc_id = module.vpc_dev.vpc_id
vpc_cidr = module.vpc_dev.vpc_cidr_block
subnet_ids = module.subnets_dev.private_subnet_ids
subnet_route_table_ids = module.subnets_dev.private_route_table_ids
route_to = null
route_to_cidr_blocks = null
transit_gateway_vpc_attachment_id = null
static_routes = null
}
}
}

module "transit_gateway" {
source = "cloudposse/transit-gateway/aws"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

ram_resource_share_enabled = false
config = local.transit_gateway_config

context = module.this.context
}

Examples

Here is a working example of using this module:

Here are automated tests for the complete example using bats and Terratest (which tests and deploys the example on AWS):

Here is an example of using this module in a multi-account environment (with the Transit Gateway in one AWS account and all the VPC attachments and routes in different AWS accounts):

Requirements

NameVersion
terraform>= 1.3
aws>= 4.4.0

Providers

NameVersion
aws>= 4.4.0

Modules

NameSourceVersion
subnet_route./modules/subnet_routen/a
thiscloudposse/label/null0.25.0
transit_gateway_route./modules/transit_gateway_routen/a

Resources

NameType
aws_ec2_transit_gateway.defaultresource
aws_ec2_transit_gateway_route_table.defaultresource
aws_ec2_transit_gateway_route_table_association.defaultresource
aws_ec2_transit_gateway_route_table_propagation.defaultresource
aws_ec2_transit_gateway_vpc_attachment.defaultresource
aws_ram_principal_association.defaultresource
aws_ram_resource_association.defaultresource
aws_ram_resource_share.defaultresource
aws_ec2_transit_gateway.thisdata source
aws_organizations_organization.defaultdata source
aws_vpc.defaultdata source

Inputs

NameDescriptionTypeDefaultRequired
additional_tag_mapAdditional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string){}no
allow_external_principalsIndicates whether principals outside your organization can be associated with a resource shareboolfalseno
attributesID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string)[]no
auto_accept_shared_attachmentsWhether resource attachment requests are automatically accepted. Valid values: disable, enable. Default value: disablestring"enable"no
configConfiguration for VPC attachments, Transit Gateway routes, and subnet routes
map(object({
vpc_id = string
vpc_cidr = string
subnet_ids = set(string)
subnet_route_table_ids = set(string)
route_to = set(string)
route_to_cidr_blocks = set(string)
transit_gateway_vpc_attachment_id = string
static_routes = set(object({
blackhole = bool
destination_cidr_block = string
}))
}))
nullno
contextSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
no
create_transit_gatewayWhether to create a Transit Gateway. If set to false, an existing Transit Gateway ID must be provided in the variable existing_transit_gateway_idbooltrueno
create_transit_gateway_route_tableWhether to create a Transit Gateway Route Table. If set to false, an existing Transit Gateway Route Table ID must be provided in the variable existing_transit_gateway_route_table_idbooltrueno
create_transit_gateway_route_table_association_and_propagationWhether to create Transit Gateway Route Table associations and propagationsbooltrueno
create_transit_gateway_vpc_attachmentWhether to create Transit Gateway VPC Attachmentsbooltrueno
default_route_table_associationWhether resource attachments are automatically associated with the default association route table. Valid values: disable, enable. Default value: disablestring"disable"no
default_route_table_propagationWhether resource attachments automatically propagate routes to the default propagation route table. Valid values: disable, enable. Default value: disablestring"disable"no
delimiterDelimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
stringnullno
descriptor_formatsDescribe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br/> format = string<br/> labels = list(string)<br/>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any{}no
dns_supportWhether resource attachments automatically propagate routes to the default propagation route table. Valid values: disable, enable. Default value: enablestring"enable"no
enabledSet to false to prevent the module from creating any resourcesboolnullno
environmentID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'stringnullno
existing_transit_gateway_idExisting Transit Gateway ID. If provided, the module will not create a Transit Gateway but instead will use the existing onestringnullno
existing_transit_gateway_route_table_idExisting Transit Gateway Route Table ID. If provided, the module will not create a Transit Gateway Route Table but instead will use the existing onestringnullno
id_length_limitLimit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
numbernullno
label_key_caseControls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
stringnullno
label_orderThe order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string)nullno
label_value_caseControls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
stringnullno
labels_as_tagsSet of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string)
[
"default"
]
no
nameID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
stringnullno
namespaceID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally uniquestringnullno
ram_principalDEPRECATED, please use ram_principals instead.

The principal to associate with the resource share. Possible values are an
AWS account ID, an Organization ARN, or an Organization Unit ARN.
stringnullno
ram_principalsA list of principals to associate with the resource share. Possible values
are:

AWS account ID
Organization ARN
* Organization Unit ARN

If this (and var.ram_principal) is not provided and
ram_resource_share_enabled is true, the Organization ARN will be used.
list(string)[]no
ram_resource_share_enabledWhether to enable sharing the Transit Gateway with the Organization using Resource Access Manager (RAM)boolfalseno
regex_replace_charsTerraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
stringnullno
route_keys_enabledIf true, Terraform will use keys to label routes, preventing unnecessary changes,
but this requires that the VPCs and subnets already exist before using this module.
If false, Terraform will use numbers to label routes, and a single change may
cascade to a long list of changes because the index or order has changed, but
this will work when the true setting generates the error The "for_each" value depends on resource attributes...
boolfalseno
route_timeoutsaws_route resource timeouts
object({
create = optional(string),
delete = optional(string),
update = optional(string)
})
{}no
stageID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'stringnullno
tagsAdditional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string){}no
tenantID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is forstringnullno
transit_gateway_cidr_blocksThe list of associated CIDR blocks. It can contain up to 1 IPv4 CIDR block
of size up to /24 and up to one IPv6 CIDR block of size up to /64. The IPv4
block must not be from range 169.254.0.0/16.
list(string)nullno
transit_gateway_descriptionTransit Gateway description. If not provided, one will be automatically generated.string""no
vpc_attachment_appliance_mode_supportWhether Appliance Mode support is enabled. If enabled, a traffic flow between a source and destination uses the same Availability Zone for the VPC attachment for the lifetime of that flow. Valid values: disable, enablestring"disable"no
vpc_attachment_dns_supportWhether resource attachments automatically propagate routes to the default propagation route table. Valid values: disable, enable. Default value: enablestring"enable"no
vpc_attachment_ipv6_supportWhether resource attachments automatically propagate routes to the default propagation route table. Valid values: disable, enable. Default value: enablestring"disable"no
vpn_ecmp_supportWhether resource attachments automatically propagate routes to the default propagation route table. Valid values: disable, enable. Default value: enablestring"enable"no

Outputs

NameDescription
ram_resource_share_idRAM resource share ID
subnet_route_idsSubnet route identifiers combined with destinations
transit_gateway_arnTransit Gateway ARN
transit_gateway_association_default_route_table_idTransit Gateway association default route table ID
transit_gateway_idTransit Gateway ID
transit_gateway_propagation_default_route_table_idTransit Gateway propagation default route table ID
transit_gateway_route_idsTransit Gateway route identifiers combined with destinations
transit_gateway_route_table_idTransit Gateway route table ID
transit_gateway_vpc_attachment_idsTransit Gateway VPC attachment IDs