I have tried to run the script below on an Ubuntu22 machine, and I have also tried to run it on a Amazon Linux machine. The instances both launch with no issues stated, however when I SSH into the instance, all the programs that I would expect to be installed are not there, I have tested all the commands manually to see if they work and they do.
The bash script:
#!/bin/bash
sudo apt update -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip -y
unzip awscliv2.zip
sudo ./aws/install
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o
/usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg]
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee
/etc/apt/sources.list.d/hashicorp.list
sudo apt install terraform -y
mkdir keys
ssh-keygen -t rsa -b 4096 -f /home/ubuntu/keys/clientkey -N ""
ssh-keygen -t rsa -b 4096 -f /home/ubuntu/keys/serverkey -N ""
clientpubkey=$(cat /home/ubuntu/keys/clientkey.pub)
serverpubkey=$(cat /home/ubuntu/keys/serverkey.pub)
aws s3 cp s3://musicappscripts --recursive
The cloud-init.log:
> 2024-02-17 09:39:05,379 - util.py[DEBUG]: Cloud-init v. 23.3.3-
0ubuntu0~22.04.1 running 'init-local' at Sat, 17 Feb 2024 09:39:05
+0000. Up 7.85 seconds.
2024-02-17 09:39:05,379 - main.py[DEBUG]: No kernel command line
url found.
2024-02-17 09:39:05,379 - main.py[DEBUG]: Closing stdin.
2024-02-17 09:39:05,390 - util.py[DEBUG]: Writing to
/var/log/cloud-init.log - ab: [640] 0 bytes
2024-02-17 09:39:05,391 - util.py[DEBUG]: Changing the ownership
of /var/log/cloud-init.log to 104:4
2024-02-17 09:39:05,391 - util.py[DEBUG]: Writing to
/var/lib/cloud/data/python-version - wb: [644] 4 bytes
2024-02-17 09:39:05,392 - util.py[DEBUG]: Attempting to remove
/var/lib/cloud/instance/boot-finished
2024-02-17 09:39:05,392 - handlers.py[DEBUG]: start: init-
local/check-cache: attempting to read from cache [check]
2024-02-17 09:39:05,392 - util.py[DEBUG]: Reading from
/var/lib/cloud/instance/obj.pkl (quiet=False)
2024-02-17 09:39:05,392 - stages.py[DEBUG]: no cache found
2024-02-17 09:39:05,392 - handlers.py[DEBUG]: finish: init-
local/check-cache: SUCCESS: no cache found
2024-02-17 09:39:05,392 - util.py[DEBUG]: Attempting to remove
/var/lib/cloud/instance
2024-02-17 09:39:05,396 - stages.py[DEBUG]: Using distro class
<class 'cloudinit.distros.ubuntu.Distro'>
2024-02-17 09:39:05,396 - __init__.py[DEBUG]: Looking for data
source in: ['Ec2', 'None'], via packages ['', 'cloudinit.sources']
that matches dependencies ['FILESYSTEM']
2024-02-17 09:39:05,423 - __init__.py[DEBUG]: Searching for local
data source in: ['DataSourceEc2Local']
2024-02-17 09:39:05,423 - handlers.py[DEBUG]: start: init-
local/search-Ec2Local: searching for local data from
DataSourceEc2Local
2024-02-17 09:39:05,424 - __init__.py[DEBUG]: Seeing if we can get
any data from
<class'cloudinit.sources.DataSourceEc2.DataSourceEc2Local'>
2024-02-17 09:39:05,424 - __init__.py[DEBUG]: Update datasource
metadata and network config due to events: boot-new-instance
2024-02-17 09:39:05,424 - util.py[DEBUG]: Reading from
/sys/hypervisor/uuid (quiet=False)
2024-02-17 09:39:05,424 - util.py[DEBUG]: Read 37 bytes from
/sys/hypervisor/uuid
2024-02-17 09:39:05,424 - dmi.py[DEBUG]: querying dmi data
/sys/class/dmi/id/product_serial
2024-02-17 09:39:05,424 - dmi.py[DEBUG]: querying dmi data
/sys/class/dmi/id/chassis_asset_tag
2024-02-17 09:39:05,424 - dmi.py[DEBUG]: querying dmi data
/sys/class/dmi/id/sys_vendor
2024-02-17 09:39:05,424 - dmi.py[DEBUG]: querying dmi data
/sys/class/dmi/id/product_name
2024-02-17 09:39:05,425 - __init__.py[DEBUG]: Machine is
configured to run on single datasource DataSourceEc2Local.
2024-02-17 09:39:05,425 - DataSourceEc2.py[DEBUG]: strict_mode:
warn, cloud_name=aws cloud_platform=ec2
2024-02-17 09:39:05,425 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/name_assign_type (quiet=False)
2024-02-17 09:39:05,425 - util.py[DEBUG]: Read 2 bytes from
/sys/class/net/eth0/name_assign_type
2024-02-17 09:39:05,425 - __init__.py[DEBUG]: Found unstable nic
names: ['eth0']; calling udevadm settle
2024-02-17 09:39:05,425 - subp.py[DEBUG]: Running command
['udevadm', 'settle'] with allowed return codes [0]
(shell=False,capture=True)
2024-02-17 09:39:05,445 - util.py[DEBUG]: Waiting for udev events
to settle took 0.020 seconds
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/address (quiet=False)
2024-02-17 09:39:05,446 - util.py[DEBUG]: Read 18 bytes from
/sys/class/net/eth0/address
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/device/device (quiet=False)
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/lo/address (quiet=False)
2024-02-17 09:39:05,446 - util.py[DEBUG]: Read 18 bytes from
/sys/class/net/lo/address
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/lo/device/device (quiet=False)
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/carrier (quiet=False)
2024-02-17 09:39:05,446 - __init__.py[DEBUG]: Interface has no
carrier: eth0
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/dormant (quiet=False)
2024-02-17 09:39:05,446 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/operstate (quiet=False)
2024-02-17 09:39:05,447 - util.py[DEBUG]: Read 5 bytes from
/sys/class/net/eth0/operstate
2024-02-17 09:39:05,447 - dhcp.py[DEBUG]: DHCP client selected:
dhclient
2024-02-17 09:39:05,447 - dhcp.py[DEBUG]: Performing a dhcp
discovery on eth0
2024-02-17 09:39:05,447 - subp.py[DEBUG]: Running command ['ip',
'link', 'set', 'dev', 'eth0', 'up'] with allowed return codes [0]
(shell=False, capture=True)
2024-02-17 09:39:05,504 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/type (quiet=False)
2024-02-17 09:39:05,504 - util.py[DEBUG]: Read 2 bytes from
/sys/class/net/eth0/type
2024-02-17 09:39:05,504 - subp.py[DEBUG]: Running command
['/usr/sbin/dhclient', '-1', '-v', '-lf', '/run/dhclient.lease',
'-pf', '/run/dhclient.pid', '-sf', '/bin/true', 'eth0'] with
allowed return codes [0] (shell=False, capture=True)
2024-02-17 09:39:05,616 - util.py[DEBUG]: All files appeared after
0 seconds: ['/run/dhclient.pid', '/run/dhclient.lease']
2024-02-17 09:39:05,616 - util.py[DEBUG]: Reading from
/run/dhclient.pid (quiet=False)
2024-02-17 09:39:05,617 - util.py[DEBUG]: Read 4 bytes from
/run/dhclient.pid
2024-02-17 09:39:05,617 - util.py[DEBUG]: Reading from
/proc/331/stat (quiet=True)
2024-02-17 09:39:05,617 - util.py[DEBUG]: Read 303 bytes from
/proc/331/stat
2024-02-17 09:39:05,617 - dhcp.py[DEBUG]: killing dhclient with
pid=331
2024-02-17 09:39:05,617 - util.py[DEBUG]: Reading from
/run/dhclient.lease (quiet=False)
2024-02-17 09:39:05,618 - util.py[DEBUG]: Read 516 bytes from
/run/dhclient.lease
2024-02-17 09:39:05,618 - ephemeral.py[DEBUG]: Received dhcp lease
on eth0 for 172.31.21.174/255.255.240.0
2024-02-17 09:39:05,618 - ephemeral.py[DEBUG]: Attempting setup of
ephemeral network on eth0 with 172.31.21.174/20 brd 172.31.31.255
2024-02-17 09:39:05,618 - subp.py[DEBUG]: Running command ['ip',
'-family', 'inet', 'addr', 'add', '172.31.21.174/20', 'broadcast',
'172.31.31.255', 'dev', 'eth0'] with allowed return codes [0]
(shell=False, capture=True)
2024-02-17 09:39:05,620 - subp.py[DEBUG]: Running command ['ip',
'-family', 'inet', 'link', 'set', 'dev', 'eth0', 'up'] with
allowed return codes [0] (shell=False, capture=True)
2024-02-17 09:39:05,622 - subp.py[DEBUG]: Running command ['ip',
'route', 'show', '0.0.0.0/0'] with allowed return codes [0]
(shell=False, capture=True)
2024-02-17 09:39:05,624 - subp.py[DEBUG]: Running command ['ip',
'-4', 'route', 'add', '172.31.16.1', 'dev', 'eth0', 'src',
'172.31.21.174'] with allowed return codes [0] (shell=False,
capture=True)
2024-02-17 09:39:05,626 - subp.py[DEBUG]: Running command ['ip',
'-4', 'route', 'add', 'default', 'via', '172.31.16.1', 'dev',
'eth0'] with allowed return codes [0] (shell=False, capture=True)
2024-02-17 09:39:05,627 - util.py[DEBUG]: Reading from
/sys/class/net/eth0/operstate (quiet=False)
2024-02-17 09:39:05,627 - util.py[DEBUG]: Read 3 bytes from
/sys/class/net/eth0/operstate
2024-02-17 09:39:05,629 - util.py[DEBUG]: Resolving URL:
http://169.254.169.254 took 0.001 seconds
2024-02-17 09:39:05,629 - util.py[DEBUG]: Resolving URL:
http://[fd00:ec2::254] took 0.000 seconds
2024-02-17 09:39:05,629 - util.py[DEBUG]: Resolving URL:
http://instance-data.:8773 took 0.000 seconds
2024-02-17 09:39:05,629 - DataSourceEc2.py[DEBUG]: Removed the
following from metadata urls: ['http://instance-data.:8773']
2024-02-17 09:39:05,629 - DataSourceEc2.py[DEBUG]: Fetching Ec2
IMDSv2 API Token
@@@
2
Answers
User data scripts are run as the root user and therefore should not use
sudo
. This is most likely where yours fails.This will fail on Amazon Linux even without
sudo
as the distribution like CentOS and others do not use theapt
but theyum
package manager instead. Make sure you test it like you did initially on Ubuntu or other distributions that useapt
like Debian.Hope this helps.
step 1 ; look for the file permission,check shebang line added in the file
step 2 ; manually install all the package
step 3 ; run the file in debugg mode
step 4 ; make sure u have added ssh public key in the git repo