forked from jetbrains-infra/terraform-aws-nat-instance
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvariables.tf
More file actions
115 lines (93 loc) · 2.83 KB
/
Copy pathvariables.tf
File metadata and controls
115 lines (93 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
variable "name" {
type = string
description = "General name for resources"
default = "nat-instance"
}
variable "environment" {
type = string
description = "General name for environment"
default = ""
}
variable "team_name" {
type = string
description = "General name for team"
default = ""
}
variable "instance_type" {
type = string
description = "NAT instance type (default to ARM-based)"
default = "t4g.nano"
}
variable "ami" {
description = "AMI for nat instance"
type = string
default = ""
}
variable "aws_iam_instance_profile" {
type = string
description = "Name of IAM instance profile to assign to EC2 instance"
default = ""
}
variable "tags" {
description = "A map of tags to add to all resources"
type = map(any)
default = {}
}
variable "public_subnet_ids" {
type = list(string)
description = "List of public subnets ids in which we will create NAT instances"
}
variable "private_route_table_ids" {
type = list(string)
description = "List of private route table IDs for which we will create NAT rules"
}
variable "security_groups" {
type = list(string)
description = "List of security groups created outside of module to attach"
default = []
}
#####################
data "aws_ami" "nat" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-*"]
}
filter {
name = "architecture"
values = ["arm64"]
}
}
data "aws_subnet" "nat_single" {
id = element(var.public_subnet_ids, 0)
}
data "aws_vpc" "vpc" {
id = data.aws_subnet.nat_single.vpc_id
}
data "aws_region" "current" {}
locals {
ami = var.ami == "" ? data.aws_ami.nat.id : var.ami
iam_instance_profile = var.aws_iam_instance_profile == "" ? aws_iam_instance_profile.ssm_profile[0].name : var.aws_iam_instance_profile
# map of public subnets received in var.public_subnet_ids list
public_subnets_map = [
for key, subnet in var.public_subnet_ids : {
subnet_id = subnet
}
]
# map of private route tables ids received in var.private_route_table_ids list
private_rtables_map = [
for key, route in var.private_route_table_ids : {
route_id = route
}
]
equal_length = length(var.public_subnet_ids) != length(var.private_route_table_ids) ? min(length(var.private_route_table_ids), length(var.public_subnet_ids)) : length(var.public_subnet_ids)
# final construct where we merge route table ids and subnets in one map blocks to use in for_each loops
rtable_subnets_map = [
for route, subnet in zipmap(slice(local.private_rtables_map.*.route_id, 0, local.equal_length), slice(local.public_subnets_map.*.subnet_id, 0, local.equal_length)) :
{
route_id = route
subnet_id = subnet
}
]
}