DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > AWS ZSH Helper

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.

James Carr user avatar by
James Carr
·
Apr. 05, 16 · Cloud Zone · Tutorial
Like (2)
Save
Tweet
5.13K Views

Join the DZone community and get the full member experience.

Join For Free

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! 

AWS

Published at DZone with permission of James Carr, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is URL Rewriting? | Java Servlets
  • How to Submit a Post to DZone
  • The Most Popular Kubernetes Alternatives and Competitors
  • Don't Underestimate Documentation

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo