LAB : https://www.qwiklabs.com/games/1431/labs/6763
VIDEO :
What to do ?
Create a Compute Engine VM with a static external IP address and a separate unused static external IP address
Deploy a Cloud Function to identify unused addresses
Create a Cloud Scheduler job to schedule the function to run by using an HTTP trigger
nano script.sh
gcloud services enable cloudscheduler.googleapis.com
git clone https://github.com/GoogleCloudPlatform/gcf-automated-resource-cleanup.git && cd gcf-automated-resource-cleanup/
export PROJECT_ID=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
export region=us-central1
WORKDIR=$(pwd)
cd $WORKDIR/unused-ip
export USED_IP=used-ip-address
export UNUSED_IP=unused-ip-address
gcloud compute addresses create $USED_IP --project=$PROJECT_ID --region=us-central1
gcloud compute addresses create $UNUSED_IP --project=$PROJECT_ID --region=us-central1
gcloud compute addresses list --filter="region:(us-central1)"
export USED_IP_ADDRESS=$(gcloud compute addresses describe $USED_IP --region=us-central1 --format=json | jq -r '.address')
gcloud compute instances create static-ip-instance \
--zone=us-central1-a \
--machine-type=n1-standard-1 \
--subnet=default \
--address=$USED_IP_ADDRESS
gcloud functions deploy unused_ip_function --trigger-http --runtime=nodejs8
gcloud scheduler jobs create http unused-ip-job \
--schedule="* 2 * * *" \
--uri=$FUNCTION_URL
gcloud scheduler jobs run unused-ip-job
gcloud compute addresses list --filter="region:(us-central1)"
Ctrl + X -> Y -> Enter