Skip to content

Instantly share code, notes, and snippets.

@roens
Last active June 25, 2020 22:50
Show Gist options
  • Save roens/717ab0afa64c8949c20f2c99cb998ee2 to your computer and use it in GitHub Desktop.
Save roens/717ab0afa64c8949c20f2c99cb998ee2 to your computer and use it in GitHub Desktop.
CLI tools for working with AWS ECS Service Discovery. In the absense of any useful Console view of thw whole Service Discovery scene in an account, I've found it necessary to come up with all this.
# A "1-liner" for listing all services.
aws servicediscovery list-services --query 'Services[].{Id:Id,Name:Name}' --output text | sort -bk 2
# A "1-liner" for deleting cruft services, and any associated insttance registrations.
# The `$id` at the start is one from the first column of the above command to list services.
id=''; \
instances=$( \
aws servicediscovery list-instances --service-id $id --query 'Instances[*].Id' --output text \
); \
for x in $instances; do
echo "drop instance ${x} from ${id}"; \
aws servicediscovery deregister-instance --service-id $id --instance-id $x; \
done; \
echo "delete service $id"; \
sleep 2; \
aws servicediscovery delete-service --id $id
# Create Service
# Needs two variables:
# $SD_NAME The name of the service to create
# $SD_NS_ID The ID of the Namespace to create the Service within
aws servicediscovery create-service \
--name "${SD_NAME}" \
--dns-config "NamespaceId=${SD_NS_ID},RoutingPolicy=MULTIVALUE,DnsRecords=[{Type=A,TTL=60}]"
#===================================
# ToDo:
# * Adjust "list services" above to also output the Namespace for each
# * Could probably employ `jq` to clean up output & mix together values from separate `aws` commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment