Skip to content

Instantly share code, notes, and snippets.

@salekseev
Created March 22, 2017 16:19
Show Gist options
  • Save salekseev/c8f5d0130ac180fd404343aa49654338 to your computer and use it in GitHub Desktop.
Save salekseev/c8f5d0130ac180fd404343aa49654338 to your computer and use it in GitHub Desktop.
Script to find private IP addresses of all EC2 instances participating in the CloudFormation stack
#!/bin/bash
command -v aws >/dev/null 2>&1 || { echo >&2 "I require awscli but it's not installed. Aborting."; exit 1; }
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed. Aborting."; exit 1; }
# Find local instance_id
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
# Find cloudformation stack-id
STACK_ID=$(aws ec2 describe-instances --output text --instance-ids $INSTANCE_ID | grep -e 'TAGS\saws:cloudformation:stack-id' | awk '{print $3}')
aws ec2 describe-instances --output text --filters "Name=tag:aws:cloudformation:stack-id,Values=${STACK_ID}" | grep PRIVATEIPADDRESSES | awk '{print $4}'
@salekseev
Copy link
Author

Run this script from one of the EC2 instances participating in the stack to discover other stack members.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment