#!/bin/bash # # author: Antonio Feijao UK (https://www.antoniofeijao.com/) # # 2023-07-26 - added support from IMDSv2 (based on AWS docs https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html) # 2023-07-26 - minor tweaks to the html and css # 2021-12-16 - initial version # # Main script sourced from https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html # # Webspage building code done by Antonio Feijao UK (https://www.antoniofeijao.com/) # echo " To use this file with your EC2' user-data, simply add the below 2 lines into the ec2 user-data #!/bin/bash curl -H 'Cache-Control: no-cache, no-store' https://raw.githubusercontent.com/AntonioFeijaoUK/aws-ec2-userdata-samples/master/sample01-hello-world-region-az.sh | bash " yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd usermod -a -G apache ec2-user chown -R ec2-user:apache /var/www chmod 2775 /var/www find /var/www -type d -exec chmod 2775 {} \; find /var/www -type f -exec chmod 0664 {} \; ## update to use token on IMDSv2 TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` INSTANCE_INFO=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/dynamic/instance-identity/document) INSTANCE_ID=$(printf "$INSTANCE_INFO" | grep instanceId | awk '{print $3}' | sed s/','//g | sed s/'"'//g) INSTANCE_AZ=$(printf "$INSTANCE_INFO" | grep availabilityZone | awk '{print $3}' | sed s/','//g | sed s/'"'//g) INSTANCE_TYPE=$(printf "$INSTANCE_INFO" | grep instanceType | awk '{print $3}' | sed s/','//g | sed s/'"'//g) INSTANCE_REGION=$(printf "$INSTANCE_INFO" | grep region | awk '{print $3}' | sed s/','//g | sed s/'"'//g) cat < /var/www/html/index.html Hello World sample page created using EC2 userdata, by Antonio Feijao UK (https://www.antoniofeijao.com/)

Hello World sample page created using EC2 userdata

by Antonio Feijao UK

Instance ID ${INSTANCE_ID}
Instance Type ${INSTANCE_TYPE}
Instance Region ${INSTANCE_REGION}
Availability Zone ${INSTANCE_AZ}
EOF