Skip to content

Commit e4f9bc9

Browse files
committed
add function to get instance with specific tags
1 parent d738b92 commit e4f9bc9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

modules/bash-commons/src/aws-wrapper.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ function aws_wrapper_get_ips_in_asg {
169169
echo "$instances" | jq -r ".Reservations[].Instances[].$ip_param"
170170
}
171171

172+
# Return a space-separated list of IPs belonging to instances with specific tag values.
173+
# If use_public_ips is "true", these will be the public IPs; otherwise, these will be the private IPs.
174+
function aws_wrapper_get_ips_with_tag {
175+
local readonly tag_key="$1"
176+
local readonly tag_value="$2"
177+
local readonly aws_region="$3"
178+
local readonly use_public_ips="$4"
179+
180+
local instances
181+
instances=$(aws_get_instances_with_tag "$tag_key" "$tag_value" "$aws_region")
182+
assert_not_empty_or_null "$instances" "Get info about Instances with tag $tag_key set to $tag_value"
183+
184+
local readonly ip_param=$([[ "$use_public_ips" == "true" ]] && echo "PublicIpAddress" || echo "PrivateIpAddress")
185+
echo "$instances" | jq -r ".Reservations[].Instances[].$ip_param"
186+
}
187+
172188
# Return a space-separated list of hostnames in the given ASG. If use_public_hostnames is "true", these will be the
173189
# public hostnames; otherwise, these will be the private hostnames.
174190
function aws_wrapper_get_hostnames_in_asg {

modules/bash-commons/src/aws.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ function aws_get_instance_tags {
6262
--filters "Name=resource-type,Values=instance" "Name=resource-id,Values=$instance_id"
6363
}
6464

65+
# Get the instances with a given tag and in a specific region. Returns JSON from the AWS CLI's describe-instances command.
66+
function aws_get_instances_with_tag {
67+
local readonly tag_key="$1"
68+
local readonly tag_value="$2"
69+
local readonly instance_region="$3"
70+
71+
aws ec2 describe-instances \
72+
--region "$instance_region" \
73+
--filters "Name=tag:$tag_key,Values=$tag_value"
74+
}
75+
6576
# Describe the given ASG in the given region. Returns JSON from the AWS CLI's describe-auto-scaling-groups command.
6677
function aws_describe_asg {
6778
local readonly asg_name="$1"

0 commit comments

Comments
 (0)