@@ -3,6 +3,19 @@ exec > >(tee /var/log/user-data.log | logger -t user-data -s 2>/dev/console) 2>&
33
44echo " Starting user-data script..."
55
6+ echo " Determining package manager..."
7+
8+ # Work with both dnf and apt-get.
9+ if command -v apt-get > /dev/null 2>&1 ; then
10+ PKG_MANAGER=apt-get
11+ INSTALL_CMD=" apt-get install -y"
12+ else
13+ PKG_MANAGER=dnf
14+ INSTALL_CMD=" dnf install -y"
15+ fi
16+
17+ echo " Detected the following package manager: $PKG_MANAGER ."
18+
619echo " Enabling IP forwarding..."
720echo ' net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
821echo ' net.ipv6.conf.all.forwarding = 1' >> /etc/sysctl.conf
@@ -54,26 +67,61 @@ retry_command() {
5467 return $exit_code
5568}
5669
57- # Install CloudWatch Agent
58- echo " Installing CloudWatch Agent..."
59- retry_command " dnf install -y amazon-cloudwatch-agent" 5
70+ # Function to install necessary packages per distro.
71+ install_packages () {
72+ case " $PKG_MANAGER " in
73+ apt-get)
74+ # Update package cache.
75+ echo " Updating package cache..."
76+ retry_command " $PKG_MANAGER update" 5
77+
78+ # Install utilities.
79+ echo " Installing utilities..."
80+ retry_command " $INSTALL_CMD curl wget" 5
81+
82+ # Install CloudWatch Agent.
83+ echo " Installing CloudWatch Agent..."
84+ distro=$( grep ' ^ID=' /etc/os-release | cut -d' =' -f2)
85+ arch=$( uname -m)
86+ case " $arch " in
87+ x86_64)
88+ arch=amd64
89+ ;;
90+ * )
91+ arch=arm64
92+ ;;
93+ esac
94+ retry_command " wget https://amazoncloudwatch-agent.s3.amazonaws.com/$distro /$arch /latest/amazon-cloudwatch-agent.deb" 5
95+ retry_command " dpkg -i -E ./amazon-cloudwatch-agent.deb" 5
96+ ;;
97+ * )
98+ # Install utilities.
99+ echo " Installing utilities..."
100+ retry_command " $INSTALL_CMD dnf-utils" 5
101+
102+ # Install CloudWatch Agent.
103+ echo " Installing CloudWatch Agent..."
104+ retry_command " $INSTALL_CMD amazon-cloudwatch-agent" 5
105+ ;;
106+ esac
107+ }
108+
109+ # Install necessary packages.
110+ echo " Installing necessary packages..."
111+ install_packages
112+
113+ # Start the CloudWatch Agent.
60114amazon-cloudwatch-agent-ctl -a start -m ec2
61115
62116# Install Tailscale
63117echo " Installing Tailscale..."
64- retry_command " dnf install -y dnf-utils" 5
65- retry_command " dnf config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo" 5
66- retry_command " dnf install -y tailscale" 5
118+ retry_command " curl -fsSL https://tailscale.com/install.sh | sh" 5
67119
68120%{ if tailscaled_extra_flags_enabled == true }
69121echo " Exporting FLAGS to /etc/default/tailscaled..."
70122sed -i " s|^FLAGS=.*|FLAGS=\" ${tailscaled_extra_flags} \" |" /etc/default/tailscaled
71123%{ endif }
72124
73- # Setup Tailscale
74- echo " Enabling and starting tailscaled service..."
75- systemctl enable --now tailscaled
76-
77125echo " Waiting for tailscaled to initialize..."
78126sleep 5
79127
0 commit comments