Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AWS ZSH Helper

DZone's Guide to

AWS ZSH Helper

This week’s function is probably only useful if you’re into AWS and use the awscli tool to interact with it from the command line. Using the awscli command direction can be quite verbose so some nice shortcuts are useful.

· Cloud Zone ·
Free Resource

Insight into the right steps to take for migrating workloads to public cloud and successfully reducing cost as a result. Read the Guide.

I’m going to start up a new recurring weekly post: Friday Functions! While some of it will aim to share my large inventory of ZSH functions I’ve acquired over the years, I’ll also be finding new additions if I run out of material. So, it also serves to help me learn more!

This week’s function is probably only useful if you’re into AWS and use the awscli tool to interact with it from the command line. Using the awscli command direction can be quite verbose so some nice shortcuts are useful. I actually learned of this handy function from Kris’s awesome collection of ZSH configuration and made a few small adaptions to it.

function aws-instances-describe() {
    zparseopts -D -E -A opts -- o: t: s:
    output=${opts[-o]:-"table"}
    tag_name=${opts[-t]:-"Name"}
    state=${opts[-s]:-"running"}

    name=${1}
    query=(
        "Reservations[].Instances[]"
        ".{"
        "Name             : Tags[?Key == \`Name\`].Value | [0],"
        "State            : State.Name,"
        "LaunchTime       : LaunchTime,"
        "InstanceId       : InstanceId,"
        "PrivateIpAddress : PrivateIpAddress,"
        "PublicIpAddress  : PublicIpAddress,"
        "ImageId          : ImageId,"
        "InstanceType     : InstanceType"
        "}"
    )

    aws --output ${output} \
        ec2 describe-instances \
        --filters "Name=tag:${tag_name},Values=*${name}*" "Name=instance-state-name,Values=${state}" \
        --query "${query}"
}

This is pretty useful. If you want to find all instances with http in the name, you just run aws-instances-describe http.

Screen Shot 2016-03-31 at 6.14.42 PM

Or, if you want to look for instances by a specific tag you can use the `-t` switch. For example, to find all instances with the worker_emailrole tag we can just run aws-instance-describe -t role worker_email. You can add -s to change the filter to include the running state and, like the actual call, you can include multiple instances. So, if you wanted to find all stopped instances with the taskhistory role, you’d run aws-instance-describe -t role taskhistory -s stopped. The function sets this to default to running instances only since that’s what I’m looking for 99% of the time… looking for stopped or terminated instances is definitely the exception.

Hope this was interesting. Ideas, thoughts, comments, or criticism are all welcome in the comments! Let me know what you think! 

TrueSight Cloud Cost Control provides visibility and control over multi-cloud costs including AWS, Azure, Google Cloud, and others.

Topics:
amazon ,cli ,zsh ,function

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}