This page describes how to create a startup script for the web service and then validate the script.
To ensure that bringing up the web service does not require manual intervention, you must create a startup script.The startup script does the following tasks:
Reads the virtual machine (VM) metadata and sets the environment variable for metadata with theCONNECTOR_ENVprefix. Any data required by consumers is taken during VM creation from Marketplace and is set as environment variables in docker. These environment variables can then be read and processed accordingly in the application.
Starts the docker container containing the web service with the appropriate environment variables.
The following code is a sample startup script:
#!/bin/bash# 1. Fetch Metadata Keysmetadata_keys_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/"metadata_keys=$(curl-H"Metadata-Flavor: Google""$metadata_keys_url")# 2. Set Environment Variables for CONNECTOR_ENV Keys (with error handling)forkeyin$metadata_keys;doif[[$key==CONNECTOR_ENV_*]];thenmetadata_value_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key"# Fetch value with error handlingvalue=$(curl-H"Metadata-Flavor: Google""$metadata_value_url"2>/dev/null)if[[-z"$value"]];thenecho"Warning: No value found for key '$key'. Skipping.">&2# Log the warning to stderrcontinue# Skip to the next iterationfiexport"$key=$value"fidone# 3. Run Docker with Environment Variablessudodockerstopconnector-service||truesudodockerrun\--nameconnector-service\$(env|grepCONNECTOR_ENV_|sed's/=/="/;s/$/"/'|sed's/^/-e /')\-d-p$CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT\--restart=unless-stopped\connector-container
Validate the startup script
In the VM instance, add the metadata for the port and all other parameters which are required during VM creation.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis guide outlines the process of creating a startup script to automate the launch of a web service within a virtual machine (VM).\u003c/p\u003e\n"],["\u003cp\u003eThe startup script fetches VM metadata, sets environment variables with the \u003ccode\u003eCONNECTOR_ENV\u003c/code\u003e prefix, and starts a Docker container with these variables.\u003c/p\u003e\n"],["\u003cp\u003eValidation of the startup script involves adding necessary metadata to the VM, running the script using \u003ccode\u003esudo google_metadata_script_runner startup\u003c/code\u003e, and verifying the Docker container's operation with \u003ccode\u003esudo docker ps\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe script uses \u003ccode\u003ecurl\u003c/code\u003e to fetch the needed values, and if no value is found for a specific key, it logs a warning and continues the process.\u003c/p\u003e\n"],["\u003cp\u003eThe provided commands also show how to add the startup script as a metadata, and how to stop and remove the docker after testing is complete.\u003c/p\u003e\n"]]],[],null,["# Create a startup script\n\nSee the [supported connectors](/integration-connectors/docs/connector-reference-overview) for Application Integration.\n\nCreate a startup script\n=======================\n\n|\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page describes how to create a startup script for the web service and then validate the script.\n\nTo ensure that bringing up the web service does not require manual intervention, you must create a startup script.The startup script does the following tasks:\n\n- Reads the virtual machine (VM) metadata and sets the environment variable for metadata with the `CONNECTOR_ENV` prefix. Any data required by consumers is taken during VM creation from Marketplace and is set as environment variables in docker. These environment variables can then be read and processed accordingly in the application.\n- Starts the docker container containing the web service with the appropriate environment variables.\n\nThe following code is a sample startup script: \n\n```bash\n#!/bin/bash\n\n# 1. Fetch Metadata Keys\nmetadata_keys_url=\"http://metadata.google.internal/computeMetadata/v1/instance/attributes/\"\nmetadata_keys=$(curl -H \"Metadata-Flavor: Google\" \"$metadata_keys_url\")\n\n# 2. Set Environment Variables for CONNECTOR_ENV Keys (with error handling)\nfor key in $metadata_keys; do\n if [[ $key == CONNECTOR_ENV_* ]]; then\n metadata_value_url=\"http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key\"\n\n # Fetch value with error handling\n value=$(curl -H \"Metadata-Flavor: Google\" \"$metadata_value_url\" 2\u003e/dev/null)\n if [[ -z \"$value\" ]]; then\n echo \"Warning: No value found for key '$key'. Skipping.\" \u003e&2 # Log the warning to stderr\n continue # Skip to the next iteration\n fi\n export \"$key=$value\"\n fi\ndone\n\n# 3. Run Docker with Environment Variables\nsudo docker stop connector-service || true\nsudo docker run \\\n --name connector-service \\\n $(env | grep CONNECTOR_ENV_ | sed 's/=/=\"/;s/$/\"/' | sed 's/^/-e /') \\\n -d -p $CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT \\\n --restart=unless-stopped \\\n connector-container\n```\n\n### Validate the startup script\n\n1. In the VM instance, add the metadata for the port and all other parameters which are required during VM creation. \n\n ```bash\n gcloud compute instances add-metadata VM_NAME \\ \n --zone=VM_ZONE \\\n --project=PROJECT_NAME \\\n --metadata=CONNECTOR_ENV_PORT=8081\n ```\n2. Edit the VM from UI and add the startup script mentioned in the automation section.\n You can also use the following gcloud command:\n\n ```bash\n gcloud compute instances add-metadata VM_NAME \\ \n --zone=VM_ZONE \\\n --project=PROJECT_NAME \\\n --metadata-from-file startup-script=gcp-start.sh\n ```\n3. After adding the startup script, ssh to the VM and run the following command: \n\n ```bash\n sudo google_metadata_script_runner startup\n ```\n4. Run the following command to ensure that the docker container is running on the mentioned port. \n\n ```bash\n sudo docker ps\n ```\n5. Run the following command to stop and remove the docker service. After testing, the service must not be running on the VM. \n\n ```bash\n sudo docker rm -f connector-service\n ```\n\nWhat's next\n-----------\n\n - Learn how to [create a VM deployment script](/integration-connectors/docs/marketplace/create-vm-deploy-tf)."]]