This post is based on reference to Use instance metadata to manage your EC2 instance – Amazon Elastic Compute Cloud
Metadata is a powerful tool for AWS users. It allows users to make query of data describing EC2 instances, and making a self reference API call.
By default any linux AMI will have curl build in, hence using of metadata will be simplified.
One of the best test case is automating input into shell script that will requires a lot of user prompt is automating or at least make configuration of setting up openvpn using AWS Lightsail easier.
!#/bin/bash
sudo chmod 777 ./openvpn-install.sh
sudo ./openvpn-install.sh << INPUT
y
1
1
11
n
n
client
1
INPUT
sudo cp /root/client.ovpn /home/ubuntu
sudo chmod 777 /home/ubuntu/client.ovpn
Above shell script will cause the openvpn-install.sh to fail in AWS, as the script does not provide public and local ip of the instance.
Below script are including the AWS Metadata
!#/bin/bash
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
PUBLICIP=`curl -X GET "http://169.254.169.254/latest/meta-data/public-ipv4" -H "X-aws-ec2-metadata-token: $TOKEN"`
LOCALIP=`curl -X GET "http://169.254.169.254/latest/meta-data/local-ipv4" -H "X-aws-ec2-metadata-token: $TOKEN"`
sudo chmod 777 ./openvpn-install.sh
sudo ./openvpn-install.sh << INPUT
$LOCALIP
$PUBLICIP
y
1
1
11
n
n
client
1
INPUT
sudo cp /root/client.ovpn /home/ubuntu
sudo chmod 777 /home/ubuntu/client.ovpn
By adding the metadata the ovpn file will be populated with correct IP.
AWS Metadata allow automation to be made simpler by running scripts that requires self-reference metadata to configure newly booted up EC2 instance(s).