#!/bin/bash # 로그 남기기 exec > >(tee /var/log/userdata.log | logger -t userdata -s 2>/dev/console) 2>&1 # 패키지 업데이트 yum update -y yum install -y httpd # IMDSv2 토큰 발급 TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \ -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") # EC2 호스트명 가져오기 HOSTNAME=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \ http://169.254.169.254/latest/meta-data/hostname) # 웹 페이지 생성 (호스트명 표시) cat < /var/www/html/index.html AWS EC2 Web Server

Welcome to AWS EC2

Hostname: $HOSTNAME

EOF # 웹서버 시작 systemctl start httpd systemctl enable httpd