This repository contains a series of bash functions that make working with the AWS CLI tool easier.
Clone this repository or download the source code, then source the
aws-cli-helpers
file in your bash script:
#!/usr/bin/env bash
. /path/to/aws-cli-helpers
Pull requests are welcome. Please follow Google's Shell Style Guide. Make a best effort to write tests. We use the Bash Automated Testing System (bats).
Many AWS commands accept the --filters
option. This option is equally a pain
in the ass as it is powerful. The functions in this library make it easier to
create filters. Assuming that the output of the filter functions in the examples
are captured in the $FILTERS
variable, they can be used in aws
commands:
aws ec2 describe-instances --filters $FILTERS
Create a simple filter, for example running instances.
filter "instance-state-name" "running"`
... which evaluates to:
Name=instance-state-name,Values="running"
Filter for running EC2 instances in a CloudFormation stack by resource:
logical_id_ec2_filter "my-stack" "MyResource"
... which evaluates to:
Name=instance-state-name,Values="running" Name=tag-key,Values="aws:cloudformation:stack-name" Name=tag-value,Values="my-stack" Name=tag-key,Values="aws:cloudformation:logical-id" Name=tag-value,Values="MyResource"
Use the metadata
function to retrieve instance metadata. Pass everything after
http://169.254.169.254/latest/meta-data/
as the first argument. For example,
to get the instance ID and the availability zone of the running EC2 instance,
run the following commands:
metadata "instance-id"
metadata "placement/availability-zone"
Check the Instance Metadata and User Data documentation for available metadata items.
Use the cfn_parameter
function to get the value of a parameter in a
CloudFormation stack.
cfn_parameter "my-stack" "SomeParameter"
Use the resource_id_from_logical_id
function to get the resource's unique ID
given a CloudFormation stack name and resource ID.
resource_id_from_logical_id "my-stack" "MyResource"
Check the "Return Value" of the Resource Type you are referencing for the value that will be returned by this function.
Use the ips_from_logical_id
function output the public IP addresses for EC2
instances in a CloudFormation stack associated with a resource that are running.
ips_from_logical_id "my-stack" "MyResource"
Use the amis_from_logical_id
function output the unique image IDs that EC2
instances in a CloudFormation stack associated with a resource ID were launched
from.
amis_from_logical_id "my-stack" "MyResource"
Use the rds_*_from_instance_id
functions to get an RDS instance's host and
port.
# Assumes $instance_id is set
rds_host_from_instance_id "$instance_id"
rds_port_from_instance_id "$instance_id"
Note: Use resource_id_from_logical_id
to get the instance ID if RDS is managed
via CloudFormation.
Use the *_route53_record
functions to create or delete records in Route53.
create_route53_record "www.new-record.mywebsite.com" "52.207.211.51" "Z2JSA8O07MCV3T" "A" "900"
delete_route53_record "www.new-record.mywebsite.com" "52.207.211.51" "Z2JSA8O07MCV3T" "A" "900"