Google Cloud Platform (GCP) serverless computing example
This repository contains resources for tracking static IP changes on a Google Cloud Platform (GCP) virtual machine VM. When a static IP change is detected, the affected VM is migrated within the GCP environment. The practical purpose of this is to repair a VM that is failing to register its new static IP address.
- Login to your GCP environment
- Open a Cloud Shell terminal
- Clone this repo into your Cloud Shell terminal
- Change the PROJECT_ID variable so that your own GCP project ID is assigned
- Change the SUBNET variable so that your own Virtual Private Cloud (VPC) subnet is assigned
- Execute the gcloud_script.sh shell script
- Check that your Pub/Sub topic and Cloud Function were created correctly
- Manually attach a different external IP to one of the VMs
- This static IP change should generate a Stackdriver event, which is captured by the Cloud Function
- The Cloud Function will perform a VM migration event, which in turn is tracked by Stackdriver
Please see the contents of the debugging folder, for resources to debug Google Cloud Functions
- Clone this repo to your workspace
- Navigate to the debugging folder
- Edit the prepare_log.sh script, adding in your Google Cloud Platform (GCP) specific values for project ID, zone, region and virtual machine (VM) instance ID
- Run prepare_log.sh, which will produce a file called prepared_log.json
- In the GCP console, create a Python 3 Cloud Function, the "Trigger" will be "Cloud Pub/Sub" and the "Topic" will be the one created in the above Installation steps; for "Source code" select "Inline editor" and for "Runtime" select "Python 3.x"
- Copy-paste the contents of debugger/main.py into the "main.py" field
- Continue creating the Cloud Function, naming it and the "Function to execute" as test_migrate_vm
- Once the Cloud Function is created, navigate to the "Testing" sub-tab
- Copy paste the contents of prepared_log.sh into the "Triggering event" textbox
- Click the "Test the function" button
- On correct execution, the "Output" should be "OK" and logs should be generated
- The print statement at the end of the Python code will output to Stackdriver log