#!/bin/bash # ------------------------------------------------------------------ # AWS EC2 User Data Script for Load Balancer Testing # ------------------------------------------------------------------ # 1. 시스템 업데이트 및 Apache 설치 (Amazon Linux 2/2023 기준) yum update -y yum install -y httpd # 2. Apache 실행 및 부팅 시 자동 시작 설정 systemctl start httpd systemctl enable httpd # 3. IMDSv2 토큰 생성 (보안 메타데이터 접근용) TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") # 4. 인스턴스 정보(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. 웹 페이지(index.html) 파일 생성 cat < /var/www/html/index.html Load Balancer Test

EC2 Instance Info

인스턴스 ID: ${INSTANCE_ID}

프라이빗 IP: ${INSTANCE_IP}

가용 영역(AZ): ${AZ}

EOF # 6. 권한 설정 chmod 644 /var/www/html/index.html