I want to delete all load balancers returned form my query below. The problem is creating an array, then looping through each item to delete from my Groovy pipeline script in Jenkins.
def load_balancer_names = sh(returnStdout: true, script: """ aws elbv2 describe-load-balancers | jq '[.LoadBalancers[] | select(.LoadBalancerName | startswith("loadbalancer-alb-")) | { LoadBalancerARN: .LoadBalancerARN } ]' """)
echo "Load balancer list: ${load_balancer_names}"
JSON output:
Load balancer list: [
{
"LoadBalancerName": "arn:aws:elasticloadbalancing:us-east-1:...-123"
},
{
"LoadBalancerName": "arn:aws:elasticloadbalancing:us-east-1:...-657"
}
]
AWS CLI delete command to delete load balancers:
aws elbv2 delete-load-balancer \
--load-balancer-arn [load balancer ARN]
CodePudding user response:
This should work for you:
aws elbv2 describe-load-balancers --query "LoadBalancers[?starts_with(LoadBalancerName,'loadbalancer-alb-')].LoadBalancerArn" --output text | tr "\t" "\n" | xargs -I{} aws elbv2 delete-load-balancer --load-balancer-arn {}
this command, which doesn't require jq
and only uses the AWS CLI, extracts the load balancer ARNs and then feeds them to xargs for deletion