Skip to content

Commit

Permalink
feat: added new resources and updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mamrajyadav committed Jul 18, 2023
1 parent f57d3d1 commit 2f5d76a
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 46 deletions.
2 changes: 1 addition & 1 deletion _example/different-account/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module "transit-gateway" {
aws_ram_resource_share_accepter = true
resource_share_arn = "arn:aws:ram:eu-west-1:XXXXXXXXXXX:resource-share/XXXXXXXXXXXXXXXXXXXXXXXXXX"
subnet_ids = module.subnets.private_subnet_id

# VPC Attachements
vpc_attachement_create = false # Enable After once create the subnets
vpc_id = module.vpc.vpc_id
Expand Down
25 changes: 25 additions & 0 deletions _example/single-account/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions _example/single-account/example.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
##------------------------------------------------------------------------------
## Provider block added, Use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS.
##------------------------------------------------------------------------------
provider "aws" {
region = "eu-west-1"
}

##------------------------------------------------------------------------------
## A VPC is a virtual network that closely resembles a traditional network that you'd operate in your own data center.
##------------------------------------------------------------------------------
module "vpc" {
source = "clouddrove/vpc/aws"
version = "1.3.1"
Expand All @@ -12,6 +18,9 @@ module "vpc" {
cidr_block = "10.10.0.0/16"
}

##------------------------------------------------------------------------------
## A subnet is a range of IP addresses in your VPC.
##------------------------------------------------------------------------------
module "subnets" {
source = "clouddrove/subnet/aws"
version = "1.3.0"
Expand All @@ -28,6 +37,9 @@ module "subnets" {
ipv6_cidr_block = module.vpc.ipv6_cidr_block
}

##------------------------------------------------------------------------------
## A VPC is a virtual network that closely resembles a traditional network that you'd operate in your own data center.
##------------------------------------------------------------------------------
module "vpc-other" {
source = "clouddrove/vpc/aws"
version = "1.3.1"
Expand All @@ -39,8 +51,11 @@ module "vpc-other" {
cidr_block = "192.168.0.0/16"
}

##------------------------------------------------------------------------------
## A subnet is a range of IP addresses in your VPC.
##------------------------------------------------------------------------------
module "subnets-other" {
source = "clouddrove/subnet/aws"
source = "clouddrove/subnet/aws"
version = "1.3.0"

name = "subnets"
Expand All @@ -56,6 +71,9 @@ module "subnets-other" {

}

##------------------------------------------------------------------------------
## transit-gateway module call.
##------------------------------------------------------------------------------
module "transit-gateway" {
source = "./../../"

Expand All @@ -75,13 +93,16 @@ module "transit-gateway" {
resource_share_allow_external_principals = true
resource_share_account_ids = ["XXXXXXXXXXXXX"]
subnet_ids = module.subnets.private_subnet_id

# VPC Attachements
vpc_attachement_create = false # Enable After once create the subnets
vpc_id = module.vpc.vpc_id
destination_cidr_block = ["192.168.0.0/16"]
}

##------------------------------------------------------------------------------
## vpc-attachement module call.
##------------------------------------------------------------------------------
module "vpc-attachement" {
source = "./../../"
name = "transit-gateway"
Expand Down
11 changes: 8 additions & 3 deletions _example/single-account/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ output "tags" {
description = "A mapping of tags to assign to the transit-gateway."
}

output "resource_share_arn" {
value = module.transit-gateway.resource_share_arn
description = "The ARN of the RAM."
output "ec2_transit_gateway_arn" {
value = module.transit-gateway.ec2_transit_gateway_arn
description = "EC2 Transit Gateway Amazon Resource Name (ARN)"
}

output "ec2_transit_gateway_route_table_id" {
value = module.transit-gateway.ec2_transit_gateway_route_table_id
description = "EC2 Transit Gateway Route Table identifier"
}
11 changes: 0 additions & 11 deletions _example/single-account/versions.tf

This file was deleted.

89 changes: 63 additions & 26 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Managed By : CloudDrove
# Description : This Script is used to create Transit Gateway.
# Copyright @ CloudDrove. All Right Reserved.

#Module : Label
#Description : This terraform module is designed to generate consistent label names and tags
# for resources. You can use terraform-labels to implement a strict naming
# convention.
##------------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##------------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
Expand All @@ -18,8 +13,9 @@ module "labels" {
label_order = var.label_order
}

#Module : TRANSIT GATEWAY
#Description : Manages an EC2 Transit Gateway.
##------------------------------------------------------------------------------
## A transit gateway acts as a Regional virtual router for traffic flowing between your virtual private clouds (VPCs) and on-premises networks.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway" "main" {
count = var.enable && var.tgw_create ? 1 : 0

Expand All @@ -32,9 +28,9 @@ resource "aws_ec2_transit_gateway" "main" {
tags = module.labels.tags
}


#Module : TRANSIT GATEWAY VPC ATTACHMENT
#Description : Get information on an EC2 Transit Gateway VPC Attachment.
##------------------------------------------------------------------------------
## Get information on an EC2 Transit Gateway VPC Attachment.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway_vpc_attachment" "main" {
count = var.enable && var.vpc_attachement_create ? 1 : 0

Expand All @@ -49,12 +45,11 @@ resource "aws_ec2_transit_gateway_vpc_attachment" "main" {
"Name" = format("%s-vpc-attachment", module.labels.id)
}
)


}

#Module : AWS RAM RESOURCE SHARE
#Description : Manages a Resource Access Manager (RAM) Resource Share. To association principals with the share.
##------------------------------------------------------------------------------
## You can use AWS Resource Access Manager (RAM) to share a transit gateway for VPC attachments across accounts or across your organization in AWS.
##------------------------------------------------------------------------------
resource "aws_ram_resource_share" "main" {
count = var.enable && var.resource_share_enable ? 1 : 0

Expand All @@ -68,17 +63,19 @@ resource "aws_ram_resource_share" "main" {
)
}

#Module : RAM PRINCIPAL ASSOCIATION
#Description : Provides a Resource Access Manager (RAM) principal association.
##------------------------------------------------------------------------------
## Provides a Resource Access Manager (RAM) principal association. Depending if RAM Sharing with AWS Organizations is enabled, the RAM behavior with different principal types changes.
##------------------------------------------------------------------------------
resource "aws_ram_principal_association" "main" {
count = var.enable && var.resource_share_enable ? length(var.resource_share_account_ids) : 0

principal = element(var.resource_share_account_ids, count.index)
resource_share_arn = join("", aws_ram_resource_share.main.*.id)
}

#Module : RAM ASSOCIATION
#Description : Manages a Resource Access Manager (RAM) Resource Association.
##------------------------------------------------------------------------------
## The Resource Association in AWS RAM can be configured in Terraform with the resource name aws_ram_resource_association.
##------------------------------------------------------------------------------
resource "aws_ram_resource_association" "main" {
count = var.enable && var.resource_share_enable ? 1 : 0

Expand All @@ -89,12 +86,11 @@ resource "aws_ram_resource_association" "main" {
data "aws_route_tables" "main" {
count = var.enable && var.vpc_attachement_create ? 1 : 0
vpc_id = var.vpc_id


}

#Module : AWS ROUTE
#Description : Provides a resource to create a routing table entry (a route) in a VPC routing table.
##------------------------------------------------------------------------------
## Provides a resource to create a routing table entry (a route) in a VPC routing table.
##------------------------------------------------------------------------------
resource "aws_route" "main" {
count = var.enable && var.vpc_attachement_create ? length(distinct(sort(data.aws_route_tables.main[0].ids)), ) * length(var.destination_cidr_block) : 0

Expand All @@ -107,7 +103,48 @@ resource "aws_route" "main" {
]
}

##------------------------------------------------------------------------------
## An AWS Transit Gateway Route Table includes dynamic routes, static routes and blackhole routes.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway_route_table" "this" {
count = var.tgw_create ? 1 : 0

transit_gateway_id = aws_ec2_transit_gateway.main[0].id

tags = merge(
module.labels.tags,
{ Name = var.name },
)
}

##------------------------------------------------------------------------------
## The Transit Gateway Route in Amazon EC2 can be configured in Terraform with the resource name aws_ec2_transit_gateway_route.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway_route" "this" {
count = var.vpc_attachement_create ? 1 : 0

destination_cidr_block = "0.0.0.0/0"
blackhole = true

transit_gateway_route_table_id = aws_ec2_transit_gateway.main.*.association_default_route_table_id
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.main.*.id
}

##------------------------------------------------------------------------------
## Associates the specified attachment with the specified transit gateway route table. You can associate one route table with an attachment.
##------------------------------------------------------------------------------
resource "aws_ec2_transit_gateway_route_table_association" "this" {
count = var.vpc_attachement_create ? 1 : 0

transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.main.*.id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[0].id
}

##------------------------------------------------------------------------------
## Associates the specified attachment with the specified transit gateway route table. You can associate one route table with an attachment.
##------------------------------------------------------------------------------
resource "aws_ram_resource_share_accepter" "receiver_accept" {
count = var.enable && var.aws_ram_resource_share_accepter ? 1 : 0
share_arn = var.resource_share_arn
}
}

20 changes: 20 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ output "resource_share_arn" {
output "tags" {
value = module.labels.tags
description = "A mapping of tags to assign to the resource."
}

output "ec2_transit_gateway_arn" {
value = try(aws_ec2_transit_gateway.main[0].arn, "")
description = "EC2 Transit Gateway Amazon Resource Name (ARN)"
}

output "ec2_transit_gateway_route_table_id" {
value = try(aws_ec2_transit_gateway_route_table.this[0].id, "")
description = "EC2 Transit Gateway Route Table identifier"
}

output "ec2_transit_gateway_vpc_attachment" {
value = aws_ec2_transit_gateway_vpc_attachment.main
description = "Map of EC2 Transit Gateway VPC Attachment attributes"
}

output "ram_resource_share_id" {
value = try(aws_ram_resource_share.main[0].id, "")
description = "The Amazon Resource Name (ARN) of the resource share"
}
1 change: 0 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ variable "repository" {
type = string
default = "https://github.com/clouddrove/terraform-aws-transit-gateway"
description = "Terraform current module repo"

}

variable "environment" {
Expand Down
4 changes: 2 additions & 2 deletions _example/different-account/versions.tf → versions.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Terraform version
terraform {
required_version = ">= 1.3.6"
required_version = ">= 1.4.6"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.48.0"
version = ">= 5.1.0"
}
}
}

0 comments on commit 2f5d76a

Please sign in to comment.