简体   繁体   中英

AWS CLI to delete all load balancer resources from AWS

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM