From 189570cf269f902d5375ef2e261f0407211ef25e Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 14 Dec 2022 15:35:17 +1100 Subject: [PATCH 01/20] stack drift, more asg --- aliases | 3 ++ lib/instance-functions | 8 ++-- lib/rds-functions | 5 +- lib/stack-functions | 104 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 105 insertions(+), 15 deletions(-) diff --git a/aliases b/aliases index 3cfe2ec2..ccf2adfc 100644 --- a/aliases +++ b/aliases @@ -160,7 +160,10 @@ alias stack-asgs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-asgs' alias stack-cancel-update='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-cancel-update' alias stack-create='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-create' alias stack-delete='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-delete' +alias stack-describe-drift='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-describe-drift' +alias stack-detect-drift='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-detect-drift' alias stack-diff='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-diff' +alias stack-diff-drift='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-diff-drift' alias stack-elbs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-elbs' alias stack-events='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-events' alias stack-exports='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma stack-exports' diff --git a/lib/instance-functions b/lib/instance-functions index 96d3a599..51ae37e4 100644 --- a/lib/instance-functions +++ b/lib/instance-functions @@ -22,6 +22,8 @@ instances() { local instance_ids=$(skim-stdin) local filters=$(__bma_read_filters $@) + local sort_key=5 + [ -z $filters ] && sort_key=6 aws ec2 describe-instances \ ${instance_ids/#/'--instance-ids '} \ @@ -34,11 +36,10 @@ instances() { State.Name, [Tags[?Key=='Name'].Value][0][0], LaunchTime, - Placement.AvailabilityZone, - VpcId + Placement.AvailabilityZone ]" | grep -E -- "$filters" | - LC_ALL=C sort -b -k 6 | + LC_ALL=C sort -b -k $sort_key | column -s$'\t' -t } @@ -87,6 +88,7 @@ instance-az() { Reservations[].Instances[][ [ InstanceId, + VpcId, Placement.AvailabilityZone ] ][]" | diff --git a/lib/rds-functions b/lib/rds-functions index 8d524d08..a0270523 100644 --- a/lib/rds-functions +++ b/lib/rds-functions @@ -19,11 +19,14 @@ rds-db-instances() { --output text \ --query "DBInstances[].[ DBInstanceIdentifier, + DBInstanceClass, Engine, EngineVersion, DBInstanceStatus, - DBName + DBName, + MultiAZ ]" | + sed 's/True$/MultiAZ/g' | sed 's/False$//' | grep -E -- "$filters" | LC_ALL=C sort -b -k 1 | column -s$'\t' -t diff --git a/lib/stack-functions b/lib/stack-functions index 153a82a3..754bafa6 100644 --- a/lib/stack-functions +++ b/lib/stack-functions @@ -700,19 +700,20 @@ stack-tail() { stack-template() { - # Return template of a stack - - local inputs=$(skim-stdin "$@") - local stack=$(_bma_stack_name_arg ${inputs}) + # Return template of each stack + local stacks=$(skim-stdin "$@") - [[ -z ${stack} ]] && __bma_usage "stack" && return 1 + [[ -z ${stacks} ]] && __bma_usage "stack [stack]" && return 1 + local stack + for stack in $stacks; do - aws cloudformation get-template \ - --stack-name "$stack" \ - --query TemplateBody | - jq --raw-output --sort-keys . | - sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' # remove trailing blank lines + aws cloudformation get-template \ + --stack-name "$stack" \ + --query TemplateBody | + jq --raw-output --sort-keys . | + sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' # remove trailing blank lines # sometimes appended by jq + done } @@ -791,6 +792,87 @@ stack-validate() { } +stack-detect-drift() { + + # Detect drift for provided stacks; and print coloured diff + local stacks=$(skim-stdin "$@") + [[ -z ${stacks} ]] && __bma_usage "stack [stack]" && return 1 + + for stack in $stacks; do + driftDetectionId=$(aws cloudformation detect-stack-drift \ + --stack-name "$stack" --output text) + DetectionStatus="" + while [[ "$DetectionStatus" != "DETECTION_COMPLETE" ]]; do + printf "." + DescribeDriftDetectionStatus=$(aws cloudformation describe-stack-drift-detection-status \ + --stack-drift-detection-id $driftDetectionId ) + DetectionStatus=$(echo $DescribeDriftDetectionStatus | jq --raw-output .DetectionStatus ) + StackDriftStatus=$(echo $DescribeDriftDetectionStatus | jq --raw-output .StackDriftStatus) + sleep 5; + done + if [[ "$StackDriftStatus" == "IN_SYNC" ]]; then + echo + echo "$stack is in sync" + else + echo "$stack has drifted" + stack-diff-drift $stack + fi + done + +} + +stack-describe-drift() { + + # List stack-tags applied to a stack + + local inputs=$(skim-stdin "$@") + local stack=$(_bma_stack_name_arg ${inputs}) + + [[ -z ${stack} ]] && __bma_usage "stack" && return 1 + + stackResourceDrifts=$( aws cloudformation describe-stack-resource-drifts \ + --stack-name "$stack" \ + --query 'StackResourceDrifts[*].{StackId:StackId,ExpectedProperties:ExpectedProperties,ActualProperties:ActualProperties}' ) +# jq -S <<< $stackResourceDrifts + ExpectedProperties=$(echo $stackResourceDrifts | jq --raw-output .[].ExpectedProperties) +# ExpectedPropertiesPretty=$(jq . -S --raw-output <<< "$ExpectedProperties") + ActualProperties=$(echo $stackResourceDrifts | jq --raw-output .[].ActualProperties) +# ActualPropertiesPretty=$(jq . -S --raw-output <<< "$ActualProperties") + echo + echo "Expected Properties:" + jq . <<< "$ExpectedProperties" + echo "Actual Properties:" + jq . <<< "$ActualProperties" + echo + +} + +stack-diff-drift() { + + # List stack-tags applied to a stack + + local inputs=$(skim-stdin "$@") + local stack=$(_bma_stack_name_arg ${inputs}) + + [[ -z ${stack} ]] && __bma_usage "stack" && return 1 + + stackResourceDrifts=$( aws cloudformation describe-stack-resource-drifts \ + --stack-name "$stack" \ + --query 'StackResourceDrifts[*].{StackId:StackId,ExpectedProperties:ExpectedProperties,ActualProperties:ActualProperties}' ) +# jq -S <<< $stackResourceDrifts + ExpectedProperties=$(echo $stackResourceDrifts | jq --raw-output .[].ExpectedProperties) + ExpectedPropertiesPretty=$(jq . -S --raw-output <<< "$ExpectedProperties") + ActualProperties=$(echo $stackResourceDrifts | jq --raw-output .[].ActualProperties) + ActualPropertiesPretty=$(jq . -S --raw-output <<< "$ActualProperties") + echo + # TODO : check whether diff is installed, otherwise just print $ExpectedPropertiesPretty and $ActualPropertiesPretty + echo "The diff ( Expected | Actual )" + diff -y --left-column --color=always <(printf %s "$ExpectedPropertiesPretty") <(printf %s "$ActualPropertiesPretty") + +} + + + stack-diff(){ # Compare live stack against local template (and optional params file) @@ -845,7 +927,7 @@ _bma_stack_diff_template() { elif command -v icdiff; then local DIFF_CMD=icdiff else - local DIFF_CMD=diff + local DIFF_CMD='diff --color=always' fi $DIFF_CMD -u \ From ff2adf53d71c81c68b14ac46496790a24f0bcb78 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 14 Dec 2022 15:36:03 +1100 Subject: [PATCH 02/20] stack drift, elasticaches --- bash_completion.sh | 3 +++ docs/command-reference.md | 17 ++++++++++++++++- functions | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/bash_completion.sh b/bash_completion.sh index 460734fe..7c149b0d 100644 --- a/bash_completion.sh +++ b/bash_completion.sh @@ -202,7 +202,10 @@ complete -F _bma_stacks_completion stack-asg-instances complete -F _bma_stacks_completion stack-asgs complete -F _bma_stacks_completion stack-cancel-update complete -F _bma_stacks_completion stack-delete +complete -F _bma_stacks_completion stack-describe-drift +complete -F _bma_stacks_completion stack-detect-drift complete -F _bma_stacks_completion stack-diff +complete -F _bma_stacks_completion stack-diff-drift complete -F _bma_stacks_completion stack-elbs complete -F _bma_stacks_completion stack-events complete -F _bma_stacks_completion stack-exports diff --git a/docs/command-reference.md b/docs/command-reference.md index d6259786..73f99c79 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -408,7 +408,7 @@ Show all events for CF stack until update completes or fails. ### stack-template -Return template of a stack +Return template of each stack ### stack-tags @@ -431,6 +431,21 @@ List outputs of a stack Validate a stack template +### stack-detect-drift + +Detect drift for provided stacks; and print coloured diff + + +### stack-describe-drift + +List stack-tags applied to a stack + + +### stack-diff-drift + +List stack-tags applied to a stack + + ### stack-diff Compare live stack against local template (and optional params file) diff --git a/functions b/functions index 76824304..d8efdaa0 100644 --- a/functions +++ b/functions @@ -63,6 +63,8 @@ columnise debug ecr-repositories ecr-repository-images +elasticaches +elasticache-replication-groups elb-azs elb-dnsname elb-instances @@ -161,7 +163,10 @@ stack-asgs stack-cancel-update stack-create stack-delete +stack-describe-drift +stack-detect-drift stack-diff +stack-diff-drift stack-elbs stack-events stack-exports From d15ab9bf1e2b169cc57b1a0d0fdadfc438fe16ca Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 22 Sep 2023 09:26:31 +1000 Subject: [PATCH 03/20] add complex ecs and simple elasticache queries --- lib/ecs-functions | 118 ++++++++++++++++++++++++++++++++++++++ lib/elasticache-functions | 56 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 lib/ecs-functions create mode 100644 lib/elasticache-functions diff --git a/lib/ecs-functions b/lib/ecs-functions new file mode 100644 index 00000000..e080a4c1 --- /dev/null +++ b/lib/ecs-functions @@ -0,0 +1,118 @@ +#!/bin/bash +# +# ecs-functions +## +# Shortcut the complicated dependencies between clusters, services, and tasks in the ecs cli + +ecs-clusters() { + # List ECS clusters + # output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount + # if you pass an argument, it'll filter for clusters whose ARN contains your text + # + # $ ecs-clusters test + # test-octopus-ecs-cluster ACTIVE 1 1 0 + # test1-ecs-cluster ACTIVE 3 1 0 + # test3-ecs-cluster ACTIVE 3 1 0 + # test2-ecs-cluster ACTIVE 3 3 0 + + local cluster=$(__bma_read_filters $@) # Filter output by arguments (optional) +# echo "cluster=$cluster" + aws ecs describe-clusters \ + --clusters $(aws ecs list-clusters --query "clusterArns[?contains(@, \`${cluster}\`) == \`true\`]" --output text) \ + --query "clusters[].[clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount]" \ + --output text | + LC_ALL=C sort -b -k 2| + columnise +} + +ecs-services() { + + # List ECS services + # output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt + # + # gets all clusters if no filter passed in + # if you do pass a filter: + # 1. if your filter is the name of one of your clusters, it will list the services in that cluster (eg ecs-clusters test1 | ecs-services) + # 2. if your filter is not a cluster name, it will list the services in all clusters whose names match your filter (ie it filters on cluster name not service name) + # 3. if you do not pass a filter, it will list all services in all clusters + # + # $ ecs-clusters test1|ecs-services + # test1-ecs-admin-7URaUr0YGJHi ACTIVE 0 0 0 2023-09-13T17:16:48.198000+10:00 + # test1-ecs-public-wEaTAqGXqbpq ACTIVE 0 0 0 2023-09-13T16:54:54.162000+10:00 + # test1-ecs-hangfire-YNIo1hlx8rjn ACTIVE 1 1 0 2023-09-13T16:39:06.218000+10:00 + + local filter=$(skim-stdin "$@") + local cluster_names=$filter +# check for input that isn't a valid cluster name and use it as a regex... + if [[ -z "$cluster_names" || "$(aws ecs describe-clusters --clusters $cluster_names --query "failures[].reason" --output text)" == "MISSING" ]]; then + #echo "cluster not found, using input to search" + cluster_names=$(ecs-clusters $cluster_names |skim-stdin) + if [[ -z "$cluster_names" ]]; then + #echo "no matching cluster, using input as a service filter" + cluster_names=$(ecs-clusters |skim-stdin) + service_filter=$filter + fi + fi +# echo "cluster_names=$cluster_names" + + local cluster_name + for cluster_name in $(echo $cluster_names); do # other scripts don't need this echo?? +# echo "cluster_name=$cluster_name" + aws ecs list-services \ + --cluster "$cluster_name" \ + --query " + serviceArns[?contains(@, \`${service_filter}\`) == \`true\`] + " \ + --output text | + xargs -d '\t' -I {} echo {} | + sed 's/.*\///' | + xargs -I {} \ + aws ecs describe-services \ + --cluster $cluster_name \ + --services {} \ + --query " + services[].[ + serviceName,status,desiredCount,runningCount,pendingCount,createdAt + ]" \ + --output text | + columnise +# --query "services[].[serviceName,status,desiredCount,runningCount,pendingCount,taskDefinition,createdAt,deployments[0].createdAt]" \ + + done +} + +ecs-tasks() { + + # List ECS tasks + # output includes taskDefinitionArn, createdAt, cpu, memory + # + # gets all tasks if no filter passed in + # if you do pass a filter, it filters on the task name. All clusters are included (I haven't worked out a way of passing a cluster name AND a filter) + # + # $ ecs-tasks test2 + # arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-public:18 2023-09-19T17:51:56.418000+10:00 2048 4096 + # arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-admin:20 2023-08-29T10:03:36.956000+10:00 2048 4096 + # arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-hangfire:22 2023-09-19T17:11:06.622000+10:00 1024 2048 + + local filter=$(skim-stdin "$@") + + for cluster_name in $(ecs-clusters |skim-stdin); do + #echo "cluster_name=$cluster_name" + aws ecs describe-tasks \ + --cluster $cluster_name \ + --query " + tasks[?contains(taskDefinitionArn, \`$filter\`) == \`true\`] + .[taskDefinitionArn, createdAt, cpu, memory] + " \ + --tasks $( \ + aws ecs list-tasks \ + --cluster $cluster_name \ + --output text | + sed 's/.*\///' \ + ) \ + --output text | + columnise + done + +# --query 'tasks[].{createdAt: createdAt,startedAt: startedAt,cpu: cpu,memory: memory,taskDefinitionArn: taskDefinitionArn,ephemeralStorage: ephemeralStorage.sizeInGiB,privateIpv4Address: containers[0].networkInterfaces[0].privateIpv4Address}' \ +} \ No newline at end of file diff --git a/lib/elasticache-functions b/lib/elasticache-functions new file mode 100644 index 00000000..ca0b1b89 --- /dev/null +++ b/lib/elasticache-functions @@ -0,0 +1,56 @@ +#!/bin/bash +# +# elasticache-functions +# +# + + +elasticaches() { + + # List elasticache thingies (code borrowed from target-groups) + # + # $ target-groups + # bash-my-aws-nlb-tg TCP 22 vpc-04636ebe5573f6f65 instance bash-my-aws-nlb + # bash-my-aws-alb-tg HTTP 443 vpc-04636ebe5573f6f65 instance bash-my-aws-alb + + local cache_names=$(skim-stdin) + local filters=$(__bma_read_filters $@) + + aws elasticache describe-cache-clusters \ + --output text \ + --query " + CacheClusters[][ + CacheClusterId, + CacheNodeType, + Engine, + EngineVersion, + CacheClusterStatus + ]" | + grep -E -- "$filters" | + LC_ALL=C sort -b | + column -s$'\t' -t +} + + +elasticache-replication-groups() { + + # + # Accepts a string to filter on + # This is not very useful without column headings. + # Most of the things you want to know about a replication group are boolean + # eg AutomaticFailover, MultiAZClusterEnabled, AtRestEncryptionEnabled etc + + local ec_names=$(skim-stdin "$@") + + for ec_name in $ec_names; do +# echo "ec_name=$ec_name" + aws elasticache describe-replication-groups \ + --output text \ + --query " + ReplicationGroups + [?contains(ReplicationGroupId, \`$ec_name\`) == \`true\`] + .[ReplicationGroupId,Status, CacheNodeType] + " + done | column -s$'\t' -t +} +# how to get ReplicationGroupId etc?? \ No newline at end of file From ca10c60c9efbf9294d28514c4e8012837d2e0c51 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 25 Sep 2023 11:54:42 +1000 Subject: [PATCH 04/20] tweak ecr functions --- aliases | 10 ++++++++++ functions | 10 +++++++++- lib/ecr-functions | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/aliases b/aliases index 115a35b7..a0224279 100644 --- a/aliases +++ b/aliases @@ -89,12 +89,19 @@ alias connector-group-members='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma connector alias connector-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma connector-groups' alias connectors='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma connectors' alias debug='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma debug' +alias deployment='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment' alias deployment-delete-danger='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-delete-danger' alias deployment-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-groups' +alias deployments='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments' alias deployments-group='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments-group' alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' alias ecr-repository-images='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repository-images' +alias ecs-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters' +alias ecs-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services' +alias ecs-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-tasks' +alias elasticache-replication-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticache-replication-groups' +alias elasticaches='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticaches' alias elb-azs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elb-azs' alias elb-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elb-dnsname' alias elb-instances='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elb-instances' @@ -108,6 +115,9 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' +alias fargate-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-clusters' +alias fargate-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-services' +alias fargate-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-tasks' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' diff --git a/functions b/functions index bf860c57..2f7c1d41 100644 --- a/functions +++ b/functions @@ -89,14 +89,19 @@ connector-group-members connector-groups connectors debug +deployment deployment-delete-danger deployment-groups +deployments deployments-group distributions ecr-repositories ecr-repository-images -elasticaches +ecs-clusters +ecs-services +ecs-tasks elasticache-replication-groups +elasticaches elb-azs elb-dnsname elb-instances @@ -110,6 +115,9 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s +fargate-clusters +fargate-services +fargate-tasks hosted-zone-ns-records hosted-zones iam-role-principal diff --git a/lib/ecr-functions b/lib/ecr-functions index e5db48ce..8ad317c8 100644 --- a/lib/ecr-functions +++ b/lib/ecr-functions @@ -34,6 +34,8 @@ ecr-repository-images() { local repository_names=$(skim-stdin "$@") # XXX Display USAGE if no repository_names passed in + local repository_names=$(skim-stdin "$@") + [[ -z ${repository_names} ]] && __bma_usage "ecr" && return 1 local repository_name for repository_name in $repository_names; do From ac92434c7a27547be03c8a5779ffb59562578fe1 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 25 Sep 2023 14:45:46 +1000 Subject: [PATCH 05/20] add scaling-ecs --- lib/autoscaling-functions | 42 +++++++++++++++++++++++++++++++++++++++ lib/ecs-functions | 3 ++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/autoscaling-functions diff --git a/lib/autoscaling-functions b/lib/autoscaling-functions new file mode 100644 index 00000000..991cab16 --- /dev/null +++ b/lib/autoscaling-functions @@ -0,0 +1,42 @@ +#!/bin/bash +# +# autoscaling-functions +## +# show the current autoscale settings +# namespace | implemented +# ecs | yes +# elasticmapreduce | no +# ec2 | maybe +# appstream | maybe +# dynamodb | no +# rds | maybe +# sagemaker | no +# custom-resource | no +# comprehend | no +# lambda | no +# cassandra | no +# kafka | no +# elasticache | no +# neptune | no + + +scaling-ecs() { + # List autoscaling actions + # filter by environment (eg test1) or namespace (eg ecs) + # if you pass an argument, it'll filter for clusters whose ARN contains your text + # + # $ scaling-actions test + # test-octopus-ecs-cluster ACTIVE 1 1 0 + # test1-ecs-cluster ACTIVE 3 1 0 + # test3-ecs-cluster ACTIVE 3 1 0 + # test2-ecs-cluster ACTIVE 3 3 0 + + local filters=$(__bma_read_filters $@) # Filter output by arguments (optional) + aws application-autoscaling describe-scheduled-actions \ + --service-namespace ecs \ + --query "ScheduledActions[].[ScheduledActionName,Schedule,ScalableTargetAction.MinCapacity,ScalableTargetAction.MaxCapacity]" \ + --output text | + grep -E -- "$filters" | + LC_ALL=C sort -b -k 2| + columnise +} diff --git a/lib/ecs-functions b/lib/ecs-functions index e080a4c1..7a48bceb 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -98,6 +98,7 @@ ecs-tasks() { for cluster_name in $(ecs-clusters |skim-stdin); do #echo "cluster_name=$cluster_name" + # TODO: remove the account bit from the task definition, ie print gc3-test1-public:27 instead of arn:aws:ecs:ap-southeast-2:167642850091:task-definition/gc3-test1-public:27 aws ecs describe-tasks \ --cluster $cluster_name \ --query " @@ -115,4 +116,4 @@ ecs-tasks() { done # --query 'tasks[].{createdAt: createdAt,startedAt: startedAt,cpu: cpu,memory: memory,taskDefinitionArn: taskDefinitionArn,ephemeralStorage: ephemeralStorage.sizeInGiB,privateIpv4Address: containers[0].networkInterfaces[0].privateIpv4Address}' \ -} \ No newline at end of file +} From 30c0abc530e83bd4bb3fce7a54372e158c275315 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 25 Sep 2023 14:47:58 +1000 Subject: [PATCH 06/20] add scaling-ecs --- lib/autoscaling-functions | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/autoscaling-functions b/lib/autoscaling-functions index 991cab16..fbc82ef0 100644 --- a/lib/autoscaling-functions +++ b/lib/autoscaling-functions @@ -25,11 +25,13 @@ scaling-ecs() { # filter by environment (eg test1) or namespace (eg ecs) # if you pass an argument, it'll filter for clusters whose ARN contains your text # - # $ scaling-actions test - # test-octopus-ecs-cluster ACTIVE 1 1 0 - # test1-ecs-cluster ACTIVE 3 1 0 - # test3-ecs-cluster ACTIVE 3 1 0 - # test2-ecs-cluster ACTIVE 3 3 0 + # $ scaling-ecs 'test.*down' # list the scale-down times of all our test environments +# test1-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test1-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test2-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test2-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test3-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test3-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 local filters=$(__bma_read_filters $@) # Filter output by arguments (optional) aws application-autoscaling describe-scheduled-actions \ From eeb84bc0e93d3e2c6ee6d2df1571d266a49dfd9c Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Mon, 25 Sep 2023 20:50:48 +1000 Subject: [PATCH 07/20] add instance-id --- lib/instance-functions | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/instance-functions b/lib/instance-functions index 51ae37e4..35585f4b 100644 --- a/lib/instance-functions +++ b/lib/instance-functions @@ -44,6 +44,18 @@ instances() { } +instance-id() { + + # Just return the instance ID of whatever was passed in (so you can run a for loop, for instance) + # + # USAGE: instances [grep] | instance-id + + local instance_ids=$(skim-stdin "$@") + [[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1 + echo $instance_ids | + column -s$'\t' -t +} + instance-asg() { # List autoscaling group membership of EC2 Instance(s) From 60b8e1273d1ca8be7c945ed9947ef03bcb757958 Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Wed, 27 Sep 2023 16:56:05 +1000 Subject: [PATCH 08/20] revise cluster-checking --- lib/ecs-functions | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/ecs-functions b/lib/ecs-functions index 7a48bceb..4f381354 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -3,6 +3,11 @@ # ecs-functions ## # Shortcut the complicated dependencies between clusters, services, and tasks in the ecs cli +# this is against the normal way bash-my-aws works +# it should be +# ecs-tasks [cluster-name] +# ecs-services [cluster-name] +# but then i don't know how to filter on the task/service name ecs-clusters() { # List ECS clusters @@ -42,17 +47,36 @@ ecs-services() { # test1-ecs-hangfire-YNIo1hlx8rjn ACTIVE 1 1 0 2023-09-13T16:39:06.218000+10:00 local filter=$(skim-stdin "$@") + echo "searching with $filter" local cluster_names=$filter # check for input that isn't a valid cluster name and use it as a regex... if [[ -z "$cluster_names" || "$(aws ecs describe-clusters --clusters $cluster_names --query "failures[].reason" --output text)" == "MISSING" ]]; then - #echo "cluster not found, using input to search" + echo "cluster not found, using input to search" cluster_names=$(ecs-clusters $cluster_names |skim-stdin) if [[ -z "$cluster_names" ]]; then - #echo "no matching cluster, using input as a service filter" + echo "no matching cluster, using input as a service filter" cluster_names=$(ecs-clusters |skim-stdin) service_filter=$filter fi fi + + +### wait that wasn't right. +## if $filter is empty just get all the clusters +## if $filter is not empty, loop through $filter to check for valid complete cluster names +## if there is one, skip the rest +## search for cluster names containing $filter +# if [[ -z "$cluster_names" ]]; then +# echo "no matching cluster, passing all clusters to search" +# cluster_names=$(ecs-clusters |skim-stdin) +# ## here we should loop through the $filter checking that each element is a cluster?, in case the ecs-clusters command returned more than one + ##for cluster_name in $(echo $cluster_names); do # other scripts don't need this echo?? +# elif [[ "$(aws ecs describe-clusters --clusters $cluster_names --query "failures[].reason" --output text)" == "MISSING" ]]; then +# echo "cluster not found, using input to search" +# cluster_names=$(ecs-clusters $cluster_names |skim-stdin) +# service_filter=$filter +# fi +# echo "cluster_names=$cluster_names" # echo "cluster_names=$cluster_names" local cluster_name @@ -77,7 +101,6 @@ ecs-services() { --output text | columnise # --query "services[].[serviceName,status,desiredCount,runningCount,pendingCount,taskDefinition,createdAt,deployments[0].createdAt]" \ - done } From c9d71292fd8166cda5266103b2a0a83f4fff6ea8 Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Tue, 3 Oct 2023 13:59:48 +1100 Subject: [PATCH 09/20] update build --- aliases | 6 +- bash_completion.sh | 1 + docs/command-reference.md | 124 +++++++++++++++++++++++++++++++++++++- functions | 6 +- 4 files changed, 128 insertions(+), 9 deletions(-) diff --git a/aliases b/aliases index a0224279..67179885 100644 --- a/aliases +++ b/aliases @@ -92,7 +92,6 @@ alias debug='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma debug' alias deployment='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment' alias deployment-delete-danger='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-delete-danger' alias deployment-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-groups' -alias deployments='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments' alias deployments-group='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments-group' alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' @@ -115,9 +114,6 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' -alias fargate-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-clusters' -alias fargate-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-services' -alias fargate-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-tasks' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' @@ -131,6 +127,7 @@ alias instance-console='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-console alias instance-dns='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-dns' alias instance-health-set-unhealthy='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-health-set-unhealthy' alias instance-iam-profile='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-iam-profile' +alias instance-id='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-id' alias instance-ip='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-ip' alias instance-rdp='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-rdp' alias instance-ssh='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-ssh' @@ -198,6 +195,7 @@ alias resource-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resource-groups' alias resource-show='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resource-show' alias resourceids='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resourceids' alias resources='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resources' +alias scaling-ecs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma scaling-ecs' alias secrets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma secrets' alias service-principals='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma service-principals' alias skim-stdin='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma skim-stdin' diff --git a/bash_completion.sh b/bash_completion.sh index a921a5bd..e8fc781f 100644 --- a/bash_completion.sh +++ b/bash_completion.sh @@ -202,6 +202,7 @@ complete -F _bma_instances_completion instance-console complete -F _bma_instances_completion instance-dns complete -F _bma_instances_completion instance-health-set-unhealthy complete -F _bma_instances_completion instance-iam-profile +complete -F _bma_instances_completion instance-id complete -F _bma_instances_completion instance-ip complete -F _bma_instances_completion instance-rdp complete -F _bma_instances_completion instance-ssh diff --git a/docs/command-reference.md b/docs/command-reference.md index e09f9885..7a0832d6 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -533,6 +533,13 @@ List EC2 Instances i-806d8f1592e2a2efd ami-123456789012 t3.nano running postgres2 2019-12-10T08:17:22.000Z ap-southeast-2a None +### instance-id + +Just return the instance ID of whatever was passed in (so you can run a for loop, for instance) + + USAGE: instances [grep] | instance-id + + ### instance-asg List autoscaling group membership of EC2 Instance(s) @@ -905,7 +912,16 @@ List CloudFormation stack for asg(s) List scaling activities for Autoscaling Group(s) -azure.azcli +## autoscaling-commands + + +### scaling-ecs + +List autoscaling actions +filter by environment (eg test1) or namespace (eg ecs) +if you pass an argument, it'll filter for clusters whose ARN contains your text + + $ scaling-ecs 'test.*down' # list the scale-down times of all our test environments ## azure-commands @@ -1237,6 +1253,9 @@ List routes of all endpoints for Front Door Profile(s) +azure.azcli + + ## backup-commands @@ -1326,6 +1345,19 @@ List logging status of Cloudtrails USAGE: cloudtrail-status cloudtrail [cloudtrail] +## codedeploy-commands + + +### deployment + +List deployments + + +### deployment-groups + +List min, desired and maximum capacities of EC2 Autoscaling Group(s) + + ## ecr-commands @@ -1339,6 +1371,96 @@ List ECR Repositories List images for ECR Repositories +## ecs-commands + + +### ecs-clusters + +List ECS clusters +output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount +if you pass an argument, it'll filter for clusters whose ARN contains your text + + $ ecs-clusters test + test-octopus-ecs-cluster ACTIVE 1 1 0 + test1-ecs-cluster ACTIVE 3 1 0 + test3-ecs-cluster ACTIVE 3 1 0 + test2-ecs-cluster ACTIVE 3 3 0 + + +### ecs-services + +List ECS services +output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt + +gets all clusters if no filter passed in +if you do pass a filter: +1. if your filter is the name of one of your clusters, it will list the services in that cluster (eg ecs-clusters test1 | ecs-services) +2. if your filter is not a cluster name, it will list the services in all clusters whose names match your filter (ie it filters on cluster name not service name) +3. if you do not pass a filter, it will list all services in all clusters + + $ ecs-clusters test1|ecs-services + test1-ecs-admin-7URaUr0YGJHi ACTIVE 0 0 0 2023-09-13T17:16:48.198000+10:00 + test1-ecs-public-wEaTAqGXqbpq ACTIVE 0 0 0 2023-09-13T16:54:54.162000+10:00 + test1-ecs-hangfire-YNIo1hlx8rjn ACTIVE 1 1 0 2023-09-13T16:39:06.218000+10:00 + + +### ecs-tasks + +List ECS tasks +output includes taskDefinitionArn, createdAt, cpu, memory + +gets all tasks if no filter passed in +if you do pass a filter, it filters on the task name. All clusters are included (I haven't worked out a way of passing a cluster name AND a filter) + + $ ecs-tasks test2 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-public:18 2023-09-19T17:51:56.418000+10:00 2048 4096 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-admin:20 2023-08-29T10:03:36.956000+10:00 2048 4096 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-hangfire:22 2023-09-19T17:11:06.622000+10:00 1024 2048 + + +## elasticache-commands + + +### elasticaches + +List elasticache thingies (code borrowed from target-groups) + + $ target-groups + bash-my-aws-nlb-tg TCP 22 vpc-04636ebe5573f6f65 instance bash-my-aws-nlb + bash-my-aws-alb-tg HTTP 443 vpc-04636ebe5573f6f65 instance bash-my-aws-alb + + +### elasticache-replication-groups + + +Accepts a string to filter on +This is not very useful without column headings. +Most of the things you want to know about a replication group are boolean +eg AutomaticFailover, MultiAZClusterEnabled, AtRestEncryptionEnabled etc + + +## elasticache-commands-littlelaptop + + +### elasticaches + +List elasticache thingies + + $ target-groups + bash-my-aws-nlb-tg TCP 22 vpc-04636ebe5573f6f65 instance bash-my-aws-nlb + bash-my-aws-alb-tg HTTP 443 vpc-04636ebe5573f6f65 instance bash-my-aws-alb + + +### elasticache-replication-groups + + +Accepts Target Group names on stdin or as arguments + + $ target-group-targets bash-my-aws-nlb-tg + i-4e15ece1de1a3f869 443 healthy bash-my-aws-nlb-tg + i-89cefa9403373d7a5 443 unhealthy bash-my-aws-nlb-tg + + ## elb-commands diff --git a/functions b/functions index 2f7c1d41..3cb8503b 100644 --- a/functions +++ b/functions @@ -92,7 +92,6 @@ debug deployment deployment-delete-danger deployment-groups -deployments deployments-group distributions ecr-repositories @@ -115,9 +114,6 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s -fargate-clusters -fargate-services -fargate-tasks hosted-zone-ns-records hosted-zones iam-role-principal @@ -131,6 +127,7 @@ instance-console instance-dns instance-health-set-unhealthy instance-iam-profile +instance-id instance-ip instance-rdp instance-ssh @@ -199,6 +196,7 @@ resource-groups resource-show resourceids resources +scaling-ecs secrets service-principals skim-stdin From 21a0fa6bad8ac1ecf6fe000c27342d1783c800db Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 5 Oct 2023 14:10:16 +1100 Subject: [PATCH 10/20] update build --- docs/command-reference.md | 70 ++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/docs/command-reference.md b/docs/command-reference.md index 7a0832d6..025a9f24 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -1353,9 +1353,33 @@ List logging status of Cloudtrails List deployments +### deployments + +List all deployment IDs for a deployment group (not useful for the user, only internal) +# ?? if no deployment group, could we list all deployments for this application, with their groups and statuses? + + ### deployment-groups -List min, desired and maximum capacities of EC2 Autoscaling Group(s) +List all deployment groups for an application + + +## codedeploy-commands~ + + +### deployment + +List deployments + + +### deployments + +List all deployment IDs for a deployment group (not useful for the user, only internal) + + +### deployment-groups + +List all deployment groups for an application ## ecr-commands @@ -1439,28 +1463,6 @@ Most of the things you want to know about a replication group are boolean eg AutomaticFailover, MultiAZClusterEnabled, AtRestEncryptionEnabled etc -## elasticache-commands-littlelaptop - - -### elasticaches - -List elasticache thingies - - $ target-groups - bash-my-aws-nlb-tg TCP 22 vpc-04636ebe5573f6f65 instance bash-my-aws-nlb - bash-my-aws-alb-tg HTTP 443 vpc-04636ebe5573f6f65 instance bash-my-aws-alb - - -### elasticache-replication-groups - - -Accepts Target Group names on stdin or as arguments - - $ target-group-targets bash-my-aws-nlb-tg - i-4e15ece1de1a3f869 443 healthy bash-my-aws-nlb-tg - i-89cefa9403373d7a5 443 unhealthy bash-my-aws-nlb-tg - - ## elb-commands @@ -1597,6 +1599,28 @@ List target groups of ELBv2(s) [Application and Network Load Balancers) bash-my-aws-alb-tg HTTP 443 vpc-018d9739 bash-my-aws-alb +## fargate-commands + + +### fargate-clusters + +List ECS clusters + + +### fargate-services + +List ECS services +gets all clusters if no cluster_names passed in +echo "cluster_names=$cluster_names" + + +### fargate-tasks + +List ECS services +gets all clusters if no cluster_names passed in +echo "service_names=$service_names" + + ## iam-commands From fcb542656881733073c67283cd8e91d36b78ea5e Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 24 Oct 2023 11:39:32 +1100 Subject: [PATCH 11/20] improve ecs task output --- aliases | 5 ++++- functions | 5 ++++- lib/ecs-functions | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/aliases b/aliases index 67179885..a998dbda 100644 --- a/aliases +++ b/aliases @@ -92,13 +92,13 @@ alias debug='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma debug' alias deployment='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment' alias deployment-delete-danger='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-delete-danger' alias deployment-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-groups' +alias deployments='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments' alias deployments-group='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments-group' alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' alias ecr-repository-images='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repository-images' alias ecs-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters' alias ecs-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services' -alias ecs-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-tasks' alias elasticache-replication-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticache-replication-groups' alias elasticaches='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticaches' alias elb-azs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elb-azs' @@ -114,6 +114,9 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' +alias fargate-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-clusters' +alias fargate-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-services' +alias fargate-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-tasks' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' diff --git a/functions b/functions index 3cb8503b..5fd2048c 100644 --- a/functions +++ b/functions @@ -92,13 +92,13 @@ debug deployment deployment-delete-danger deployment-groups +deployments deployments-group distributions ecr-repositories ecr-repository-images ecs-clusters ecs-services -ecs-tasks elasticache-replication-groups elasticaches elb-azs @@ -114,6 +114,9 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s +fargate-clusters +fargate-services +fargate-tasks hosted-zone-ns-records hosted-zones iam-role-principal diff --git a/lib/ecs-functions b/lib/ecs-functions index 4f381354..8b010afb 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -126,7 +126,7 @@ ecs-tasks() { --cluster $cluster_name \ --query " tasks[?contains(taskDefinitionArn, \`$filter\`) == \`true\`] - .[taskDefinitionArn, createdAt, cpu, memory] + .[taskDefinitionArn, healthStatus, lastStatus, createdAt, cpu, memory] " \ --tasks $( \ aws ecs list-tasks \ @@ -135,6 +135,7 @@ ecs-tasks() { sed 's/.*\///' \ ) \ --output text | + sed 's/arn.*task-definition\///g' | columnise done From 2b1f2d42e63042e38b62d4d7fb5a2f8867f2b049 Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Wed, 22 Nov 2023 10:53:12 +1100 Subject: [PATCH 12/20] build --- aliases | 7 +++---- functions | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/aliases b/aliases index b5aaec89..677f2480 100644 --- a/aliases +++ b/aliases @@ -92,13 +92,13 @@ alias debug='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma debug' alias deployment='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment' alias deployment-delete-danger='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-delete-danger' alias deployment-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-groups' -alias deployments='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments' alias deployments-group='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments-group' alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' alias ecr-repository-images='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repository-images' alias ecs-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters' alias ecs-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services' +alias ecs-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-tasks' alias elasticache-replication-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticache-replication-groups' alias elasticaches='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticaches' alias elb-azs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elb-azs' @@ -114,9 +114,6 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' -alias fargate-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-clusters' -alias fargate-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-services' -alias fargate-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-tasks' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' @@ -211,7 +208,9 @@ alias ssm-association-execution-targets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma alias ssm-association-executions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-association-executions' alias ssm-associations='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-associations' alias ssm-automation-execution='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-automation-execution' +alias ssm-automation-execution-failures='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-automation-execution-failures' alias ssm-automation-executions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-automation-executions' +alias ssm-automation-step-executions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-automation-step-executions' alias ssm-instances='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-instances' alias ssm-parameter-value='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-parameter-value' alias ssm-parameters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ssm-parameters' diff --git a/functions b/functions index 8f47eacc..fe956ff5 100644 --- a/functions +++ b/functions @@ -92,13 +92,13 @@ debug deployment deployment-delete-danger deployment-groups -deployments deployments-group distributions ecr-repositories ecr-repository-images ecs-clusters ecs-services +ecs-tasks elasticache-replication-groups elasticaches elb-azs @@ -114,9 +114,6 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s -fargate-clusters -fargate-services -fargate-tasks hosted-zone-ns-records hosted-zones iam-role-principal @@ -212,7 +209,9 @@ ssm-association-execution-targets ssm-association-executions ssm-associations ssm-automation-execution +ssm-automation-execution-failures ssm-automation-executions +ssm-automation-step-executions ssm-instances ssm-parameter-value ssm-parameters From e8d0384c07c8dba18a97463cd7b3aafd83fa38b9 Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Wed, 22 Nov 2023 11:38:30 +1100 Subject: [PATCH 13/20] ecs-scaling functions --- aliases | 3 ++- functions | 3 ++- lib/autoscaling-functions | 22 +++++++++---------- lib/ecs-functions | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/aliases b/aliases index 677f2480..020a74e3 100644 --- a/aliases +++ b/aliases @@ -97,6 +97,8 @@ alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' alias ecr-repository-images='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repository-images' alias ecs-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters' +alias ecs-scaling-actions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-scaling-actions' +alias ecs-scaling-activities='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-scaling-activities' alias ecs-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services' alias ecs-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-tasks' alias elasticache-replication-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticache-replication-groups' @@ -198,7 +200,6 @@ alias resource-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resource-groups' alias resource-show='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resource-show' alias resourceids='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resourceids' alias resources='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma resources' -alias scaling-ecs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma scaling-ecs' alias secrets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma secrets' alias service-principals='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma service-principals' alias skim-stdin='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma skim-stdin' diff --git a/functions b/functions index fe956ff5..7d70a74f 100644 --- a/functions +++ b/functions @@ -97,6 +97,8 @@ distributions ecr-repositories ecr-repository-images ecs-clusters +ecs-scaling-actions +ecs-scaling-activities ecs-services ecs-tasks elasticache-replication-groups @@ -199,7 +201,6 @@ resource-groups resource-show resourceids resources -scaling-ecs secrets service-principals skim-stdin diff --git a/lib/autoscaling-functions b/lib/autoscaling-functions index fbc82ef0..b98b02c6 100644 --- a/lib/autoscaling-functions +++ b/lib/autoscaling-functions @@ -19,8 +19,8 @@ # elasticache | no # neptune | no - -scaling-ecs() { +### this has been moved to the ecs functions. was that a good idea? +##scaling-ecs() { # List autoscaling actions # filter by environment (eg test1) or namespace (eg ecs) # if you pass an argument, it'll filter for clusters whose ARN contains your text @@ -33,12 +33,12 @@ scaling-ecs() { # test3-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 # test3-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 - local filters=$(__bma_read_filters $@) # Filter output by arguments (optional) - aws application-autoscaling describe-scheduled-actions \ - --service-namespace ecs \ - --query "ScheduledActions[].[ScheduledActionName,Schedule,ScalableTargetAction.MinCapacity,ScalableTargetAction.MaxCapacity]" \ - --output text | - grep -E -- "$filters" | - LC_ALL=C sort -b -k 2| - columnise -} +## local filters=$(__bma_read_filters $@) # Filter output by arguments (optional) +## aws application-autoscaling describe-scheduled-actions \ +## --service-namespace ecs \ +## --query "ScheduledActions[].[ScheduledActionName,Schedule,ScalableTargetAction.MinCapacity,ScalableTargetAction.MaxCapacity]" \ +## --output text | +## grep -E -- "$filters" | +## LC_ALL=C sort -b -k 2| +## columnise +##} diff --git a/lib/ecs-functions b/lib/ecs-functions index 8b010afb..a9107de6 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -141,3 +141,49 @@ ecs-tasks() { # --query 'tasks[].{createdAt: createdAt,startedAt: startedAt,cpu: cpu,memory: memory,taskDefinitionArn: taskDefinitionArn,ephemeralStorage: ephemeralStorage.sizeInGiB,privateIpv4Address: containers[0].networkInterfaces[0].privateIpv4Address}' \ } + +ecs-scaling-activities() { + + # show scaling activities for a selected ECS cluster + # eg + # ecs-scaling www + # 2023-11-22T06:24:50.937000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmLow-64de4512-d901-4b26-a6a2-184bb1e90bc6 in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 2. Change successfully fulfilled by ecs. + # 2023-11-22T05:25:48.611000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmHigh-6408c172-647e-4c0e-aac9-a800cd83317d in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 3. Change successfully fulfilled by ecs. + local filter=$(skim-stdin "$@") + +aws application-autoscaling describe-scaling-activities \ + --service-namespace ecs \ + --query " + ScalingActivities[].[StartTime, Description, Cause] + " \ + --output text | + # --resource-id # add cluster and service name for better filtering + grep $filter | + sed 's/monitor alarm TargetTracking-service\/.*\///' | + columnise + +} + + +ecs-scaling-actions() { + # List autoscaling actions + # filter by environment (eg test1) or namespace (eg ecs) + # if you pass an argument, it'll filter for clusters whose ARN contains your text + # + # $ scaling-ecs 'test.*down' # list the scale-down times of all our test environments +# test1-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test1-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test2-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test2-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test3-ecs-admin-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 +# test3-ecs-public-scale-down-overnight cron(0 0 9 ? * MON-FRI *) 0 0 + + local filters=$(__bma_read_filters $@) # Filter output by arguments (optional) + aws application-autoscaling describe-scheduled-actions \ + --service-namespace ecs \ + --query "ScheduledActions[].[ScheduledActionName,Schedule,ScalableTargetAction.MinCapacity,ScalableTargetAction.MaxCapacity]" \ + --output text | + grep -E -- "$filters" | + LC_ALL=C sort -b -k 2| + columnise +} From c16e6aa35dc42427f8c6cb89df4b674208a0d402 Mon Sep 17 00:00:00 2001 From: Andrew Lorien Date: Wed, 22 Nov 2023 11:57:32 +1100 Subject: [PATCH 14/20] trim some dates --- aliases | 1 + functions | 1 + lib/ecs-functions | 10 ++++++---- lib/misc-functions | 4 ++++ lib/stack-functions | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/aliases b/aliases index 020a74e3..b9068646 100644 --- a/aliases +++ b/aliases @@ -256,6 +256,7 @@ alias tag-keys='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma tag-keys' alias tag-values='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma tag-values' alias target-group-targets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma target-group-targets' alias target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma target-groups' +alias trim_date='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma trim_date' alias vpc-az-count='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma vpc-az-count' alias vpc-azs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma vpc-azs' alias vpc-default-delete='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma vpc-default-delete' diff --git a/functions b/functions index 7d70a74f..78b8fc16 100644 --- a/functions +++ b/functions @@ -257,6 +257,7 @@ tag-keys tag-values target-group-targets target-groups +trim_date vpc-az-count vpc-azs vpc-default-delete diff --git a/lib/ecs-functions b/lib/ecs-functions index a9107de6..faea511f 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -144,11 +144,13 @@ ecs-tasks() { ecs-scaling-activities() { - # show scaling activities for a selected ECS cluster + # LIst autoscaling activities - the actual scaling events that have happened # eg # ecs-scaling www # 2023-11-22T06:24:50.937000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmLow-64de4512-d901-4b26-a6a2-184bb1e90bc6 in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 2. Change successfully fulfilled by ecs. # 2023-11-22T05:25:48.611000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmHigh-6408c172-647e-4c0e-aac9-a800cd83317d in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 3. Change successfully fulfilled by ecs. + + # TODO : the list can be very long, add max-items or even better implement generic paging for BMA local filter=$(skim-stdin "$@") aws application-autoscaling describe-scaling-activities \ @@ -158,15 +160,16 @@ aws application-autoscaling describe-scaling-activities \ " \ --output text | # --resource-id # add cluster and service name for better filtering - grep $filter | + grep -E -- "$filters" | sed 's/monitor alarm TargetTracking-service\/.*\///' | + trim_date | columnise } ecs-scaling-actions() { - # List autoscaling actions + # List autoscaling actions - cron-based scheduled scaling # filter by environment (eg test1) or namespace (eg ecs) # if you pass an argument, it'll filter for clusters whose ARN contains your text # @@ -184,6 +187,5 @@ ecs-scaling-actions() { --query "ScheduledActions[].[ScheduledActionName,Schedule,ScalableTargetAction.MinCapacity,ScalableTargetAction.MaxCapacity]" \ --output text | grep -E -- "$filters" | - LC_ALL=C sort -b -k 2| columnise } diff --git a/lib/misc-functions b/lib/misc-functions index 6102aa49..c35d807d 100644 --- a/lib/misc-functions +++ b/lib/misc-functions @@ -20,3 +20,7 @@ columnise() { column -t -s $'\t' fi } + +trim_date() { + sed 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)\S*/\1/g' +} diff --git a/lib/stack-functions b/lib/stack-functions index e5b13921..d962b975 100644 --- a/lib/stack-functions +++ b/lib/stack-functions @@ -90,6 +90,7 @@ stacks() { --output text | grep -E -- "$filters" | LC_ALL=C sort -b -k 3 | + trim_date | columnise } From f73a6442e34fee326cb7dd667e38379b91c3caa4 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 12 Dec 2023 11:30:47 +1100 Subject: [PATCH 15/20] ECS : add IP address --- lib/ecs-functions | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ecs-functions b/lib/ecs-functions index 8b010afb..6446ac23 100644 --- a/lib/ecs-functions +++ b/lib/ecs-functions @@ -9,7 +9,7 @@ # ecs-services [cluster-name] # but then i don't know how to filter on the task/service name -ecs-clusters() { +ecs-clusters2() { # List ECS clusters # output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount # if you pass an argument, it'll filter for clusters whose ARN contains your text @@ -30,7 +30,7 @@ ecs-clusters() { columnise } -ecs-services() { +ecs-services2() { # List ECS services # output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt @@ -52,10 +52,10 @@ ecs-services() { # check for input that isn't a valid cluster name and use it as a regex... if [[ -z "$cluster_names" || "$(aws ecs describe-clusters --clusters $cluster_names --query "failures[].reason" --output text)" == "MISSING" ]]; then echo "cluster not found, using input to search" - cluster_names=$(ecs-clusters $cluster_names |skim-stdin) + cluster_names=$(ecs-clusters2 $cluster_names |skim-stdin) if [[ -z "$cluster_names" ]]; then echo "no matching cluster, using input as a service filter" - cluster_names=$(ecs-clusters |skim-stdin) + cluster_names=$(ecs-clusters2 |skim-stdin) service_filter=$filter fi fi @@ -119,14 +119,14 @@ ecs-tasks() { local filter=$(skim-stdin "$@") - for cluster_name in $(ecs-clusters |skim-stdin); do + for cluster_name in $(ecs-clusters2 |skim-stdin); do #echo "cluster_name=$cluster_name" # TODO: remove the account bit from the task definition, ie print gc3-test1-public:27 instead of arn:aws:ecs:ap-southeast-2:167642850091:task-definition/gc3-test1-public:27 aws ecs describe-tasks \ --cluster $cluster_name \ --query " tasks[?contains(taskDefinitionArn, \`$filter\`) == \`true\`] - .[taskDefinitionArn, healthStatus, lastStatus, createdAt, cpu, memory] + .[taskDefinitionArn, lastStatus, createdAt, cpu, memory, containers[].networkInterfaces[].privateIpv4Address] " \ --tasks $( \ aws ecs list-tasks \ From 3e3cac9360fbb22a61150520c235d15d041915e8 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 12 Dec 2023 13:14:23 +1100 Subject: [PATCH 16/20] build --- aliases | 8 ++++++-- docs/command-reference.md | 38 +++++++++++++++++++++++++++++++++++--- functions | 8 ++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/aliases b/aliases index b9068646..32c89eb6 100644 --- a/aliases +++ b/aliases @@ -92,14 +92,15 @@ alias debug='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma debug' alias deployment='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment' alias deployment-delete-danger='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-delete-danger' alias deployment-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployment-groups' +alias deployments='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments' alias deployments-group='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma deployments-group' alias distributions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma distributions' alias ecr-repositories='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repositories' alias ecr-repository-images='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecr-repository-images' -alias ecs-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters' +alias ecs-clusters2='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-clusters2' alias ecs-scaling-actions='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-scaling-actions' alias ecs-scaling-activities='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-scaling-activities' -alias ecs-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services' +alias ecs-services2='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-services2' alias ecs-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma ecs-tasks' alias elasticache-replication-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticache-replication-groups' alias elasticaches='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elasticaches' @@ -116,6 +117,9 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' +alias fargate-clusters='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-clusters' +alias fargate-services='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-services' +alias fargate-tasks='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma fargate-tasks' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' diff --git a/docs/command-reference.md b/docs/command-reference.md index 3afe468a..ee2cf8ec 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -923,7 +923,7 @@ List scaling activities for Autoscaling Group(s) ## autoscaling-commands -### scaling-ecs +### ##scaling-ecs List autoscaling actions filter by environment (eg test1) or namespace (eg ecs) @@ -1406,7 +1406,7 @@ List images for ECR Repositories ## ecs-commands -### ecs-clusters +### ecs-clusters2 List ECS clusters output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount @@ -1419,7 +1419,7 @@ if you pass an argument, it'll filter for clusters whose ARN contains your text test2-ecs-cluster ACTIVE 3 3 0 -### ecs-services +### ecs-services2 List ECS services output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt @@ -1450,6 +1450,24 @@ if you do pass a filter, it filters on the task name. All clusters are included arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-hangfire:22 2023-09-19T17:11:06.622000+10:00 1024 2048 +### ecs-scaling-activities + +LIst autoscaling activities - the actual scaling events that have happened +eg +ecs-scaling www +2023-11-22T06:24:50.937000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmLow-64de4512-d901-4b26-a6a2-184bb1e90bc6 in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 2. Change successfully fulfilled by ecs. +2023-11-22T05:25:48.611000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmHigh-6408c172-647e-4c0e-aac9-a800cd83317d in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 3. Change successfully fulfilled by ecs. + + +### ecs-scaling-actions + +List autoscaling actions - cron-based scheduled scaling +filter by environment (eg test1) or namespace (eg ecs) +if you pass an argument, it'll filter for clusters whose ARN contains your text + + $ scaling-ecs 'test.*down' # list the scale-down times of all our test environments + + ## elasticache-commands @@ -2124,6 +2142,20 @@ USAGE: ssm-automation-executions [filter] ghijqrst-uvwx-2345-yzab-abcd5678efgh UpdateAndSecureNodes i-3d4e5f6g7h8i90123 Failed 2023-07-20T09:00:40.000000+00:00 None +### ssm-automation-execution-failures + + + +### ssm-automation-step-executions + +Show step-by-step details for an SSM Automation Execution + + USAGE: automation-execution-steps execution_id [execution_id] + + $ ssm-automation-executions | ssm-automation-steps-executions + [Outputs detailed step information for each provided execution ID] + + ### ssm-automation-execution Show details for an SSM Automation Execution diff --git a/functions b/functions index 78b8fc16..5dc311e7 100644 --- a/functions +++ b/functions @@ -92,14 +92,15 @@ debug deployment deployment-delete-danger deployment-groups +deployments deployments-group distributions ecr-repositories ecr-repository-images -ecs-clusters +ecs-clusters2 ecs-scaling-actions ecs-scaling-activities -ecs-services +ecs-services2 ecs-tasks elasticache-replication-groups elasticaches @@ -116,6 +117,9 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s +fargate-clusters +fargate-services +fargate-tasks hosted-zone-ns-records hosted-zones iam-role-principal From a35d7671f9f40432b589378bd40ea48e3417adf8 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 Jan 2024 12:06:00 +1100 Subject: [PATCH 17/20] add A-records, support multiple hosted zones --- lib/route53-functions | 56 ++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/route53-functions b/lib/route53-functions index 08e9b553..bdf7b434 100644 --- a/lib/route53-functions +++ b/lib/route53-functions @@ -42,23 +42,47 @@ hosted-zone-ns-records(){ # bash-my-aws.org. 300 IN NS ns-1464.awsdns-55.org. local inputs=$(skim-stdin "$@") - local hosted_zone_id=$(echo "$inputs" | awk '{print $1}') - [[ -z "$hosted_zone_id" ]] && __bma_usage "hosted-zone-id" && return 1 + local hosted_zone_ids=$(echo "$inputs") + [[ -z "$hosted_zone_ids" ]] && __bma_usage "hosted-zone-id" && return 1 local hosted_zone_name - hosted_zone_name=$( - aws route53 list-resource-record-sets \ - --hosted-zone-id "$hosted_zone_id" \ - --query "ResourceRecordSets[?Type=='NS'].Name" \ - --output text - ) + for hosted_zone_id in $hosted_zone_ids; do + hosted_zone_name=$( + aws route53 list-resource-record-sets \ + --hosted-zone-id "$hosted_zone_id" \ + --query "ResourceRecordSets[?Type=='NS'].Name" \ + --output text + ) - aws route53 list-resource-record-sets \ - --hosted-zone-id "$hosted_zone_id" \ - --output text \ - --query " - ResourceRecordSets[?Type=='NS'].ResourceRecords[].[ - '$hosted_zone_name 300 IN NS', - Value - ]" + aws route53 list-resource-record-sets \ + --hosted-zone-id "$hosted_zone_id" \ + --output text \ + --query " + ResourceRecordSets[?Type=='NS'].ResourceRecords[].[ + '$hosted_zone_name 300 IN NS', + Value + ]" + done +} + +hosted-zone-a-records(){ + + # Generate NS records for delegating domain to AWS + # + # $ hosted-zone-a-records bash-my-aws.org + # + # $ hosted-zones | hosted-zone-a-records + + local inputs=$(skim-stdin "$@") + local hosted_zone_ids=$(echo "$inputs") + [[ -z "$hosted_zone_ids" ]] && __bma_usage "hosted-zone-id" && return 1 + + local hosted_zone_name + for hosted_zone_id in $hosted_zone_ids; do + + aws route53 list-resource-record-sets \ + --hosted-zone-id "$hosted_zone_id" \ + --output text \ + --query "ResourceRecordSets[?Type=='A'].[Name,AliasTarget.DNSName]" + done } From 85f501b63704c0ca0cea0c1f5a7a94a53a267fce Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 Jan 2024 12:17:40 +1100 Subject: [PATCH 18/20] build --- aliases | 3 ++- docs/command-reference.md | 35 +++++++++++++++++++++++++++++++++++ functions | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/aliases b/aliases index a6ef299d..58dac048 100644 --- a/aliases +++ b/aliases @@ -108,6 +108,7 @@ alias elbv2-dnsname='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-dnsname' alias elbv2-subnets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-subnets' alias elbv2-target-groups='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2-target-groups' alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s' +alias hosted-zone-a-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-a-records' alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records' alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones' alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal' @@ -247,7 +248,7 @@ alias vpcs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma vpcs' # We'll find a less suprising place for this in future # region() needs to be a function in order to let it # set AWS_DEFAULT_REGION in the current shell -function region() { +function region() { local inputs=$(skim-stdin "$@"); if [[ -z "$inputs" ]]; then echo "${AWS_DEFAULT_REGION:-'AWS_DEFAULT_REGION not set'}"; diff --git a/docs/command-reference.md b/docs/command-reference.md index 9fad1ae0..fb02397f 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -1800,6 +1800,41 @@ Generate NS records for delegating domain to AWS bash-my-aws.org. 300 IN NS ns-1464.awsdns-55.org. +### hosted-zones + +List Route53 Hosted Zones + + $ hosted-zones + /hostedzone/Z3333333333333 5 NotPrivateZone bash-my-aws.org. + /hostedzone/Z5555555555555 2 NotPrivateZone bash-my-universe.com. + /hostedzone/Z4444444444444 3 NotPrivateZone bashmyaws.org. + /hostedzone/Z1111111111111 3 NotPrivateZone bash-my-aws.com. + /hostedzone/Z2222222222222 3 NotPrivateZone bashmyaws.com. + + +### hosted-zone-ns-records + +Generate NS records for delegating domain to AWS + + $ hosted-zones bash-my-aws.org + /hostedzone/ZJ6ZCG2UD6OKX 5 NotPrivateZone bash-my-aws.org. + + $ hosted-zones bash-my-aws.org | hosted-zone-ns-records + bash-my-aws.org. 300 IN NS ns-786.awsdns-34.net. + bash-my-aws.org. 300 IN NS ns-1549.awsdns-01.co.uk. + bash-my-aws.org. 300 IN NS ns-362.awsdns-45.com. + bash-my-aws.org. 300 IN NS ns-1464.awsdns-55.org. + + +### hosted-zone-a-records + +Generate NS records for delegating domain to AWS + + $ hosted-zone-a-records bash-my-aws.org + + $ hosted-zones | hosted-zone-a-records + + ## s3-commands diff --git a/functions b/functions index 85f35768..ab8868e7 100644 --- a/functions +++ b/functions @@ -108,6 +108,7 @@ elbv2-dnsname elbv2-subnets elbv2-target-groups elbv2s +hosted-zone-a-records hosted-zone-ns-records hosted-zones iam-role-principal From 3204b3cb809030ce8aa1b0a5640999555b79d10c Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 Jan 2024 13:14:34 +1100 Subject: [PATCH 19/20] columnise --- lib/route53-functions | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/route53-functions b/lib/route53-functions index bdf7b434..75a0896b 100644 --- a/lib/route53-functions +++ b/lib/route53-functions @@ -83,6 +83,7 @@ hosted-zone-a-records(){ aws route53 list-resource-record-sets \ --hosted-zone-id "$hosted_zone_id" \ --output text \ - --query "ResourceRecordSets[?Type=='A'].[Name,AliasTarget.DNSName]" + --query "ResourceRecordSets[?Type=='A'].[Name,AliasTarget.DNSName]" | + columnise done } From 069ccfe537a9e1186f596f4591694b7081ed331e Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 1 Feb 2024 10:06:09 +1100 Subject: [PATCH 20/20] Merge branch 'master' into features/route53-fixes --- aliases | 2 +- docs/command-reference.md | 124 ++++++++++++++++++++++++++++---------- 2 files changed, 94 insertions(+), 32 deletions(-) diff --git a/aliases b/aliases index d2f92d75..df9e9a32 100644 --- a/aliases +++ b/aliases @@ -280,7 +280,7 @@ alias vpcs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma vpcs' # We'll find a less suprising place for this in future # region() needs to be a function in order to let it # set AWS_DEFAULT_REGION in the current shell -function region() { +function region() { local inputs=$(skim-stdin "$@"); if [[ -z "$inputs" ]]; then echo "${AWS_DEFAULT_REGION:-'AWS_DEFAULT_REGION not set'}"; diff --git a/docs/command-reference.md b/docs/command-reference.md index 1652aee9..192194de 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -1405,9 +1405,6 @@ List images for ECR Repositories ## ecs-commands - -### ecs-clusters2 - List ECS clusters output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount if you pass an argument, it'll filter for clusters whose ARN contains your text @@ -1419,7 +1416,7 @@ if you pass an argument, it'll filter for clusters whose ARN contains your text test2-ecs-cluster ACTIVE 3 3 0 -### ecs-services2 +### ecs-services List ECS services output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt @@ -1489,6 +1486,68 @@ Most of the things you want to know about a replication group are boolean eg AutomaticFailover, MultiAZClusterEnabled, AtRestEncryptionEnabled etc +### ecs-clusters + +List ECS clusters +output includes clusterName,status,activeServicesCount,runningTasksCount,pendingTasksCount +if you pass an argument, it'll filter for clusters whose ARN contains your text + + $ ecs-clusters test + test-octopus-ecs-cluster ACTIVE 1 1 0 + test1-ecs-cluster ACTIVE 3 1 0 + test3-ecs-cluster ACTIVE 3 1 0 + test2-ecs-cluster ACTIVE 3 3 0 + + +### ecs-services + +List ECS services +output includes serviceName,status,desiredCount,runningCount,pendingCount,createdAt + +gets all clusters if no filter passed in +if you do pass a filter: +1. if your filter is the name of one of your clusters, it will list the services in that cluster (eg ecs-clusters test1 | ecs-services) +2. if your filter is not a cluster name, it will list the services in all clusters whose names match your filter (ie it filters on cluster name not service name) +3. if you do not pass a filter, it will list all services in all clusters + + $ ecs-clusters test1|ecs-services + test1-ecs-admin-7URaUr0YGJHi ACTIVE 0 0 0 2023-09-13T17:16:48.198000+10:00 + test1-ecs-public-wEaTAqGXqbpq ACTIVE 0 0 0 2023-09-13T16:54:54.162000+10:00 + test1-ecs-hangfire-YNIo1hlx8rjn ACTIVE 1 1 0 2023-09-13T16:39:06.218000+10:00 + + +### ecs-tasks + +List ECS tasks +output includes taskDefinitionArn, createdAt, cpu, memory + +gets all tasks if no filter passed in +if you do pass a filter, it filters on the task name. All clusters are included (I haven't worked out a way of passing a cluster name AND a filter) + + $ ecs-tasks test2 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-public:18 2023-09-19T17:51:56.418000+10:00 2048 4096 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-admin:20 2023-08-29T10:03:36.956000+10:00 2048 4096 + arn:aws:ecs:ap-southeast-2:xxxxxxxxxxxx:task-definition/test2-hangfire:22 2023-09-19T17:11:06.622000+10:00 1024 2048 + + +### ecs-scaling-activities + +LIst autoscaling activities - the actual scaling events that have happened +eg +ecs-scaling www +2023-11-22T06:24:50.937000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmLow-64de4512-d901-4b26-a6a2-184bb1e90bc6 in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 2. Change successfully fulfilled by ecs. +2023-11-22T05:25:48.611000+11:00 www-ecs-public-ServicePublic-OuN3rXBLvmx3-AlarmHigh-6408c172-647e-4c0e-aac9-a800cd83317d in state ALARM triggered policy www-ecs-public-target-tracking-mem70 Successfully set desired count to 3. Change successfully fulfilled by ecs. + + +### ecs-scaling-actions + +List autoscaling actions - cron-based scheduled scaling +filter by environment (eg test1) or namespace (eg ecs) +if you pass an argument, it'll filter for clusters whose ARN contains your text + + $ scaling-ecs 'test.*down' # list the scale-down times of all our test environments + + ## elb-commands @@ -1987,6 +2046,18 @@ Generate NS records for delegating domain to AWS bash-my-aws.org. 300 IN NS ns-1464.awsdns-55.org. +### hosted-zone-a-records + +Generate NS records for delegating domain to AWS + + $ hosted-zone-a-records bash-my-aws.org + + $ hosted-zones | hosted-zone-a-records + + +## route53-commandsTEMP + + ### hosted-zones List Route53 Hosted Zones @@ -2013,15 +2084,6 @@ Generate NS records for delegating domain to AWS bash-my-aws.org. 300 IN NS ns-1464.awsdns-55.org. -### hosted-zone-a-records - -Generate NS records for delegating domain to AWS - - $ hosted-zone-a-records bash-my-aws.org - - $ hosted-zones | hosted-zone-a-records - - ## s3-commands @@ -2142,24 +2204,24 @@ Run a command locally on EC2 instance(s) running Windows $ ssm-instances Windows | ssm-send-command-windows Get-Hotfix Command ID: a0eeeddc-2edf-42bc-b0c7-122f5bc50956 Waiting for command to complete... - i-0fake1234abcd - Source Description HotFixID InstalledBy InstalledOn - ------ ----------- -------- ----------- ----------- - FAKEAPP01234 Update KB1234567 NT AUTHORITY\SYSTEM 10/11/2023 12:00:00 AM - FAKEAPP01234 Update KB8901234 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM - FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM - FAKEAPP01234 Update KB2345678 NT AUTHORITY\SYSTEM 1/9/2019 12:00:00 AM - FAKEAPP01234 Update KB3456789 NT AUTHORITY\SYSTEM 3/11/2021 12:00:00 AM - FAKEAPP01234 Security Update KB4567890 NT AUTHORITY\SYSTEM 4/21/2019 12:00:00 AM - FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 5/15/2019 12:00:00 AM - FAKEAPP01234 Security Update KB6789012 NT AUTHORITY\SYSTEM 6/12/2019 12:00:00 AM - ---Output truncated--- - i-0fake1234abcd - Source Description HotFixID InstalledBy InstalledOn - ------ ----------- -------- ----------- ----------- - FAKEAPP01234 Update KB1234567 NT AUTHORITY\SYSTEM 10/11/2023 12:00:00 AM - FAKEAPP01234 Update KB8901234 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM - FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM + i-0fake1234abcd + Source Description HotFixID InstalledBy InstalledOn + ------ ----------- -------- ----------- ----------- + FAKEAPP01234 Update KB1234567 NT AUTHORITY\SYSTEM 10/11/2023 12:00:00 AM + FAKEAPP01234 Update KB8901234 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM + FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM + FAKEAPP01234 Update KB2345678 NT AUTHORITY\SYSTEM 1/9/2019 12:00:00 AM + FAKEAPP01234 Update KB3456789 NT AUTHORITY\SYSTEM 3/11/2021 12:00:00 AM + FAKEAPP01234 Security Update KB4567890 NT AUTHORITY\SYSTEM 4/21/2019 12:00:00 AM + FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 5/15/2019 12:00:00 AM + FAKEAPP01234 Security Update KB6789012 NT AUTHORITY\SYSTEM 6/12/2019 12:00:00 AM + ---Output truncated--- + i-0fake1234abcd + Source Description HotFixID InstalledBy InstalledOn + ------ ----------- -------- ----------- ----------- + FAKEAPP01234 Update KB1234567 NT AUTHORITY\SYSTEM 10/11/2023 12:00:00 AM + FAKEAPP01234 Update KB8901234 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM + FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM See also: ssm-send-command-windows