A new instance is an empty Amazon Linux. Setting it up by hand means connecting and typing the same commands every time. User data is a script you give at launch; the instance runs it once, at first boot.
text
#!/bin/bash
dnf install -y httpd ← install the Apache web server
echo '<h1>Monster Market</h1>' > /var/www/html/index.html ← the home page
systemctl enable --now httpd ← start it, now and at every bootdnf installs software on Amazon Linux; httpd is the Apache web server. It serves the files in /var/www/html on port 80.
Two more parts of run-instances make a web server in one go:
New concept
--user-data and --security-group-ids
--user-data file:// hands the script to the instance. --security-group-ids puts the instance in your security group from the start.
Usageaws ec2 run-instances --image-id AMI --instance-type type --security-group-ids sg-id --user-data file://script --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=name}]'
Example
aws ec2 run-instances --image-id ami-0c7217cdde317cfec --instance-type t3.micro --security-group-ids sg-0123456789abcdef0 --user-data file://setup.sh --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test-box}]'
The script takes a few seconds after the instance is running. Then the page answers, if the security group lets port 80 in.