aws.zsh 2.98 KB
Newer Older
1
# switch aws profile
2
awsp() {
3 4 5 6 7 8 9 10 11 12 13 14 15
    # Usage:
    # awsp for interactive prompt
    # awsp $profile to automatically select first match
    if [ -z "$1" ]; then
        export AWS_PROFILE=$(grep profile ${HOME}/.aws/config \
            | awk '{print $2}' | sed 's,],,g' \
            | fzf --layout reverse --height=10% --border)
    else
        export AWS_PROFILE=$(grep profile ${HOME}/.aws/config \
            | awk '{print $2}' | sed 's,],,g' \
            | grep -i "$1" \
            | head)
    fi
Justin Garrison's avatar
Justin Garrison committed
16 17 18 19 20
    export AWS_DEFAULT_PROFILE=$AWS_PROFILE
}

# export aws region
awsr() {
21 22 23 24 25 26 27 28 29 30 31 32 33
    # Usage:
    # awsr for interactive prompt
    # awsr $region to automatically select first match
    if [ -z "$1" ]; then
        export AWS_REGION=$(printf 'us-east-1\tN. Virginia\nus-east-2\tOhio\nus-west-1\tN. California\nus-west-2\tOregon\neu-west-1\tIreland\neu-central-1\tFrankfurt' \
            | fzf --layout reverse --height=10% --border \
            | awk -F'\t' '{print $1}')
    else
        export AWS_REGION=$(printf 'us-east-1\tN. Virginia\nus-east-2\tOhio\nus-west-1\tN. California\nus-west-2\tOregon\neu-west-1\tIreland\neu-central-1\tFrankfurt' \
            | grep -i "$1" \
            | awk -F'\t' '{print $1}' \
            | head)
    fi
Justin Garrison's avatar
Justin Garrison committed
34
    export AWS_DEFAULT_REGION=$AWS_REGION
35
}
Justin Garrison's avatar
Justin Garrison committed
36

37 38 39 40 41 42 43 44 45
# copy aws account number
awsa() {
    grep -B1 --no-group-separator iam ${HOME}/.aws/credentials \
        | cut -d: -f5 | tr ']\n' ' ' | tr '[' '\n' \
        | column -t | fzf --height 10% --border \
        | tee /dev/tty | awk '{print $2}' \
        | xclip -selection clipboard
}

46 47 48 49 50 51 52 53
# unset AWS_*
awsu() {
    unset AWS_PROFILE \
        AWS_DEFAULT_PROFILE \
        AWS_REGION \
        AWS_DEFAULT_REGION
}

Justin Garrison's avatar
Justin Garrison committed
54
# print current AWS_* variables
55
awse() {
Justin Garrison's avatar
Justin Garrison committed
56 57 58
    env | grep --color=never AWS_
}

59 60 61 62
aws-asg-counts() {
  aws autoscaling  describe-auto-scaling-groups --auto-scaling-group-names $1 | jq .AutoScalingGroups[].Instances[].AvailabilityZone | sort | uniq -c
}

Justin Garrison's avatar
Justin Garrison committed
63
# SSH to instance via instance ID
64
sshi() {
65
    ssh [email protected]$(aws ec2 describe-instances --instance-ids ${1:-$INSTANCEID} --query 'Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddress' --output text)
Justin Garrison's avatar
Justin Garrison committed
66
}
67 68 69 70
# Start session manager to instance via ID
ssmi() {
    aws ssm start-session --target ${1:-INSTANCEID}
}
71 72 73 74 75 76 77
# Start session manager to instance via private IP
ssmip() {
    aws ssm start-session --target $(aws ec2 describe-instances --filter Name=private-ip-address,Values=${1} --query 'Reservations[].Instances[].[InstanceId]' --output text)
}
# Give me a dev vm in aws
# exports $INSTANCEID for ssmi and sshi functions
alias aws-devm='eval $(create-instance)'
78 79

# Mark instance unhealthy
Justin Garrison's avatar
Justin Garrison committed
80 81 82 83
alias aws-set-unhealthy='aws autoscaling set-instance-health --health-status Unhealthy --instance-id '

# Get tags on an instance
function aws-get-tags() {
84
    aws ec2 describe-tags \
Justin Garrison's avatar
Justin Garrison committed
85 86 87
        --filters "Name=resource-id,Values=${1:$INSTANCE-ID}" "Name=key,Values=${TAG_PREFIX}*"  \
        --output text
}
88 89

alias aws-get-console='aws ec2 get-console-output --output text --latest --instance-id '