I have a script for EC2 instance User Data. I verified after EC2 instance is READY
#!/bin/bash
yum update -y
yum install nginx
service nginx start
echo '<h4> Welcome to AWS</h4>' >> /usr/share/nginx/html/index.html
If I login into EC2 instance and if i try to execute the above commands one-by-one and then we can see the results. But not executing same script when i use inside User Data while EC2 start up. Please someone can suggest me on this? did i miss any setting/configurations ?
Logs
Cloud-init v. 22.2.2 running 'init' at Sun, 11 Jun 2023 12:12:40 +0000. Up 8.80 seconds.
ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: | Device | Up | Address | Mask | Scope | Hw-Address |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: | enX0 | True | 10.80.2.254 | 255.255.255.0 | global | 0a:56:b9:5f:1f:30 |
ci-info: | enX0 | True | fe80::856:b9ff:fe5f:1f30/64 | . | link | 0a:56:b9:5f:1f:30 |
ci-info: | lo | True | 127.0.0.1 | 255.0.0.0 | host | . |
ci-info: | lo | True | ::1/128 | . | host | . |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: +++++++++++++++++++++++++++++Route IPv4 info+++++++++++++++++++++++++++++
ci-info: +-------+-------------+-----------+-----------------+-----------+-------+
ci-info: | Route | Destination | Gateway | Genmask | Interface | Flags |
ci-info: +-------+-------------+-----------+-----------------+-----------+-------+
ci-info: | 0 | 0.0.0.0 | 10.80.2.1 | 0.0.0.0 | enX0 | UG |
ci-info: | 1 | 10.80.0.2 | 10.80.2.1 | 255.255.255.255 | enX0 | UGH |
ci-info: | 2 | 10.80.2.0 | 0.0.0.0 | 255.255.255.0 | enX0 | U |
ci-info: | 3 | 10.80.2.1 | 0.0.0.0 | 255.255.255.255 | enX0 | UH |
ci-info: +-------+-------------+-----------+-----------------+-----------+-------+
ci-info: +++++++++++++++++++Route IPv6 info+++++++++++++++++++
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: | Route | Destination | Gateway | Interface | Flags |
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: | 1 | fe80::/64 | :: | enX0 | U |
ci-info: | 3 | local | :: | enX0 | U |
ci-info: | 4 | multicast | :: | enX0 | U |
ci-info: +-------+-------------+---------+-----------+-------+
Generating public/private ed25519 key pair.
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub
The key fingerprint is:
SHA256:E6rTH/qkWkm+1MnibB19PEAF/fCiUHoGNlD5umOU9Cs [email protected]
The key's randomart image is:
+--[ED25519 256]--+
| .o...+. |
| = o o |
| ..B + |
| .+.= . o |
| o.SO + . |
| = +=o+ + |
| o Bo*o o . |
| *.BE.. |
| .o*ooo |
+----[SHA256]-----+
Generating public/private ecdsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub
The key fingerprint is:
SHA256:OeTafm90Z9JyrM+pLB9ETVTC/wLgXvA5seUizr7LCC4 [email protected]
The key's randomart image is:
+---[ECDSA 256]---+
| .ooo|
| o . =. |
| .. + B o |
| o .o X . .|
| S+ o = o.|
| o .+ o = B|
| o .. . o O |
| E. o oo.o o..|
| .. o.+=++.oo|
+----[SHA256]-----+
Cloud-init v. 22.2.2 running 'modules:config' at Sun, 11 Jun 2023 12:12:44 +0000. Up 12.79 seconds.
Cloud-init v. 22.2.2 running 'modules:final' at Sun, 11 Jun 2023 12:12:46 +0000. Up 14.11 seconds.
Amazon Linux 2023 repository 24 MB/s | 14 MB 00:00
Amazon Linux 2023 Kernel Livepatch repository 495 kB/s | 156 kB 00:00
Dependencies resolved.
Nothing to do.
Complete!
Last metadata expiration check: 0:00:02 ago on Sun Jun 11 12:12:56 2023.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.24.0-1.amzn2023.0.1 amazonlinux 32 k
Installing dependencies:
generic-logos-httpd noarch 18.0.0-12.amzn2023.0.3 amazonlinux 19 k
gperftools-libs x86_64 2.9.1-1.amzn2023.0.2 amazonlinux 309 k
libunwind x86_64 1.4.0-5.amzn2023.0.2 amazonlinux 66 k
nginx-core x86_64 1:1.24.0-1.amzn2023.0.1 amazonlinux 586 k
nginx-filesystem noarch 1:1.24.0-1.amzn2023.0.1 amazonlinux 9.0 k
nginx-mimetypes noarch 2.1.49-3.amzn2023.0.3 amazonlinux 21 k
Transaction Summary
================================================================================
Install 7 Packages
Total download size: 1.0 M
Installed size: 3.4 M
Is this ok [y/N]: Operation aborted.
Redirecting to /bin/systemctl start nginx.service
Failed to start nginx.service: Unit nginx.service not found.
/var/lib/cloud/instance/scripts/part-001: line 5: /usr/share/nginx/html/index.html: No such file or directory
2023-06-11 12:12:58,743 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2023-06-11 12:12:58,744 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3.9/site-packages/cloudinit/config/cc_scripts_user.py'>) failed
Cloud-init v. 22.2.2 finished at Sun, 11 Jun 2023 12:12:59 +0000. Datasource DataSourceEc2. Up 27.18 seconds
2
Answers
You have the
-y
flag already in youryum update
command, so that it doesn’t stop and give you the[y/N]
prompt. You just need to do the same thing with youryum install
command:If you want to see what exactly went wrong with the execution of a User Script on an EC2, SSH into the instance and navigate to /var/log. Then cat cloud-init-output.log, which will show you all the messages generated while the script was being executed.