AWS EC2 instance showing Apache test page instead of webpage - amazon-web-services

I am new to AWS and recently I was trying to access a webpage using an EC2 instance. I uploaded the webpage using the following bash commands in the User Data field while creating the instance:
#!/bin/bash
yum update -y
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo '<html><h1>Sample Webpage</h1></html>' > /var/www/html/index.html
I noticed that the public IP address of the instance directed me to the Apache Web Server's test page when the names of the security group and the instance were different, but to the desired webpage when the names were same.
Could anyone please explain why is it so?

There is nothing wrong with your user_data. It works exactly as expected. Whatever you are checking, does not involve this code, thus please double check your instances and their user data.

Related

AWS EC2 User Data not working (Tried Installing and starting httpd via User Data)

The Following is my EC2 User Data:
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
In Security Group SSH 22 Port and HTTP 80 Port is Open.
Yet when I try accessing http://public_ip_of_instance the HTTP Apache page doesn't load.
Also, on the Instance Apache is not installed when I checked sudo systemctl status httpd.
I then manually tried it on the EC2 Server and it worked. Then I removed it through yum remove as I wanted to see whether User Data works.
I stopped the Instance and started again but I observed that the User Data Script doesn't work as I am unable to access http page through browser and also on Instance http is not installed.
Where is the actual issue? Some months back this same thing worked on another instance I remember.
Your user data is correct. Whatever is happening with your website is not due to the user data code that you provided.
There could be many reasons it does not work. Public IP of the instance has changed, as always happens when you stop/start the instance. Instance may have per-existing software that clashes with httpd.
Here's some general advice on running UserData once or each startup.
Short answer as John mentioned in the comments EC2's only run the UserData (aka Bootstrap) script once on initalization.
The user data Bash/Powershell is Infrastructure-As-Code. You deploy the script and it installs and configures the machine.
This causes confusion with everyone starting AWS. When you think about it though it doesn't make sense to run the UserData script each time when the PCs already been configured.
What people do often instead is make "Golden Images" (aka Amazon Machine Images - AMI's) of pre-setup EC2s, typically for PCs that take long time to install/configure. The beauty of this is you can setup AutoScaleGroups to use the images which saves any long installation during a scale up event.
Pro Tip: When developing an UserData script run through and test it manually on the EC2. Trust me its far quicker than troubleshooting unattended EC2 UserData errors.
Long answer: you can run the UserData on each boot of the machine using Mime multi-part file. A mime multi-part file allows your script to override how frequently user data is run in the cloud-init package.
https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
For all those who will run into this problem, first of all check the log with the command:
sudo cat /var/log/cloud-init-output.log
then if you notice connection errors to the various repositories, the reason is because you don't have an internet connection. However, if once inside your EC2 you manage to launch the update and install commands, then the reason why they fail in the UserData is because your EC2 takes a few seconds to get the Internet connection and executes the commands before having it. So to solve this problem, just add this command after #!/bin/bash
#!/bin/bash
until ping -c1 8.8.8.8 &>/dev/null; do :; done
sudo yum update -y
...
This will prevent your EC2 from executing commands before an internet connection is established

Allow a bash script to run at boot in AWS Centos 7 instance

I need to create AWS CentOS 7 instance images for a customer, and need it to automatically send the ip and instance id to our AWS server every time the instance boots. For example, this is the very basic test version of the script I need to run:
#!/bin/bash
$serverIP=""
curl "https://$serverIP"/myphp.php?id='sentid'&ip='sentip'"
If the script is run directly, it works fine and is received by the server and processed there. But I can't get it to run at boot. I cannot put the script in the "User Data" directly due to security concerns as the customer can then see it easily, it needs to be in a script in the filesystem of the image.
I've tried several things that work fine on a physical Linux server, but not on AWS. I know profile.d runs every time someone logs in but over-sending like that is fine.
/etc/profile.d/myscript.sh
This stops the AWS instance from booting. Even just
#!/bin/bash/
echo "hello world"
prevents it from booting. The instance starts, but when you go to ssh into it you get 'Network Error: connection timed out', which is the standard error if you put a wrong ip in, or upset it by leaving a service like httpd enabled.
However, a blank bash script with just #!/bin/bash will allow the instance to start. Removing the script via user data usually makes it boot, sometimes it just dies.
The first thing I tried was crontab. I did:
crontab -e
#reboot /var/ook/myscript.sh
systemctl enable crond.service
But the instance wouldn't start. So I put "systemctl disable crond.service" in the User Data and one booted, but another still stayed dead. Myscript.sh was just another echo "doob" >> file which worked fine when run directly.
I tried putting in /etc/systemd/system/my-startup.service:
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/var/ook/writedood.sh
[Install]
WantedBy=multi-user.target
then:
systemctl enable my-startup.service
But this did nothing. My script "writedood.sh" was just echo "doob" >> ./file.txt ensuring file.txt was chmod 777. At least it didn't prevent the instance from starting.
To give context, an instance won't start if httpd is left enabled on shutdown, but will if you disable it in User Data.
I wanted to have a go at putting something in init.d but I'm not sure how to simply tell it to run a script once in the background, and given the plethora of success I've had so far with the instance not restarting, I'm not holding out much hope that that would work.
Thanks in advance!
EDIT::: I realised that sometimes AWS EC2 Instances Console is causing the problem where I can't ssh in after stopping and starting. It blanks the public ipv4 address when I click stop, but when I start, it puts the old address up and hangs. If I refresh the page, or uncheck/check the instance; the ip changes to the new address. This has caused much consternation.
Crontab worked if I placed the scripts and output file in different folders. It's very finicky; any errors, such as it not being able to write to the output file, and the instance won't start. I put startscript.sh in /usr/local/src, and output.out to /tmp/ to ensure there were no permissions problems, and now the instance starts and runs the script on boot.
I then realised that sometimes AWS EC2 Instances Console is causing the problem where I can't ssh in after stopping and starting. It blanks the public ipv4 address when I click stop, but when I start, it puts the old address up and hangs. If I refresh the page, or uncheck/check the instance; the ip changes to the new address. This has caused much consternation.

Lightsail Bitnami reboot resets the machine_hostname setting

I have a running AWS Lightsail instance, with bitname and Wordpress running on it.
I have assigned it a static IP address.
After setting it up I have opened the console and configured the machine_hostname like this:
sudo ./bnconfig --machine_hostname <domain>
After this, the domain now works, and everything operates as expected..... Until the instance reboots. At that time the domain is lost, and I have to perform this again.
Not sure how to prevent this from happening on a reboot?
Bitnami Engineer here,
If you run the bnconfig tool to configure the domain name in the application, you need to rename the tool so it's not run again when the instance is started/rebooted.
sudo ./bnconfig --machine_hostname <domain>
sudo mv ./bnconfig ./bnconfig.disabled
https://docs.bitnami.com/aws/faq/configuration/configure-custom-domain/
You can also update the WordPress' configuration with your domain (/opt/bitnami/apps/wordpress/htdocs/wp-config.php)
define('WP_SITEURL', 'http://DOMAIN/');
define('WP_HOME', 'http://DOMAIN/');
You can learn more about that here
https://docs.bitnami.com/aws/apps/wordpress/administration/configure-domain/
This worked for me:
sudo hostnamectl set-hostname <domain>

How to join AWS EC2 ubuntu instance to simple directory

in AWS I am trying to join ubuntu EC2 instance to AWS simple directory. I used steps described here to manually join instance.
First i Connected to instance using PuTTY
In step 4 i executed the following command
$ sudo apt-get -y install sssd realmd krb5-user samba-common packagekit adcli
As mentioned in the article, i was presented with several pop-up configuration screens and i left the fields in these screens blank.
then i tried to join the instance to the directory with the following command.
$ sudo realm join -U user#INTERNAL.MYCOMPANY.COM internal.mycompany.com --verbose
Since instance has Ubuntu 16.04, i entered the domain name portion of the username with all capital letters
However i am getting the No such realm found error
I am not linux expert, infact this is the first time i am using linux. All other instances in our network are windows.
Can anyone guide me what could be the issue here?
found it
To configure an EC2 instance running Linux to use static DNS server entries, use a text editor such as vim to edit the file /etc/dhcp/dhclient.conf and add the following line to the end of the file:
supersede domain-name-servers xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx;
Where xxx.xxx.xxx.xxx is the IP address of a DNS server that you want the instance to use
https://aws.amazon.com/premiumsupport/knowledge-center/ec2-static-dns-ubuntu-debian/
and then try to join using
$ sudo realm join -U user#INTERNAL.MYCOMPANY.COM internal.mycompany.com --verbose

Cassandra stop working on AWS (ubuntu server)

I have configure cassandra-cluster locally and it works fine, following the same steps I configure cassandra-cluster on AWS on a ubuntu-server instance.
It works fine, but if I stop cassandra service from one node:
sudo service cassandra stop
And then I start it, this node never connect to the cluster again.
And it fails throwing the next error:
* could not access pidfile for Cassandra
My cassandra version is 3.7, so if I access to /etc/init.d/cassandra, so the cmd_patt is the next:
CMD_PATT="Dcassandra-pidfile=.*cassandra.pid"
Cassandra version: 3.7
Host: ubuntu server 14.04 (AWS).
You have to remove /var/run/cassandra folder hence it has wrong permissions:
sudo rm -rf /var/run/cassandra
Or you can fix permissions manually:
sudo chmod 750 /var/run/cassandra
Then start Cassandra as service:
sudo service cassandra start
Some explanations
Instructions of file permissions you can find here.
It is safe to delete that folder because it recreates with right permissions and content. But do not delete it once it works correct. It may result in loss of data or incorrect behavior.
chmod 750 decrypts as rwxr-x--- permissions. It allows read-write-execute to the user, read-execute to the group and nothing to others. For Cassandra, it is enough to set permissions so.
Stop cassandra service:
sudo service cassandra stop
Remove the default dataset:
sudo rm -rf /var/lib/cassandra/data/system/*
Start cassandra service:
sudo service cassandra start