Created
February 2, 2026 16:53
-
-
Save nmagee/b2d5bfea625d0523b233bb0420503ee6 to your computer and use it in GitHub Desktop.
EC2 bootrapping material
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| apt-get update | |
| apt-get install -y nginx jq | |
| # IMDSv2 token-based access (recommended) | |
| TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") | |
| # Get instance metadata using the token | |
| INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id) | |
| AZ=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone) | |
| REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region) | |
| INSTANCE_TYPE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-type) | |
| LOCAL_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4) | |
| PUBLIC_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4) | |
| AMI_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/ami-id) | |
| # Create custom page with all metadata | |
| cat > /var/www/html/index.html <<EOF | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Instance: $INSTANCE_ID</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; margin: 40px; } | |
| table { border-collapse: collapse; } | |
| td, th { border: 1px solid #ddd; padding: 8px; text-align: left; } | |
| th { background-color: #4CAF50; color: white; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>EC2 Instance Information</h1> | |
| <table> | |
| <tr><th>Property</th><th>Value</th></tr> | |
| <tr><td>Instance ID</td><td>$INSTANCE_ID</td></tr> | |
| <tr><td>Instance Type</td><td>$INSTANCE_TYPE</td></tr> | |
| <tr><td>Region</td><td>$REGION</td></tr> | |
| <tr><td>Availability Zone</td><td>$AZ</td></tr> | |
| <tr><td>AMI ID</td><td>$AMI_ID</td></tr> | |
| <tr><td>Local IP</td><td>$LOCAL_IP</td></tr> | |
| <tr><td>Public IP</td><td>$PUBLIC_IP</td></tr> | |
| <tr><td>Bootstrapped</td><td>$(date)</td></tr> | |
| </table> | |
| </body> | |
| </html> | |
| EOF | |
| # Set hostname based on instance ID for easy identification | |
| hostnamectl set-hostname "ec2-$INSTANCE_ID" | |
| systemctl enable nginx | |
| systemctl start nginx | |
| # Log completion with metadata | |
| echo "Bootstrap completed for $INSTANCE_ID in $AZ at $(date)" > /var/log/bootstrap-complete.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #cloud-config | |
| package_update: true | |
| packages: | |
| - nginx | |
| runcmd: | |
| - service nginx start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #cloud-config | |
| package_reboot_if_required: true | |
| package_update: true | |
| package_upgrade: true | |
| packages: | |
| - jq | |
| - git | |
| - python3 | |
| - python3-pip | |
| - snap: | |
| - [aws-cli, --classic] | |
| runcmd: | |
| - python3 -m pip install awscli |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment