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
andram_principals
) - 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 theprod
environment we create subnet routes to route traffic from theprod
subnets to the VPC attachments in thestaging
anddev
environments. Specify eitherroute_to
orroute_to_cidr_blocks
.route_to_cidr_blocks
supersedesroute_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 thestaging
environment we create subnet routes to route traffic from thestaging
subnets to thedev
VPC CIDR. Specify eitherroute_to
orroute_to_cidr_blocks
.route_to_cidr_blocks
supersedesroute_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):
Variables
Required Variables
Optional Variables
allow_external_principals
(bool
) optionalIndicates whether principals outside your organization can be associated with a resource share
Default value:
false
auto_accept_shared_attachments
(string
) optionalWhether resource attachment requests are automatically accepted. Valid values:
disable
,enable
. Default value:disable
Default value:
"enable"
config
optionalConfiguration for VPC attachments, Transit Gateway routes, and subnet routes
Type:
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
}))
}))Default value:
null
create_transit_gateway
(bool
) optionalWhether to create a Transit Gateway. If set to
false
, an existing Transit Gateway ID must be provided in the variableexisting_transit_gateway_id
Default value:
true
create_transit_gateway_route_table
(bool
) optionalWhether to create a Transit Gateway Route Table. If set to
false
, an existing Transit Gateway Route Table ID must be provided in the variableexisting_transit_gateway_route_table_id
Default value:
true
create_transit_gateway_route_table_association_and_propagation
(bool
) optionalWhether to create Transit Gateway Route Table associations and propagations
Default value:
true
create_transit_gateway_vpc_attachment
(bool
) optionalWhether to create Transit Gateway VPC Attachments
Default value:
true
default_route_table_association
(string
) optionalWhether resource attachments are automatically associated with the default association route table. Valid values:
disable
,enable
. Default value:disable
Default value:
"disable"
default_route_table_propagation
(string
) optionalWhether resource attachments automatically propagate routes to the default propagation route table. Valid values:
disable
,enable
. Default value:disable
Default value:
"disable"
dns_support
(string
) optionalWhether resource attachments automatically propagate routes to the default propagation route table. Valid values:
disable
,enable
. Default value:enable
Default value:
"enable"
existing_transit_gateway_id
(string
) optionalExisting Transit Gateway ID. If provided, the module will not create a Transit Gateway but instead will use the existing one
Default value:
null
existing_transit_gateway_route_table_id
(string
) optionalExisting Transit Gateway Route Table ID. If provided, the module will not create a Transit Gateway Route Table but instead will use the existing one
Default value:
null
ram_principal
(string
) optionalDEPRECATED, 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.Default value:
null
ram_principals
(list(string)
) optionalA 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
istrue
, the Organization ARN will be used.Default value:
[ ]
- AWS account ID
ram_resource_share_enabled
(bool
) optionalWhether to enable sharing the Transit Gateway with the Organization using Resource Access Manager (RAM)
Default value:
false
route_keys_enabled
(bool
) optionalIf 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 thetrue
setting generates the errorThe "for_each" value depends on resource attributes...
Default value:
false
route_timeouts
optionalaws_route resource timeouts
Type:
object({
create = optional(string),
delete = optional(string),
update = optional(string)
})Default value:
{ }
transit_gateway_cidr_blocks
(list(string)
) optionalThe 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.Default value:
null
transit_gateway_description
(string
) optionalTransit Gateway description. If not provided, one will be automatically generated.
Default value:
""
vpc_attachment_appliance_mode_support
(string
) optionalWhether 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
,enable
Default value:
"disable"
vpc_attachment_dns_support
(string
) optionalWhether resource attachments automatically propagate routes to the default propagation route table. Valid values:
disable
,enable
. Default value:enable
Default value:
"enable"
vpc_attachment_ipv6_support
(string
) optionalWhether resource attachments automatically propagate routes to the default propagation route table. Valid values:
disable
,enable
. Default value:enable
Default value:
"disable"
vpn_ecmp_support
(string
) optionalWhether resource attachments automatically propagate routes to the default propagation route table. Valid values:
disable
,enable
. Default value:enable
Default value:
"enable"
Context Variables
The following variables are defined in the context.tf
file of this module and part of the terraform-null-label pattern.
context.tf
file of this module and part of the terraform-null-label pattern.additional_tag_map
(map(string)
) optionalAdditional key-value pairs to add to each map in
tags_as_list_of_maps
. Not added totags
orid
.
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.Required: No
Default value:
{ }
attributes
(list(string)
) optionalID element. Additional attributes (e.g.
workers
orcluster
) to add toid
,
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 thedelimiter
and treated as a single ID element.Required: No
Default value:
[ ]
context
(any
) optionalSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables asnull
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.Required: No
Default value:
{
"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
}delimiter
(string
) optionalDelimiter to be used between ID elements.
Defaults to-
(hyphen). Set to""
to use no delimiter at all.Required: No
Default value:
null
descriptor_formats
(any
) optionalDescribe 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 isany
so the map values can later be enhanced to provide additional options.)
format
is a Terraform format string to be passed to theformat()
function.
labels
is a list of labels, in order, to pass toformat()
function.
Label values will be normalized before being passed toformat()
so they will be
identical to how they appear inid
.
Default is{}
(descriptors
output will be empty).Required: No
Default value:
{ }
enabled
(bool
) optionalSet to false to prevent the module from creating any resources
Required: NoDefault value:
null
environment
(string
) optionalID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: NoDefault value:
null
id_length_limit
(number
) optionalLimit
id
to this many characters (minimum 6).
Set to0
for unlimited length.
Set tonull
for keep the existing setting, which defaults to0
.
Does not affectid_full
.Required: No
Default value:
null
label_key_case
(string
) optionalControls the letter case of the
tags
keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via thetags
input.
Possible values:lower
,title
,upper
.
Default value:title
.Required: No
Default value:
null
label_order
(list(string)
) optionalThe 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.Required: No
Default value:
null
label_value_case
(string
) optionalControls 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 thetags
input.
Possible values:lower
,title
,upper
andnone
(no transformation).
Set this totitle
and setdelimiter
to""
to yield Pascal Case IDs.
Default value:lower
.Required: No
Default value:
null
labels_as_tags
(set(string)
) optionalSet 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 thetags
output.
Set to[]
to suppress all generated tags.
Notes:
The value of thename
tag, if included, will be theid
, not thename
.
Unlike othernull-label
inputs, the initial setting oflabels_as_tags
cannot be
changed in later chained modules. Attempts to change it will be silently ignored.Required: No
Default value:
[
"default"
]name
(string
) optionalID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as atag
.
The "name" tag is set to the fullid
string. There is no tag with the value of thename
input.Required: No
Default value:
null
namespace
(string
) optionalID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: NoDefault value:
null
regex_replace_chars
(string
) optionalTerraform 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.Required: No
Default value:
null
stage
(string
) optionalID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: NoDefault value:
null
tags
(map(string)
) optionalAdditional tags (e.g.
{'BusinessUnit': 'XYZ'}
).
Neither the tag keys nor the tag values will be modified by this module.Required: No
Default value:
{ }
tenant
(string
) optionalID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: NoDefault value:
null
Outputs
ram_resource_share_id
RAM resource share ID
subnet_route_ids
Subnet route identifiers combined with destinations
transit_gateway_arn
Transit Gateway ARN
transit_gateway_association_default_route_table_id
Transit Gateway association default route table ID
transit_gateway_id
Transit Gateway ID
transit_gateway_propagation_default_route_table_id
Transit Gateway propagation default route table ID
transit_gateway_route_ids
Transit Gateway route identifiers combined with destinations
transit_gateway_route_table_id
Transit Gateway route table ID
transit_gateway_vpc_attachment_ids
Transit Gateway VPC attachment IDs
Dependencies
Requirements
terraform
, version:>= 1.3
aws
, version:>= 4.4.0
Providers
aws
, version:>= 4.4.0
Modules
Name | Version | Source | Description |
---|---|---|---|
subnet_route | latest | ./modules/subnet_route | Create routes in the subnets' route tables to route traffic from subnets to the Transit Gateway VPC attachments Only route to VPCs of the environments defined in route_to attribute |
this | 0.25.0 | cloudposse/label/null | n/a |
transit_gateway_route | latest | ./modules/transit_gateway_route | Static Transit Gateway routes Static routes have a higher precedence than propagated routes https://docs.aws.amazon.com/vpc/latest/tgw/how-transit-gateways-work.html https://docs.aws.amazon.com/vpc/latest/tgw/tgw-route-tables.html |
Resources
The following resources are used by this module:
aws_ec2_transit_gateway.default
(resource)aws_ec2_transit_gateway_route_table.default
(resource)aws_ec2_transit_gateway_route_table_association.default
(resource)aws_ec2_transit_gateway_route_table_propagation.default
(resource)aws_ec2_transit_gateway_vpc_attachment.default
(resource)aws_ram_principal_association.default
(resource)aws_ram_resource_association.default
(resource)aws_ram_resource_share.default
(resource)
Data Sources
The following data sources are used by this module:
aws_ec2_transit_gateway.this
(data source)aws_organizations_organization.default
(data source)aws_vpc.default
(data source)