Bash-Funk “aws” module
This module contains functions related to Amazon Web Services. It only loads if the aws commandline client is installed or the host is an EC2 instance.
The following commands are available when this module is loaded:
- -aws-account-id
- -aws-az
- -aws-describe-stack
- -aws-instance-id
- -aws-is-ec2
- -aws-private-ip
- -aws-region
- -aws-stack-name
- -aws-vpc-cidr-block
- -aws-vpc-id
- -test-all-aws
License
SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-aws-account-id
Usage: -aws-account-id [OPTION]...
Prints this server's AWS account ID.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
$http_get http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F\" '/accountId/ {print $4}'
-aws-az
Usage: -aws-az [OPTION]...
Prints this server's AWS availability zone.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
$http_get http://169.254.169.254/latest/meta-data/placement/availability-zone
-aws-describe-stack
Usage: -aws-describe-stack [OPTION]... [STACK_NAME]
Prints this server's AWS stack name. The server requires 'AmazonEC2ReadOnlyAccess' permission.
Requirements:
+ Command 'aws' must be available.
Parameters:
STACK_NAME
Name of the stack to describe.
Options:
--region ID
AWS region.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
if [[ ! $_region ]]; then
local _region=$($http_get http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F\" '/region/ {print $4}')
fi
if [[ ! $_STACK_NAME ]]; then
local instanceId=$($http_get http://169.254.169.254/latest/meta-data/instance-id)
local _STACK_NAME=$(aws ec2 describe-instances --region $_region --instance-id $instanceId --query 'Reservations[*].Instances[*].Tags[?Key==`aws:cloudformation:stack-name`].Value' --output text)
fi
aws --region $_region cloudformation describe-stacks --stack-name $_STACK_NAME 2>&1
-aws-instance-id
Usage: -aws-instance-id [OPTION]...
Prints this server's AWS instance ID.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
$http_get http://169.254.169.254/latest/meta-data/instance-id
-aws-is-ec2
Usage: -aws-is-ec2 [OPTION]...
Determins if this server is an EC2 instance.
Options:
-v, --verbose
Prints additional information during command execution.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
if [[ -f /sys/hypervisor/uuid && $(head -c 3 /sys/hypervisor/uuid) == "ec2" ]]; then
[[ $_verbose ]] && echo "This is an AWS EC2 instance." || true
return 0
else
[[ $_verbose ]] && echo "This is no AWS EC2 instance." || true
return 1
fi
-aws-private-ip
Usage: -aws-private-ip [OPTION]...
Prints this server's AWS private IP address.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
$http_get http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F\" '/privateIp/ {print $4}'
-aws-region
Usage: -aws-region [OPTION]...
Prints this server's AWS region.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
$http_get http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F\" '/region/ {print $4}'
-aws-stack-name
Usage: -aws-stack-name [OPTION]...
Prints this server's AWS stack name. The server requires 'AmazonEC2ReadOnlyAccess' permission.
Requirements:
+ Command 'aws' must be available.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
local region=$($http_get http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F\" '/region/ {print $4}')
local instanceId=$($http_get http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 describe-instances --region $region --instance-id $instanceId --query 'Reservations[*].Instances[*].Tags[?Key==`aws:cloudformation:stack-name`].Value' --output text
-aws-vpc-cidr-block
Usage: -aws-vpc-cidr-block [OPTION]...
Prints this server's AWS VPC CIDR Block.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
local mac=$($http_get http://169.254.169.254/latest/meta-data/mac)
$http_get http://169.254.169.254/latest/meta-data/network/interfaces/macs/$mac/vpc-ipv4-cidr-block
-aws-vpc-id
Usage: -aws-vpc-id [OPTION]...
Prints this server's AWS VPC ID.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
hash wget &>/dev/null && local http_get="wget -qO-" || local http_get="curl -s"
local mac=$($http_get http://169.254.169.254/latest/meta-data/mac)
$http_get http://169.254.169.254/latest/meta-data/network/interfaces/macs/$mac/vpc-id
-test-all-aws
Usage: -test-all-aws [OPTION]...
Performs a selftest of all functions of this module by executing each function with option '--selftest'.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
-aws-account-id --selftest && echo || return 1
-aws-az --selftest && echo || return 1
-aws-describe-stack --selftest && echo || return 1
-aws-instance-id --selftest && echo || return 1
-aws-is-ec2 --selftest && echo || return 1
-aws-private-ip --selftest && echo || return 1
-aws-region --selftest && echo || return 1
-aws-stack-name --selftest && echo || return 1
-aws-vpc-cidr-block --selftest && echo || return 1
-aws-vpc-id --selftest && echo || return 1