Skip to content

Feature/existing transit gateway support#24

Open
thiago4dnx wants to merge 4 commits intomasterfrom
feature/existing-transit-gateway-support
Open

Feature/existing transit gateway support#24
thiago4dnx wants to merge 4 commits intomasterfrom
feature/existing-transit-gateway-support

Conversation

@thiago4dnx
Copy link

Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue.

Description

Add support for managing existing Transit Gateways created via AWS Console or CLI.

Changes

  • Add transit_gateway_id variable (optional, default: null)
  • Enable RAM sharing for existing Transit Gateways
  • Maintain full backwards compatibility

Use Cases

  • Manage console-created Transit Gateways with Terraform
  • Gradual migration to infrastructure-as-code
  • Import existing TGW infrastructure without recreation

Backwards Compatibility

✅ Fully backwards compatible - default behavior unchanged

Types of changes

What types of changes does your code introduce to <repo_name>?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING.md doc.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Add transit_gateway_id variable to allow users to provide an existing
Transit Gateway ID instead of always creating a new one. This enables
managing RAM sharing and VPC attachments for Transit Gateways created
via AWS Console or CLI.

Key changes:
- Add transit_gateway_id variable (default: null)
- Add local.transit_gateway_id to determine which TGW to use
- Update TGW resource to only create when transit_gateway_id is null
- Enable RAM sharing for existing TGWs
- Add data sources for region and account ID (needed for ARN construction)
- Update outputs to maintain backwards compatibility

Backwards compatibility:
- Default behavior unchanged (creates new TGW when transit_gateway_id is null)
- No breaking changes to existing configurations
- Output type remains string for backwards compatibility

Use cases:
- Manage existing console-created Transit Gateways with Terraform
- Import existing TGW infrastructure without recreation
- Gradual migration to infrastructure-as-code
- Add usage example for managing existing Transit Gateway
- Add transit_gateway_id to inputs table
- Clarify use cases for new vs existing TGW
@thiago4dnx thiago4dnx requested a review from Copilot February 3, 2026 04:23
@amazon-q-developer
Copy link

⚠️ Review Failed

I was unable to finalize my review because the pull request head or merge base was modified since I began my review. Please try again.

Request ID: a4248b94-417d-53ac-9fe1-ea6f3b1d79f6

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for managing existing Transit Gateways that were created outside of Terraform (via AWS Console or CLI), enabling gradual migration to infrastructure-as-code without requiring resource recreation.

Changes:

  • Introduces optional transit_gateway_id variable to reference existing Transit Gateways
  • Updates resource count conditions to prevent creating new TGW when existing ID is provided
  • Enables RAM sharing functionality for both new and existing Transit Gateways

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
_variables.tf Adds new optional transit_gateway_id variable with null default
_locals.tf Implements logic to select between provided, created, or data source TGW ID
transit-gateway.tf Updates resource counts to skip creation when existing TGW ID provided
ram.tf Extends RAM sharing to work with existing TGWs and constructs ARN manually when needed
_outputs.tf Modifies output to return provided TGW ID when available
_data.tf Adds data sources for current region and account identity
README.md Documents new usage pattern for existing Transit Gateways

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

transit_gateway_id = (
var.transit_gateway_id != null ? var.transit_gateway_id :
var.transit_gateway_enabled ? aws_ec2_transit_gateway.default[0].id :
data.aws_ec2_transit_gateway.default[0].id
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When var.transit_gateway_id is null and var.transit_gateway_enabled is false, this references data.aws_ec2_transit_gateway.default[0].id, but the data source only has count = try(var.attachment, false) ? 1 : 0 in _data.tf. If var.attachment is false, the data source won't exist, causing an index out of bounds error.

Suggested change
data.aws_ec2_transit_gateway.default[0].id
var.attachment ? data.aws_ec2_transit_gateway.default[0].id : null

Copilot uses AI. Check for mistakes.
count = var.transit_gateway_enabled || var.transit_gateway_id != null ? 1 : 0

resource_arn = aws_ec2_transit_gateway.default[0].arn
resource_arn = var.transit_gateway_id != null ? "arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:transit-gateway/${var.transit_gateway_id}" : aws_ec2_transit_gateway.default[0].arn
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ARN construction is duplicated as a long inline string. Consider extracting this to a local variable for better readability and maintainability.

Copilot uses AI. Check for mistakes.
README.md Outdated
Comment on lines +66 to +67
transit_gateway_id = "tgw-0123456789abcdef0"
transit_gateway_enabled = true # Keep true to enable RAM sharing
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment suggests keeping transit_gateway_enabled = true is required for RAM sharing with existing TGWs, but based on the logic in ram.tf line 2, RAM sharing is enabled when either var.transit_gateway_enabled is true OR var.transit_gateway_id != null. This documentation could be misleading.

Suggested change
transit_gateway_id = "tgw-0123456789abcdef0"
transit_gateway_enabled = true # Keep true to enable RAM sharing
transit_gateway_id = "tgw-0123456789abcdef0"
# RAM sharing is enabled when either transit_gateway_enabled is true or transit_gateway_id is set (as in this example).
transit_gateway_enabled = false

Copilot uses AI. Check for mistakes.
@@ -1,5 +1,5 @@
resource "aws_ec2_transit_gateway" "default" {
count = var.transit_gateway_enabled ? 1 : 0
count = var.transit_gateway_id == null && var.transit_gateway_enabled ? 1 : 0
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is duplicated in multiple resources (lines 2 and 43). Consider extracting to a local variable like local.create_transit_gateway for consistency and easier maintenance.

Copilot uses AI. Check for mistakes.
RAM sharing is enabled when either transit_gateway_enabled is true
OR transit_gateway_id is set. Updated comment to reflect this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants