EC2 user data
30 January, 2021
BackText in the user data is a script that is run when the instance is booted.
Here's a very simple way to install an Apache web server and load an index.html file into the instance.
sudo yum update -y
sudo yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
sudo bash -c 'echo "Hello EC2" > /var/www/html/index.html'
And to verify that the html file is loaded, run this:
cat /var/html/index.html
A simple mistake I made while spinning up my EC2 instance was at the user data part. I configure the EC2, VPC, subnets, IGW, route tables, and security groups correctly but still couldn't access the public IP. That was because I copied a line of code from a tutorial which didn't work here. Learning up a bit of Linux cmd helped solve the script too.
Alternative
#!/bin/bash
sudo su
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<html><h1>Hello EC2</h1><html>" >> /var/www/html/index.html
Back