11resource "aws_security_group" "this" {
22 name_prefix = var. name
33 vpc_id = var. vpc_id
4+ description = " Security group for NAT instance ${ var . name } "
5+ tags = {
6+ Name = " nat-instance-${ var . name } "
7+ }
48}
59
6- resource "aws_security_group_rule" "this_egress " {
10+ resource "aws_security_group_rule" "egress " {
711 security_group_id = aws_security_group. this . id
812 type = " egress"
913 cidr_blocks = [" 0.0.0.0/0" ]
@@ -12,7 +16,7 @@ resource "aws_security_group_rule" "this_egress" {
1216 protocol = " tcp"
1317}
1418
15- resource "aws_security_group_rule" "this_ingress " {
19+ resource "aws_security_group_rule" "ingress " {
1620 security_group_id = aws_security_group. this . id
1721 type = " ingress"
1822 cidr_blocks = var. private_subnets_cidr_blocks
@@ -21,15 +25,64 @@ resource "aws_security_group_rule" "this_ingress" {
2125 protocol = " tcp"
2226}
2327
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+
2462resource "aws_launch_template" "this" {
2563 name_prefix = var. name
2664 image_id = var. image_id
65+ key_name = var. key_name
66+
2767 iam_instance_profile {
2868 arn = aws_iam_instance_profile. this . arn
2969 }
70+
3071 network_interfaces {
3172 associate_public_ip_address = true
3273 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 } "
3386 }
3487}
3588
@@ -38,7 +91,7 @@ resource "aws_autoscaling_group" "this" {
3891 desired_capacity = 1
3992 min_size = 1
4093 max_size = 1
41- vpc_zone_identifier = var. public_subnets
94+ vpc_zone_identifier = [ var . public_subnet ]
4295
4396 mixed_instances_policy {
4497 instances_distribution {
@@ -58,6 +111,12 @@ resource "aws_autoscaling_group" "this" {
58111 }
59112 }
60113
114+ tag {
115+ key = " Name"
116+ value = " nat-instance-${ var . name } "
117+ propagate_at_launch = true
118+ }
119+
61120 lifecycle {
62121 create_before_destroy = true
63122 }
@@ -86,7 +145,26 @@ resource "aws_iam_role" "this" {
86145EOF
87146}
88147
89- resource "aws_iam_role_policy_attachment" "this_ssm " {
148+ resource "aws_iam_role_policy_attachment" "ssm " {
90149 policy_arn = " arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
91150 role = aws_iam_role. this . name
92151}
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