This repository was archived by the owner on Jul 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +66
-42
lines changed Expand file tree Collapse file tree 7 files changed +66
-42
lines changed Original file line number Diff line number Diff line change 1
1
.terraform /
2
2
.terraform. *
3
3
* .tfstate
4
+ * .tfstate.backup
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -85,12 +85,31 @@ resource "aws_launch_template" "this" {
85
85
delete_on_termination = true
86
86
}
87
87
88
- user_data = base64encode (
89
- templatefile (" ${ path . module } /data/init.sh" , {
90
- eni_id = aws_network_interface.this.id
91
- extra_user_data = var.extra_user_data
88
+ user_data = base64encode (join (" \n " , [
89
+ " #cloud-config" ,
90
+ yamlencode ({
91
+ # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
92
+ write_files : [
93
+ {
94
+ path : " /opt/nat/runonce.sh" ,
95
+ content : templatefile (" ${ path . module } /runonce.sh" , { eni_id = aws_network_interface.this.id }),
96
+ permissions : " 0755" ,
97
+ },
98
+ {
99
+ path : " /opt/nat/snat.sh" ,
100
+ content : file (" ${ path . module } /snat.sh" ),
101
+ permissions : " 0755" ,
102
+ },
103
+ {
104
+ path : " /etc/systemd/system/snat.service" ,
105
+ content : file (" ${ path . module } /snat.service" ),
106
+ },
107
+ ],
108
+ runcmd : [
109
+ [" /opt/nat/runonce.sh" ],
110
+ ],
92
111
})
93
- )
112
+ ]) )
94
113
95
114
description = " Launch template for NAT instance ${ var . name } "
96
115
tags = {
Original file line number Diff line number Diff line change
1
+ #! /bin/bash -x
2
+
3
+ # attach the ENI
4
+ aws ec2 attach-network-interface \
5
+ --region " $( /opt/aws/bin/ec2-metadata -z | sed ' s/placement: \(.*\).$/\1/' ) " \
6
+ --instance-id " $( /opt/aws/bin/ec2-metadata -i | cut -d' ' -f2) " \
7
+ --device-index 1 \
8
+ --network-interface-id " ${eni_id} "
9
+
10
+ # start SNAT
11
+ systemctl enable snat
12
+ systemctl start snat
Original file line number Diff line number Diff line change
1
+ [Unit]
2
+ Description = SNAT via ENI eth1
3
+
4
+ [Service]
5
+ ExecStart = /opt/nat/snat.sh
6
+ Type = oneshot
7
+
8
+ [Install]
9
+ WantedBy = multi-user.target
Original file line number Diff line number Diff line change
1
+ #! /bin/bash -x
2
+
3
+ # wait for eth1
4
+ while ! ip link show dev eth1; do
5
+ sleep 1
6
+ done
7
+
8
+ # Enable IP forwarding and NAT
9
+ sysctl -q -w net.ipv4.ip_forward=1
10
+ sysctl -q -w net.ipv4.conf.eth1.send_redirects=0
11
+ iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
12
+
13
+ # Switch the default route to eth1
14
+ ip route del default dev eth0
15
+
16
+ # Waiting for network connection
17
+ curl --retry 10 http://www.example.com
18
+
19
+ # Restart the SSM agent
20
+ systemctl restart amazon-ssm-agent.service
Original file line number Diff line number Diff line change @@ -30,12 +30,6 @@ variable "private_route_table_ids" {
30
30
default = []
31
31
}
32
32
33
- variable "extra_user_data" {
34
- description = " Extra script to run in the NAT instance"
35
- type = string
36
- default = " "
37
- }
38
-
39
33
variable "image_id" {
40
34
description = " AMI of the NAT instance. Default to the latest Amazon Linux 2"
41
35
type = string
You can’t perform that action at this time.
0 commit comments