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

Time:02-21

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.

CodePudding user response:

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.

CodePudding user response:

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/

  • Related