#!/bin/bash # ------------------------------------------------------------------ # AWS EC2 User Data Script for Load Balancer Testing (English Version) # ------------------------------------------------------------------ # 1. Update system and install Apache (Amazon Linux 2/2023) yum update -y yum install -y httpd # 2. Start Apache and enable it on boot systemctl start httpd systemctl enable httpd # 3. Create IMDSv2 Token (For secure metadata access) TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") # 4. Fetch Instance Information (Private IP, ID, AZ) INSTANCE_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/local-ipv4) INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/instance-id) AZ=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/placement/availability-zone) # 5. Generate web page (index.html) cat < /var/www/html/index.html Load Balancer Test

EC2 Instance Info

Instance ID: ${INSTANCE_ID}

Private IP: ${INSTANCE_IP}

Availability Zone: ${AZ}

EOF # 6. Set permissions chmod 644 /var/www/html/index.html [cite: 1, 2, 3, 4, 5, 6, 7, 8, 9]