'How to enable GCP flow logs for entire vpc or all regions in one command?

Bash command to --enable-flow-logging for entire VPC or all regions with a VPC



Solution 1:[1]

Simple bash script to query all regions with a VPC and loop through to enable flow logging

a=( $(gcloud compute networks subnets list --filter="<VPC NAME>" --format="csv(region)")); a=("${a[@]:1}"); for i in "${a[@]}"; do gcloud compute networks subnets update <VPC NAME> --region=$i --enable-flow-logs; done

Remember to change out the 2 instances of "VPC NAME" above with the name of the VPC example default in most cases

Can be used to turn off flow logging for entire vpc as well using --no-enable-flow-logging

a=( $(gcloud compute networks subnets list --filter="<VPC NAME>" --format="csv(region)")); a=("${a[@]:1}"); for i in "${a[@]}"; do gcloud compute networks subnets update <VPC NAME> --region=$i --no-enable-flow-logs; done

Solution 2:[2]

Here's my attempt, using the default VPC:

gcloud compute networks subnets list | awk '/default/{print$2}' | xargs -I{} gcloud compute networks subnets update default --region={} --enable-flow-logs

I think you can replace default with your VPC name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rupin Talreja
Solution 2 Keeth