1
1
resource "aws_security_group" "this" {
2
2
name_prefix = var. name
3
3
vpc_id = var. vpc_id
4
+ description = " Security group for NAT instance ${ var . name } "
5
+ tags = {
6
+ Name = " nat-instance-${ var . name } "
7
+ }
4
8
}
5
9
6
- resource "aws_security_group_rule" "this_egress " {
10
+ resource "aws_security_group_rule" "egress " {
7
11
security_group_id = aws_security_group. this . id
8
12
type = " egress"
9
13
cidr_blocks = [" 0.0.0.0/0" ]
@@ -12,7 +16,7 @@ resource "aws_security_group_rule" "this_egress" {
12
16
protocol = " tcp"
13
17
}
14
18
15
- resource "aws_security_group_rule" "this_ingress " {
19
+ resource "aws_security_group_rule" "ingress " {
16
20
security_group_id = aws_security_group. this . id
17
21
type = " ingress"
18
22
cidr_blocks = var. private_subnets_cidr_blocks
@@ -21,15 +25,64 @@ resource "aws_security_group_rule" "this_ingress" {
21
25
protocol = " tcp"
22
26
}
23
27
28
+ resource "aws_security_group_rule" "ssh" {
29
+ count = var. key_name == " " ? 0 : 1
30
+ security_group_id = aws_security_group. this . id
31
+ type = " ingress"
32
+ cidr_blocks = [" 0.0.0.0/0" ]
33
+ from_port = 22
34
+ to_port = 22
35
+ protocol = " tcp"
36
+ }
37
+
38
+ resource "aws_network_interface" "this" {
39
+ security_groups = [aws_security_group . this . id ]
40
+ subnet_id = var. public_subnet
41
+ source_dest_check = false
42
+ description = " ENI for NAT instance ${ var . name } "
43
+ tags = {
44
+ Name = " nat-instance-${ var . name } "
45
+ }
46
+ }
47
+
48
+ resource "aws_eip" "this" {
49
+ network_interface = aws_network_interface. this . id
50
+ tags = {
51
+ Name = " nat-instance-${ var . name } "
52
+ }
53
+ }
54
+
55
+ resource "aws_route" "this" {
56
+ count = length (var. private_route_table_ids )
57
+ route_table_id = var. private_route_table_ids [count . index ]
58
+ destination_cidr_block = " 0.0.0.0/0"
59
+ network_interface_id = aws_network_interface. this . id
60
+ }
61
+
24
62
resource "aws_launch_template" "this" {
25
63
name_prefix = var. name
26
64
image_id = var. image_id
65
+ key_name = var. key_name
66
+
27
67
iam_instance_profile {
28
68
arn = aws_iam_instance_profile. this . arn
29
69
}
70
+
30
71
network_interfaces {
31
72
associate_public_ip_address = true
32
73
security_groups = [aws_security_group . this . id ]
74
+ delete_on_termination = true
75
+ }
76
+
77
+ user_data = base64encode (
78
+ templatefile (" ${ path . module } /data/init.sh" , {
79
+ eni_id = aws_network_interface.this.id
80
+ })
81
+ )
82
+
83
+ description = " Launch template for NAT instance ${ var . name } "
84
+ tags = {
85
+ Name = " nat-instance-${ var . name } "
33
86
}
34
87
}
35
88
@@ -38,7 +91,7 @@ resource "aws_autoscaling_group" "this" {
38
91
desired_capacity = 1
39
92
min_size = 1
40
93
max_size = 1
41
- vpc_zone_identifier = var. public_subnets
94
+ vpc_zone_identifier = [ var . public_subnet ]
42
95
43
96
mixed_instances_policy {
44
97
instances_distribution {
@@ -58,6 +111,12 @@ resource "aws_autoscaling_group" "this" {
58
111
}
59
112
}
60
113
114
+ tag {
115
+ key = " Name"
116
+ value = " nat-instance-${ var . name } "
117
+ propagate_at_launch = true
118
+ }
119
+
61
120
lifecycle {
62
121
create_before_destroy = true
63
122
}
@@ -86,7 +145,26 @@ resource "aws_iam_role" "this" {
86
145
EOF
87
146
}
88
147
89
- resource "aws_iam_role_policy_attachment" "this_ssm " {
148
+ resource "aws_iam_role_policy_attachment" "ssm " {
90
149
policy_arn = " arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
91
150
role = aws_iam_role. this . name
92
151
}
152
+
153
+ resource "aws_iam_role_policy" "eni" {
154
+ role = aws_iam_role. this . name
155
+ name_prefix = var. name
156
+ policy = << EOF
157
+ {
158
+ "Version": "2012-10-17",
159
+ "Statement": [
160
+ {
161
+ "Effect": "Allow",
162
+ "Action": [
163
+ "ec2:AttachNetworkInterface"
164
+ ],
165
+ "Resource": "*"
166
+ }
167
+ ]
168
+ }
169
+ EOF
170
+ }
0 commit comments