Automating AWS CLI with Bash

In the past I have use AWS BOTO3, the python api, to automate some of my aws tasks. I had an existing file with a list of AWS CLI calls I used to doing a dev to production migration.  I used to run these manually but I want to create a foundation that I could the migration with a single script.

The biggest challenge with the CLI is how to use parameters in the aws CLI call and how to store the output from an api call so it can be used in a subsequent step. Turns out its relatively straight forward.  The post will show you how if you are on a Mac or Linux. You could use Powershell also but this post is specific to Bash.

Saving the output of a CLI call to a variable

instance=$(aws ec2 describe-instances \
–filters “Name=instance-state-name,Values=running” \
–query ‘Reservations[*].Instances[*].[InstanceId]’ \
–output text)

Here we are saving the output of the describe-instance call to a variable called instance. Note the syntax with the ().  Also I filter to only running instances and the Query option limits what’s returned to  just instanced

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »