Skip to content

Commit 8f7678f

Browse files
authored
Merge pull request #6 from gruntwork-io/feature/get-instance-with-tags
Add function to get instance with specific tags
2 parents d738b92 + 91beaf7 commit 8f7678f

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ 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+
183+
local readonly ip_param=$([[ "$use_public_ips" == "true" ]] && echo "PublicIpAddress" || echo "PrivateIpAddress")
184+
echo "$instances" | jq -r ".Reservations[].Instances[].$ip_param"
185+
}
186+
172187
# Return a space-separated list of hostnames in the given ASG. If use_public_hostnames is "true", these will be the
173188
# public hostnames; otherwise, these will be the private hostnames.
174189
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"

test/aws-cli.bats

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,32 @@ END_HEREDOC
121121
local num_instances
122122
num_instances=$(echo "$output" | jq -r '.Reservations | length')
123123
assert_greater_than "$num_instances" 0
124-
}
124+
}
125+
126+
@test "aws_get_instances_with_tag empty" {
127+
run aws_get_instances_with_tag "Name" "Value" "us-east-1"
128+
assert_success
129+
130+
local readonly expected=$(cat <<END_HEREDOC
131+
{
132+
"Reservations": []
133+
}
134+
END_HEREDOC
135+
)
136+
137+
assert_output_json "$expected"
138+
}
139+
140+
@test "aws_get_instances_with_tag non-empty" {
141+
local -r tag_key="Name"
142+
local -r tag_value="Value"
143+
local -r region="us-east-1"
144+
145+
create_mock_instance_with_tags "$tag_key" "$tag_value"
146+
run aws_get_instances_with_tag "$tag_key" "$tag_value" "$region"
147+
assert_success
148+
149+
local num_instances
150+
num_instances=$(echo "$output" | jq -r '.Reservations | length')
151+
assert_greater_than "$num_instances" 0
152+
}

0 commit comments

Comments
 (0)