User data By default, the user data scripts are run one time when you launch the instance. To run the user data scripts every time you reboot or start the instance, add <persist>true</persist>
.
http://169.254.169.254/latest/user-data
Linux
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello from $(hostname -f)" > /var/www/html/index.html
Windows
D drive association and IIS install and landing page
<powershell>
Initialize-Disk -Number 1
New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter D
Format-Volume –DriveLetter D -FileSystem NTFS
Install-WindowsFeature -name Web-Server -IncludeManagementTools
New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Value "WELCOME WEB SERVER 1 !!!" -Force
</powershell>
EIP association
<powershell>
$token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "21600"} -Method PUT –Uri http://169.254.169.254/latest/api/token
$INSTANCE_ID = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Method GET -Uri http://169.254.169.254/latest/meta-data/instance-id
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id eipalloc-0912c72a68ca32ca1 --allow-reassociation
</powershell>
Metadata
http://169.254.169.254/latest/meta-data/
curl http://169.254.169.254/latest/meta-data/
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/