Skip to content

Instantly share code, notes, and snippets.

@madeddie
Last active March 23, 2016 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeddie/715a62eaa0f3fcee5bee to your computer and use it in GitHub Desktop.
Save madeddie/715a62eaa0f3fcee5bee to your computer and use it in GitHub Desktop.
Some useful bash functions for dealing with AWS/EC2/OpsWorks stacks and instances
# AWS functions
function instances() {
if [[ $1 == '--ip' || $1 == '--ips' ]]; then
filter='.[PrivateIpAddress]'
output='text'
shift
elif [[ $1 == '--id' || $1 == '--ids' ]]; then
filter='.[InstanceId]'
output='text'
shift
else
# shellcheck disable=SC2016
filter='.{Id: InstanceId, State: State.Name, PrivateIp: PrivateIpAddress, Name: (Tags[?Key==`Name`].Value | [0])}'
output='table'
fi
stack=${1:-edwin}
profile=${2:-default}
region=${3:-eu-central-1}
aws --profile="$profile" --region="$region" --output "$output" ec2 describe-instances \
--filter \
"Name=tag-key,Values=*:stack*" \
"Name=tag-value,Values=${stack}" \
"Name=instance-state-name,Values=pending,running" \
--query "Reservations[].Instances[]${filter}"
}
export -f instances
function owstacks() {
profile=${1:-default}
region=${2:-eu-central-1}
aws --profile="$profile" --region=us-east-1 opsworks describe-stacks --output table \
--query 'sort_by(Stacks, &Name)[].{Name: Name, StackId: StackId, SourceUrl: CustomCookbooksSource.Url}'
}
export -f owstacks
function cfstacks() {
profile=${1:-default}
region=${2:-eu-central-1}
aws --profile="$profile" --region="$region" cloudformation describe-stacks --output table \
--query 'sort_by(Stacks, &StackName)[].{Name: StackName, Status: StackStatus}'
}
export -f cfstacks
function awsintip() {
instanceids=${1:-XXX}
profile=${2:-default}
region=${3:-eu-central-1}
aws --output=text --profile="$profile" --region="$region" ec2 describe-instances --instance-ids "$instanceids" \
--query 'Reservations[].Instances[].PrivateIpAddress'
}
export -f awsintip
function awsextip() {
instanceids=${1:-XXX}
profile=${2:-default}
region=${3:-eu-central-1}
aws --output=text --profile="$profile" --region="$region" ec2 describe-instances --instance-ids "$instanceids" \
--query 'Reservations[].Instances[].PublicIpAddress'
}
export -f awsextip
function awssh() {
IFS='@' read -r -a parts <<< "$1"
if [[ ${#parts[*]} -gt 1 ]]; then
username="${parts[0]}@"
IFS='.' read -r -a hostparts <<< "${parts[1]}"
else
IFS='.' read -r -a hostparts <<< "${parts[0]}"
fi
if [[ ${#hostparts[*]} != 4 ]]; then
echo "Can't parse hostname $1"
return 1
fi
domain=$(IFS=. ; echo "${hostparts[*]:2}")
if [ "$domain" == "appdev.io" ]; then
profile="default"
elif [ "$domain" == "lgi.io" ]; then
profile="prod"
else
echo "Domain $domain not recognized"
return 1
fi
hostname=${hostparts[0]}
stack=${hostparts[1]}
region=${2:-eu-central-1}
ip=$(aws --output text --profile="$profile" --region="$region" ec2 describe-instances \
--filter \
Name=tag:opsworks:stack,Values="$stack" \
Name=instance-state-name,Values=pending,running \
Name=tag:opsworks:instance,Values="$hostname" \
--query 'Reservations[0].Instances[0].PrivateIpAddress')
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
ssh "$username$ip"
else
echo "IP: '$ip' not valid. (If None then no instance was found, check region maybe)"
return 1
fi
}
export -f awssh
@madeddie
Copy link
Author

These need and expect awscli to be installed.
They also expect a profile 'default' and a profile 'prod' to be configured for awscli.
awssh is hardcoded for use with appdev.io and lgi.io domains. appdev.io being development and corresponding to the 'default' profile and lgi.io being production and using the 'prod' profile.

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