|
| 1 | +data "aws_vpn_gateway" "this" { |
| 2 | + id = var.vgw_id |
| 3 | +} |
| 4 | + |
| 5 | +data "aws_ec2_transit_gateway" "this" { |
| 6 | + id = var.transit_gateway_id |
| 7 | +} |
| 8 | + |
| 9 | +module "psk1" { |
| 10 | + source = "git::https://github.com/rhythmictech/terraform-aws-secretsmanager-random-secret?ref=v1.1.1" |
| 11 | + name = "${var.account_name}/${var.customer_name}-psk1-secret" |
| 12 | + create_secret = var.use_secrets_manager |
| 13 | + length = 40 |
| 14 | + min_special = 3 |
| 15 | + override_special = "._" |
| 16 | + pass_version = var.tunnel1_psk_version |
| 17 | + use_special = true |
| 18 | + tags = var.tags |
| 19 | +} |
| 20 | + |
| 21 | +module "psk2" { |
| 22 | + source = "git::https://github.com/rhythmictech/terraform-aws-secretsmanager-random-secret?ref=v1.1.1" |
| 23 | + name = "${var.account_name}/${var.customer_name}-psk2-secret" |
| 24 | + create_secret = var.use_secrets_manager |
| 25 | + length = 40 |
| 26 | + min_special = 3 |
| 27 | + override_special = "._" |
| 28 | + pass_version = var.tunnel2_psk_version |
| 29 | + tags = var.tags |
| 30 | + use_special = true |
| 31 | +} |
| 32 | + |
| 33 | +locals { |
| 34 | + |
| 35 | + tags_with_name = merge(var.tags, { |
| 36 | + "Name" = "${var.account_name}<=>${var.customer_name}" |
| 37 | + } |
| 38 | + ) |
| 39 | + tunnel1_psk = var.use_secrets_manager ? module.psk1.secret : var.tunnel1_psk |
| 40 | + tunnel2_psk = var.use_secrets_manager ? module.psk2.secret : var.tunnel2_psk |
| 41 | + |
| 42 | + # compute aws bgp asn |
| 43 | + amazon_bgp_asn = var.vgw_id == null ? data.aws_ec2_transit_gateway.this.amazon_side_asn : data.aws_vpn_gateway.this.amazon_side_asn |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_customer_gateway" "this" { |
| 47 | + bgp_asn = var.customer_bgp_asn |
| 48 | + ip_address = var.customer_ip_address |
| 49 | + tags = local.tags_with_name |
| 50 | + type = var.customer_gateway_type |
| 51 | +} |
| 52 | + |
| 53 | +resource "aws_vpn_connection" "this" { |
| 54 | + customer_gateway_id = aws_customer_gateway.this.id |
| 55 | + tags = local.tags_with_name |
| 56 | + transit_gateway_id = var.transit_gateway_id |
| 57 | + tunnel1_inside_cidr = var.tunnel1_inside_cidr |
| 58 | + tunnel1_preshared_key = local.tunnel1_psk |
| 59 | + tunnel2_inside_cidr = var.tunnel2_inside_cidr |
| 60 | + tunnel2_preshared_key = local.tunnel2_psk |
| 61 | + type = aws_customer_gateway.this.type |
| 62 | + vpn_gateway_id = var.vgw_id |
| 63 | +} |
| 64 | + |
| 65 | +resource "local_file" "this" { |
| 66 | + count = var.generate_fortigate_config ? 1 : 0 |
| 67 | + content = templatefile("${path.module}/fortigate_config.txt.tpl", |
| 68 | + { |
| 69 | + account_name = var.account_name |
| 70 | + amazon_bgp_asn = local.amazon_bgp_asn |
| 71 | + customer_bgp_asn = var.customer_bgp_asn |
| 72 | + customer_ip_address = var.customer_ip_address |
| 73 | + customer_name = var.customer_name |
| 74 | + shortname = substr(var.account_name, 0, 14) |
| 75 | + tunnel1_address = aws_vpn_connection.this.tunnel1_address |
| 76 | + tunnel1_inside_address_amazon = aws_vpn_connection.this.tunnel1_vgw_inside_address |
| 77 | + tunnel1_inside_address_customer = aws_vpn_connection.this.tunnel1_cgw_inside_address |
| 78 | + tunnel2_address = aws_vpn_connection.this.tunnel2_address |
| 79 | + tunnel2_inside_address_amazon = aws_vpn_connection.this.tunnel2_vgw_inside_address |
| 80 | + tunnel2_inside_address_customer = aws_vpn_connection.this.tunnel2_cgw_inside_address |
| 81 | + wan_interface = var.wan_interface |
| 82 | + } |
| 83 | + ) |
| 84 | + filename = "${var.account_name}-${var.customer_name}-fortigate_config.txt" |
| 85 | +} |
0 commit comments