An instance is created when a new file is uploaded to the storage. The startup runs a python script that generates a pdf file for the new file, uploads the pdf back to the storage and deletes the instance. Since the python script has pretty lengthy, I have stored the startup script and the python script in the same location (Cloud Storage). I have passed the paths are metadata while creating the instance. The input to the python script is the file name of the new file. I checked the logs of the instance, its throwing some errors there. Can someone point out what is it that I am doing wrong.
Edited
{
"cpuPlatform": "Intel Haswell",
"creationTimestamp": "2021-08-02T06:40:36.346-07:00",
"deletionProtection": false,
"disks": [
{
"autoDelete": true,
"boot": true,
"deviceName": "xyz",
"diskSizeGb": "10",
"guestOsFeatures": [
{
"type": "UEFI_COMPATIBLE"
},
{
"type": "VIRTIO_SCSI_MULTIQUEUE"
}
],
"index": 0,
"interface": "SCSI",
"kind": "compute#attachedDisk",
"licenses": [
"projects/debian-cloud/global/licenses/debian-10-buster"
],
"mode": "READ_WRITE",
"source": "projects/patch-us/zones/us-central1-a/disks/instance-name",
"type": "PERSISTENT"
}
],
"fingerprint": "XlZ7biyVpAI=",
"id": "3984870299667155772",
"kind": "compute#instance",
"labelFingerprint": "42WmSpB8rSM=",
"lastStartTimestamp": "2021-08-02T06:40:46.210-07:00",
"machineType": "projects/project-name/zones/us-central1-a/machineTypes/e2-medium",
"metadata": {
"fingerprint": "f5o3Pxed5VY=",
"items": [
{
"key": "startup-script-url",
"value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/start-script.sh"
},
{
"key": "file_name",
"value": "123456"
},
{
"key": "python_script_name",
"value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/generate_fd_report.py"
}
],
"kind": "compute#metadata"
},
"name": "instance-name",
"networkInterfaces": [
{
"accessConfigs": [
{
"kind": "compute#accessConfig",
"name": "External NAT",
"natIP": "35.202.255.222",
"networkTier": "PREMIUM",
"type": "ONE_TO_ONE_NAT"
}
],
"fingerprint": "565TD6a2Y2c=",
"kind": "compute#networkInterface",
"name": "nic0",
"network": "projects/project-name/global/networks/default",
"networkIP": "10.128.0.29",
"stackType": "IPV4_ONLY",
"subnetwork": "projects/project-name/regions/us-central1/subnetworks/default"
}
],
"scheduling": {
"automaticRestart": true,
"onHostMaintenance": "MIGRATE",
"preemptible": false
},
"selfLink": "projects/project-name/zones/us-central1-a/instances/instance-name",
"serviceAccounts": [
{
"email": "[email protected]",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform"
]
}
],
"shieldedInstanceConfig": {
"enableIntegrityMonitoring": true,
"enableSecureBoot": false,
"enableVtpm": true
},
"shieldedInstanceIntegrityPolicy": {
"updateAutoLearnPolicy": true
},
"startRestricted": false,
"status": "RUNNING",
"tags": {
"fingerprint": "42WmSpB8rSM="
},
"zone": "projects/project-name/zones/us-central1-a"
}
start-script-sh
#! /bin/bash
ECG_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_path -H "Metadata-Flavor: Google")
PYTHON_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/python_script_name -H "Metadata-Flavor: Google")
ECG_FILE_NAME = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_name -H "Metadata-Flavor: Google")
curl -s -o generate_fd.py PYTHON_FILE_PATH
chmod +x generate_fd.py
python3 generate_fd.py ECG_FILE_PATH &
generate_fd_report.py
#!/usr/bin/env python3
def main(file_name):
print("Hello")
main(file_name)
Logs
To download the script, URL path to which you've saved as the metadata value is as follows:
curl -s -o filename.txt $(curl -s http://metadata/computeMetadata/v1/instance/attributes/filename -H "Metadata-Flavor: Google")