Scenario
A simple landing page must be served from an EC2 instance. The instance should be reachable over HTTP and associated with its own security group.
Task
Create a security group named web-sg that allows TCP port 80 (HTTP) from any IPv4 address.
Launch an EC2 instance with the following configuration:
| Property |
Value |
| Tag Name |
web-1 |
| Instance type |
t2.micro |
| Security group |
web-sg |
Select any available AMI in the current region (you may use aws ec2 describe-images to find one).
Ensure the instance automatically creates the following file during startup:
/var/www/html/index.html with the content: Hello from web-1
Note: You can use either the AWS Management Console or AWS CLI to complete this task.
Step 1: Create the security group
SG_ID=$(aws ec2 create-security-group \
--group-name web-sg \
--description "Web server security group" \
--query 'GroupId' --output text)
Step 2: Add inbound HTTP rule
aws ec2 authorize-security-group-ingress \
--group-id "$SG_ID" \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0
Allows HTTP traffic from anywhere to reach the web server.
Step 3: Create the user data script
cat > userdata.sh << 'EOF'
#!/bin/bash
mkdir -p /var/www/html
echo "Hello from web-1" > /var/www/html/index.html
EOF
This script runs on first boot and creates the landing page.
Step 4: Find an available AMI
AMI_ID=$(aws ec2 describe-images --filters "Name=architecture,Values=x86_64" --query 'Images[0].ImageId' --output text)
echo $AMI_ID
Step 5: Launch the instance
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t2.micro \
--security-group-ids "$SG_ID" \
--user-data file://userdata.sh \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'
Launches a t2.micro instance with the security group, user data script, and the Name tag.
Step 6: Verify the instance
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=web-1" \
--query 'Reservations[0].Instances[0].{Id:InstanceId,Type:InstanceType,State:State.Name,SG:SecurityGroups[0].GroupName}'
Should show the instance as running with t2.micro type and web-sg security group.