Reputation: 47
Hi Guys I need to do a script which copies the ips from an aws region AMIs that I have running, after copying this IPs place them into a text file inside. If the Instances are turned off, that IP would get removed, and the text file would change real-time, on it's own automatically, I need this to run across all regions, so any Instance that I have "X" AMI running with, the script would find it, copy its IP keep it if it's running and remove it from the file if they switch to shutdown mode.
stack the IPs in a text like
55.555.555.55
66.123.545.54
.....
.....
real-time.
I've never really used aws cli and I know this is possible to do.
Upvotes: 0
Views: 85
Reputation: 34297
This command uses a aws cli "describe-instances" command with a filter for only instances that are running.
This outputs a lot of data including the "PublicIp" field. The sed
command strips out just the ip address from that line and the uniq
removes duplicates
aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' | sed -n 's/^.*"PublicIp": "\([0-9\.]*\)\",/\1/p'| uniq
See http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html for details on the aws cli describe instances command, including other filters you might want to apply
Upvotes: 1
Reputation: 3404
Use the describe-instances command in the AWS CLI. All the information that you need (AMI, instance state, IP address) will be included in the response to that command. Note that you will have to run describe-instances
once for each region. (Set the --region
flag when running the CLI to set the region.)
You can parse the JSON output of the CLI however you want then write the information you want to the text file.
Upvotes: 1