#!/bin/bash # 1. 모든 출력을 로그 파일에 기록 (디버깅용) exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 echo "--- Starting Dynamic Configuration Setup ---" # 2. 패키지 설치 및 업데이트 yum update -y yum install -y httpd # 3. IMDSv2 토큰 생성 및 현재 IP 가져오기 (매 부팅 시 갱신됨) TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") CURRENT_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/local-ipv4) # 4. S3 퍼블릭 URL에서 설정 가져오기 # 아래 URL을 실제 본인의 S3 파일 주소로 변경하세요. S3_URL="https://server-ip1/config.txt" S3_CONTENT=$(curl -s $S3_URL) # 만약 curl 실패 시를 대비해 기본값 설정 if [ -z "$S3_CONTENT" ]; then S3_CONTENT="S3 파일을 읽을 수 없습니다. (URL 또는 퍼블릭 권한 확인 필요)" fi # 5. 동적 index.html 작성 cat < /var/www/html/index.html EC2 Status Page
EC2 Instance Info

Config from S3:

$S3_CONTENT
$CURRENT_IP

로드 밸런서 연결 상태: 정상(Healthy)

EOF # 6. 서비스 시작 및 활성화 systemctl enable httpd systemctl restart httpd echo "--- Setup Completed Successfully ---"